Skip to content

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

Description

@WindingWinter

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions