Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution build always fail with CS2012 "Cannot open dll for writing" errors #45034

Open
WindingWinter opened this issue Nov 22, 2024 · 1 comment
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@WindingWinter
Copy link

WindingWinter commented Nov 22, 2024

Related to #9964 and #11593, but this time, it seems that I can always always reproduce it with the below solution and csprojects.

The solution file:


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSProjectMultiConstants", "CSProjectMultiConstants\CSProjectMultiConstants.csproj", "{9AA1B407-BD5F-486D-BE56-CBCD639B8A23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultipleConstantTest", "MultipleConstantTest\MultipleConstantTest.csproj", "{90C599A9-4DD3-4B22-95EB-D70F514E76DB}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{9AA1B407-BD5F-486D-BE56-CBCD639B8A23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{9AA1B407-BD5F-486D-BE56-CBCD639B8A23}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{9AA1B407-BD5F-486D-BE56-CBCD639B8A23}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{9AA1B407-BD5F-486D-BE56-CBCD639B8A23}.Release|Any CPU.Build.0 = Release|Any CPU
		{90C599A9-4DD3-4B22-95EB-D70F514E76DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{90C599A9-4DD3-4B22-95EB-D70F514E76DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{90C599A9-4DD3-4B22-95EB-D70F514E76DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{90C599A9-4DD3-4B22-95EB-D70F514E76DB}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {21D359E2-E91A-4F4F-B54B-5B41A82BCF88}
	EndGlobalSection
EndGlobal


The CSProjectMultiConstants.csproj

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<C3DVersion>Ver2023</C3DVersion>
		 <PublishDir>bin\Publish\$(C3DVersion)</PublishDir>
	</PropertyGroup>
	<PropertyGroup>
		<OutputType>WinExe</OutputType>
		<TargetFramework>net8.0-windows</TargetFramework>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
		<UseWPF>true</UseWPF>
		<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> 
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
		<PlatformTarget>AnyCPU</PlatformTarget>
		<Prefer32Bit>false</Prefer32Bit>
		<OutputPath>bin\Debug\$(C3DVersion)\</OutputPath>
		<DefineConstants>DEBUG;Trace;$(C3DVersion)</DefineConstants>
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
		<PlatformTarget>AnyCPU</PlatformTarget>
		<OutputPath>bin\Release\$(C3DVersion)\</OutputPath>
		<Prefer32Bit>false</Prefer32Bit>
		<DebugType>none</DebugType>
		<DefineConstants>Trace;$(C3DVersion)</DefineConstants>
		<IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\$(C3DVersion)</IntermediateOutputPath>
	</PropertyGroup>
	
    <Target Name="BuildVer2023">
    <Exec Command="dotnet build -p:C3DVersion=Ver2023 -c $(Configuration)"/>
	 </Target>

</Project>


The MultipleConstantTest.csproj


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
	<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>  

  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\CSProjectMultiConstants\CSProjectMultiConstants.csproj" />
  </ItemGroup>
  <Target Name="BuildVer2023">
    <Exec Command="dotnet build -p:C3DVersion=Ver2023 -c $(Configuration)"/>
  </Target>
  
</Project>

The actual content of the cs files doesn't seem to matter.

And now, this is how I write my batch script


set slnFile=%~dp0CSProjectMultiConstants.sln

dotnet clean "%slnFile%" --configuration Debug  REM comment the dotnet clean line out will make the error disappears
dotnet build "%slnFile%" -t:BuildVer2023 -c Debug


If I run the above .bat file, then I will surely get crash as such:


E:\>dotnet clean "D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants.sln" --configuration Debug

Build succeeded in 0.4s

E:\>dotnet build "D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants.sln" -t:BuildVer2023 -c Debug
  MultipleConstantTest net8.0 failed with 3 error(s) (1.2s)
    CSC : error CS2012: Cannot open 'D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants\obj\Debug\CSProjectMultiConstants.dll' for writing -- 'The process cannot access the file 'D\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants\obj\Debug\CSProjectMultiConstants.dll' because it is being used by another process.' [D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants\CSProjectMultiConstants_dfx0esc2_wpftmp.csproj]
    CSC : error CS2012: Cannot open 'D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants\obj\Debug\CSProjectMultiConstants.dll' for writing -- 'The process cannot access the file 'D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants\obj\Debug\CSProjectMultiConstants.dll' because it is being used by another process.' [D:\.Net Plain\CSProjectMultiConstants\CSProjectMultiConstants\CSProjectMultiConstants_dfx0esc2_wpftmp.csproj]
    D:\.Net Plain\CSProjectMultiConstants\MultipleConstantTest\MultipleConstantTest.csproj(15,5): error MSB3073: The command "dotnet build -p:C3DVersion=Ver2023 -c Debug" exited with code 1.

Build failed with 3 error(s) in 1.5s

The crash can be reproduced even after I restart my machine. And consistently, every single time. Regardless of whether VS 2022 is opened at that time or not.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Nov 22, 2024
@WindingWinter WindingWinter changed the title Solution build always fail with CS2012 "Cannot open" file errors Solution build always fail with CS2012 "Cannot open dll for writing" errors Nov 22, 2024
@WindingWinter
Copy link
Author

WindingWinter commented Nov 22, 2024

And I can avoid the problem if either I

  1. Don't dotnet clean before dotnet build
  2. Remove MultipleConstantTest.csproj, and effective make the solution a one project solution.
  3. Remove -p:C3DVersion=Ver2023 from both of the csproj files

Condition 3) especially leads me to think that this is a bug in dotnet build, I've no idea why -p flag will interfere with the build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

1 participant