templating
templating copied to clipboard
Internal - Add proper x-process synchronization during test templates packing
Background
CI tests are occasionaly failing with file access error in PackageManager (TestHelper). E.g. (failed run):
System.Exception : Failed to pack the project D:\a\1\s\test\Microsoft.TemplateEngine.TestTemplates\Microsoft.TemplateEngine.TestTemplates.csproj: \r\nStdOut: MSBuild version 17.4.0-preview-22368-02+c8492483a for .NET\r\n Determining projects to restore...\r\n Restored D:\a\1\s\test\Microsoft.TemplateEngine.TestTemplates\Microsoft.TemplateEngine.TestTemplates.csproj (in 509 ms).\r\n Microsoft.TemplateEngine.TestTemplates -> D:\a\1\s\artifacts\bin\Microsoft.TemplateEngine.TestTemplates\Debug\net7.0\Microsoft.TemplateEngine.TestTemplates.dll\r\nD:\a\1\s\.dotnet\sdk\7.0.100-preview.7.22377.5\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(221,5): error : The process cannot access the file 'D:\a\1\s\artifacts\obj\Microsoft.TemplateEngine.TestTemplates\Debug\Microsoft.TemplateEngine.TestTemplates.8.0.100-dev.nuspec' because it is being used by another process. [D:\a\1\s\test\Microsoft.TemplateEngine.TestTemplates\Microsoft.TemplateEngine.TestTemplates.csproj]\r\n.\r\nStdErr: .
at
at Microsoft.TemplateEngine.TestHelper.PackageManager.PackNuGetPackage(String projectPath) in /_/test/Microsoft.TemplateEngine.TestHelper/PackageManager.cs:line 116
at Microsoft.TemplateEngine.Tests.TestBase.PackTestTemplatesNuGetPackage(PackageManager packageManager) in /_/test/Shared/TestBase.cs:line 49
at Microsoft.TemplateSearch.TemplateDiscovery.IntegrationTests.TemplateDiscoveryTests.CanDetectRemovedPackagesInDiffMode() in /_/test/Microsoft.TemplateSearch.TemplateDiscovery.IntegrationTests/TemplateDiscoveryTests.cs:line 344
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Proposal
Test templates are being prepared by multiple tests that can run in parallel. We should add a named mutex to prevent concurrent attempts to create same pack (the packing process is quite expensive - the extra mutex cost will be negligable). This way we should not see the error in CI again. Although quite rare - it still consumes some cycles to look on the reason and rerun and fix is very easy
Another failed CI run: https://github.com/dotnet/templating/pull/5314/checks?check_run_id=8514891843
Done in #5338