Multiple AddCSharpApp calls to projects in the same repo can cause build contention
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 201
Description
I'm converting https://github.com/dotnet/eshop to use a TypeScript AppHost, which means I need to use `builder.addCSharpApp` to reference all the projects. When making changes that force a recompile, I sometimes get race conditions in my build:
```
CSC : error CS2012: Cannot open 'D:\git\eShop\artifacts\obj\EventBus\debug\EventBus.dll' for writing --
The process cannot access the file 'D:\git\eShop\artifacts\obj\EventBus\debug\EventBus.dll' because it is being used by another process.; file may be locked by 'VBCSCompiler' (7700) [D:\git\eShop\src\EventBus\EventBus.csproj]
```
```
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.GenerateAssemblyInfo.targets(182,5):
error MSB3491: Could not write lines to file "D:\git\eShop\artifacts\obj\EventBus\debug\EventBus.AssemblyInfoInputs.cache". Cannot create a file when that file already exists. [D:\git\eShop\src\EventBus\EventBus.csproj]
```
```
CSC : error CS2012: Cannot open 'D:\git\eShop\artifacts\obj\eShop.ServiceDefaults\debug\eShop.ServiceDefaults.dll' for writing --
The process cannot access the file 'D:\git\eShop\artifacts\obj\eShop.ServiceDefaults\debug\eShop.ServiceDefaults.dll' because it is being used by another process.; file may be locked by 'VBCSCompiler' (60952) [D:\git\eShop\src\eShop.ServiceDefaults\eShop.ServiceDefaults.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018:
The "GenerateDepsFile" task failed unexpectedly. [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: System.IO.IOException: The process cannot access the file 'D:\git\eShop\artifacts\bin\EventBusRabbitMQ\debug\EventBusRabbitMQ.deps.json' because it is being used by another process. [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options) [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at System.IO.File.Create(String path) [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateDepsFile.WriteDepsFile(String depsFilePath) [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Execute() [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(308,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [D:\git\eShop\src\EventBusRabbitMQ\EventBusRabbitMQ.csproj]
```
This is because multiple AddCSharpApp calls will build each project simutaneously. And any common projects referenced by those projects will get built at the same time, and MSBuild will complain.
From @JamesNK:
To fix, one option would be to build AddCSharpApp sequentially instead of in parallel.
Or generate a slnx/proj that references the AddCSharpApp instances (might not be possible with single file apps?) and build them together
Contributor guide
Research direction
Reproduce the contention with the TypeScript AppHost in dotnet/eshop and multiple AddCSharpApp calls, then start at the AddCSharpApp entry point to understand how the projects are built. Done means shared projects no longer build concurrently and the reported MSBuild file-lock and duplicate-file errors do not occur.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, typescript
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100