GitVersion icon indicating copy to clipboard operation
GitVersion copied to clipboard

[Bug] WriteVersionInfoToBuildLog sometimes fails on Windows due to gitversion.properties file lock

Open junlinz2 opened this issue 2 years ago • 2 comments

Describe the bug We are running .NET class library builds on a Jenkins CI system. The MSBuild task WriteVersionInfoToBuildLog will sometimes fail on Windows with the following stack trace:

C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error : IOException: The process cannot access the file 'C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\gitversion.properties' because it is being used by another process. [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options) [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at System.IO.StreamWriter.ValidateArgsAndOpenPath(String path, Boolean append, Encoding encoding, Int32 bufferSize) [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at System.IO.File.WriteAllLines(String path, IEnumerable`1 contents, Encoding encoding) [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at GitVersion.BuildAgents.Jenkins.WriteIntegration(Action`1 writer, VersionVariables variables, Boolean updateBuildNumber) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\BuildAgents\Jenkins.cs:line 46 [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at GitVersion.VersionConverters.OutputGenerator.OutputGenerator.Execute(VersionVariables variables, OutputContext context) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionConverters\OutputGenerator\OutputGenerator.cs:line 35 [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at GitVersion.GitVersionOutputTool.OutputVariables(VersionVariables variables, Boolean updateBuildNumber) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionOutputTool.cs:line 39 [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at GitVersion.MsBuild.GitVersionTaskExecutor.WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\GitVersionTaskExecutor.cs:line 78 [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at GitVersion.MsBuild.GitVersionTasks.<>c__DisplayClass3_0.<WriteVersionInfoToBuildLog>b__0(IGitVersionTaskExecutor executor) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\GitVersionTasks.cs:line 19 [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :    at GitVersion.MsBuild.GitVersionTasks.ExecuteGitVersionTask[T](T task, Action`1 action) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\GitVersionTasks.cs:line 30 [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]
   C:\Users\user\.nuget\packages\gitversion.msbuild\5.12.0\tools\GitVersion.MsBuild.targets(13,9): error :  [C:\J\workspace\toolkit-foo_develop\Project\toolkit-foo\src\Toolkit.Foo\Toolkit.Foo.csproj::TargetFramework=net48]

0 Warning(s)
1 Error(s)

Each Jenkins job runs from a brand new folder, where the pipeline:

  1. Wipes the contents of the job workspace
  2. Checks out using git the folder of build scripts
  3. Checks out using git the solution (toolkit-foo) that contains a PackageReference to GitVersion.MsBuild
  4. Call dotnet build-server shutdown which should clean up any previous build server processes
  5. Calls the appropriate build script, which has the line dotnet build --configuration Release --no-incremental --verbosity normal <project>.csproj used to build the project.

The Visual Studio solution and project has a folder structure as follows:

src
|_ Toolkit.Foo
    |_ Toolkit.Foo.csproj
    |_ gitversion.properties (file produced in step 5)
Toolkit.Foo.sln

Expected Behavior

The MSBuild task should be able to consistently access this file for writing/modification without any file locking errors.

Actual Behavior

Usually, the expected behaviour is seen. However, occasionally GitVersion fails to access the file in question, as shown in the stack trace.

Possible Fix

Steps to Reproduce

This issue is difficult to reproduce issue consistently, but:

  • Toolkit.Foo.csproj has the following package reference:
<ItemGroup>
   <PackageReference Include="GitVersion.MsBuild" Version="5.12.0" PrivateAssets="contentfiles;analyzers"/>
</ItemGroup>
  • Project runs under Jenkins, so JENKINS_URL is set in that environment. I believe that as per https://gitversion.net/docs/usage/msbuild, WriteVersionInfoToBuildLog is called automatically and writes a .properties file due to said environment variable being set.

Context

We are building a series of .NET class libraries sequentially which in turn are depended on by application repos. The class library next in line, that depends on Toolkit.Foo will only start to build if the build job for Toolkit.Foo is marked as successful in Jenkins.

Your Environment

  • Version Used: 5.12.0
  • Operating System and version (Windows 10, Ubuntu 18.04): Windows 11 22H2
  • Link to your project:
  • Link to your CI build (if appropriate):

junlinz2 avatar Oct 25 '23 06:10 junlinz2

Hi there.

I think you need to investigate this problem in more detail and find the root cause. Why do you think it is a problem in GitVersion?

If I view the code I don't see anything special: image

Obviously another process blocking this file. How would GitVersion solve this if the executable is not the owner of the file handler? Do you have maybe concurrent runs? Does a sleep between the steps mitigate this problem?

HHobeck avatar Jan 05 '24 09:01 HHobeck

This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs.

github-actions[bot] avatar Apr 04 '24 11:04 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Thank you for your contributions

github-actions[bot] avatar May 05 '24 11:05 github-actions[bot]