nx-dotnet: [BUG] Concurrency errors when accessing same files as parallel test

Current Behavior

When testing 2 projects targeting the same file, we get a CS1202 The process cannot access the file '' because it is being used by another process. These are caused by 3 items:

  1. a runtimeconfig.json'
  2. a .deps.json
  3. a .dll file

All the .net standard and .net 6 projects have RestorePackagesWithLockFile on them

 <PropertyGroup>
    <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
  </PropertyGroup>

this is the dependency graph image

Expected Behavior

That the nx affected --target test runs and does not lock the file, therefore running the dotnet projects parallel

Github Repo

No response

Steps to Reproduce

  1. Have 3 dotnet items(projects/libs) with test projects, where 1 project is being used by 2 test projects
  2. (don’t know if this is affecting it) Have package restore on them
  3. run the tests

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 20.3.0
   OS     : win32 x64
   npm    : 9.6.4
   Hasher : Native

   nx                 : 16.0.3
   @nx/js             : 16.0.3
   @nx/jest           : 16.0.3
   @nx/linter         : 16.0.3
   @nx/workspace      : 16.0.3
   @nx/angular        : 16.0.3
   @nx/cypress        : 16.0.3
   @nx/devkit         : 16.0.3
   @nx/eslint-plugin  : 16.0.3
   @nx/plugin         : 16.0.3
   @nrwl/tao          : 16.0.3
   @nx/webpack        : 16.0.3
   nx-cloud           : 16.0.5
   typescript         : 4.9.5
   ---------------------------------------
   Community plugins:
   @fortawesome/angular-fontawesome : 0.12.1
   @ngrx/component-store            : 15.4.0
   @ngrx/component                  : 15.4.0
   @ngrx/effects                    : 15.4.0
   @ngrx/entity                     : 15.4.0
   @ngrx/router-store               : 15.4.0
   @ngrx/store-devtools             : 15.4.0
   @ngrx/store                      : 15.4.0
   @ngrx/schematics                 : 15.4.0
   @nx-dotnet/core                  : 1.22.0
   ---------------------------------------
   Local workspace plugins:
         @meteninbim/workspace-plugin

nx.json

{
	"npmScope": "meteninbim",
	"affected": {
		"defaultBase": "master"
	},
	"tasksRunnerOptions": {
		"default": {
			"runner": "nx-cloud",
			"options": {
				"cacheableOperations": ["build", "lint", "test", "e2e"],
				"parallel": 3
			}
		}
	},
	"plugins": ["@nx-dotnet/core"],
	"generators": {
		"@nx/angular:application": {
			"style": "scss",
			"linter": "eslint",
			"unitTestRunner": "jest",
			"e2eTestRunner": "cypress"
		},
		"@nx/angular:library": {
			"linter": "eslint",
			"unitTestRunner": "jest"
		},
		"@schematics/angular:component": {
			"style": "scss",
			"changeDetection": "OnPush",
			"project": "client",
			"path": "apps/client/src/app"
		},
		"@nx/angular:component": {
			"style": "scss",
			"changeDetection": "OnPush",
			"project": "client",
			"path": "apps/client/src/app"
		}
	},
	"$schema": "./node_modules/nx/schemas/nx-schema.json",
	"cli": {
		"cache": {
			"enabled": true
		}
	},
	"targetDefaults": {
		"build": {
			"dependsOn": ["^build"],
			"inputs": ["production", "^production"]
		},
		"test": {
			"inputs": [
				"default",
				"^production",
				"{workspaceRoot}/jest.preset.js"
			]
		},
		"lint": {
			"inputs": ["default", "{workspaceRoot}/.eslintrc.json"]
		},
		"lint-fix": {
			"inputs": ["default", "{workspaceRoot}/.eslintrc.json"]
		},
		"serve": {
			"dependsOn": ["^build"]
		}
	},
	"namedInputs": {
		"default": ["{projectRoot}/**/*", "sharedGlobals"],
		"sharedGlobals": [],
		"production": [
			"default",
			"!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
			"!{projectRoot}/tsconfig.spec.json",
			"!{projectRoot}/jest.config.[jt]s",
			"!{projectRoot}/.eslintrc.json"
		]
	}
}

Failure Logs

Executing Command: dotnet "test" "C:\Source\repos\DigitaalMeten\3d\meteninbim\apps\api-integration-test\Meteninbim.Api.IntegrationTest.csproj" "--settings" "../../csharp.runsettings" "--results-directory" "../../coverage/apps/api-integration-test"
  Determining projects to restore...
         Restored C:\Source\repos\DigitaalMeten\3d\meteninbim\apps\api\Meteninbim.Api.csproj (in 724 ms).
         7 of 8 projects are up-to-date for restore.
         Meteninbim.Bimsync -> C:\Source\repos\DigitaalMeten\3d\meteninbim\dist\libs\bimsync\netstandard2.0\Meteninbim.Bimsync.dll
         Meteninbim.Common -> C:\Source\repos\DigitaalMeten\3d\meteninbim\dist\libs\shared\MeteninBim.Common\netstandard2.0\Meteninbim.Common.dll
       CSC : error CS2012: Cannot open 'C:\Source\repos\DigitaalMeten\3d\meteninbim\libs\business\obj\Debug\net6.0\Meteninbim.Business.dll' for writing -- 'The process cannot access the file 'C:\Source\repos\DigitaalMeten\3d\meteninbim\libs\business\obj\Debug\net6.0\Meteninbim.Business.dll' because it is being used by another process.' [C:\Source\repos\DigitaalMeten\3d\meteninbim\libs\business\Meteninbim.Business.csproj]
         Meteninbim.Shared.Startup -> C:\Source\repos\DigitaalMeten\3d\meteninbim\dist\libs\shared\startup\net6.0\Meteninbim.Shared.Startup.dll
       Error: dotnet execution returned status code 1
    at DotNetClient.logAndExecute (C:\Source\repos\DigitaalMeten\3d\meteninbim\node_modules\@nx-dotnet\dotnet\src\lib\core\dotnet.client.js:171:19)
    at DotNetClient.test (C:\Source\repos\DigitaalMeten\3d\meteninbim\node_modules\@nx-dotnet\dotnet\src\lib\core\dotnet.client.js:78:25)
    at C:\Source\repos\DigitaalMeten\3d\meteninbim\node_modules\@nx-dotnet\core\src\executors\test\executor.js:17:41
    at Generator.next (<anonymous>)
    at fulfilled (C:\Source\repos\DigitaalMeten\3d\meteninbim\node_modules\tslib\tslib.js:164:62)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Additional Information

i tried removing RestorePackagesWithLockFile, but this still showed the same error

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 5
  • Comments: 21 (10 by maintainers)

Most upvoted comments

This is interesting… I’ll look into it, may be locking happening on the .NET side of things. We don’t really touch those files so I’ll have to see if its something we can get around.

Just wanted to follow up here too. We’re seeing the same locally when running builds with parallelization. It takes 1-2 tries to get past the errors and get everything to go green. As previously mentioned by others, we need to disable parallelization in CI in order to avoid the aforementioned, but it makes things pretty slow. Here’s some log output confirm what others have also describe:

dotnet-billing:        (GenerateBuildDependencyFile target) -> 
dotnet-billing:          /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018: The "GenerateDepsFile" task failed unexpectedly. [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018: System.IO.IOException: The process cannot access the file '/Users/wojtek/src/Acme/redacted/dist/libs/dotnet/shared/util/Acme.Util/net6.0/Acme.Util.deps.json' because it is being used by another process. [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at Microsoft.Win32.SafeHandles.SafeFileHandle.Init(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Int64& fileLength, UnixFileMode& filePermissions) [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, UnixFileMode openPermissions, Int64& fileLength, UnixFileMode& filePermissions, Func`4 createOpenException) [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at System.IO.File.Create(String path) [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at Microsoft.NET.Build.Tasks.GenerateDepsFile.WriteDepsFile(String depsFilePath) [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at Microsoft.NET.Build.Tasks.TaskBase.Execute() [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:        /usr/local/share/dotnet/sdk/7.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(197,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/Users/wojtek/src/Acme/redacted/libs/dotnet/shared/util/Acme.Util/Acme.Util.csproj]
dotnet-billing:     14 Warning(s)
dotnet-billing:     1 Error(s)
dotnet-billing: Time Elapsed 00:00:31.50
dotnet-billing:  >  NX   dotnet execution returned status code 1
dotnet-billing:    Pass --verbose to see the stacktrace.

Set noBuild true for the test target, no dependencies true for the build target, and test to depend on build, and build to depend on dependant project builds