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

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

Closed
junlinz2 opened this issue Oct 25, 2023 · 3 comments

Comments

@junlinz2
Copy link

junlinz2 commented Oct 25, 2023

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 junlinz2 added the bug label Oct 25, 2023
@HHobeck
Copy link
Contributor

HHobeck commented Jan 5, 2024

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?

Copy link

github-actions bot commented Apr 4, 2024

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 github-actions bot added the stale label Apr 4, 2024
Copy link

github-actions bot commented May 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants