diff --git a/.github/workflows/cli-starter-validation.yml b/.github/workflows/cli-starter-validation.yml index 2a906eeeea9..41c98e39035 100644 --- a/.github/workflows/cli-starter-validation.yml +++ b/.github/workflows/cli-starter-validation.yml @@ -144,10 +144,20 @@ jobs: Copy-Item -Path $cliLogsDir -Destination $sharedLogsDir -Recurse -Force } + # upload-artifact fails if a file it found disappears before it reads it. Starter project + # dependency installers can still change node_modules after `aspire stop`, so upload an archive. + - name: Archive starter validation diagnostics + if: always() + shell: pwsh + run: | + & "$env:GITHUB_WORKSPACE/eng/scripts/stage-cli-starter-validation-diagnostics.ps1" ` + -SourceRoot (Join-Path $env:RUNNER_TEMP 'aspire-cli-starter-validation') ` + -DestinationRoot (Join-Path $env:RUNNER_TEMP 'aspire-cli-starter-validation-upload') + - name: Upload starter validation diagnostics if: always() uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: cli-starter-validation-${{ inputs.artifactSuffix }} - path: ${{ runner.temp }}/aspire-cli-starter-validation + path: ${{ runner.temp }}/aspire-cli-starter-validation-upload/starter-validation.tar if-no-files-found: ignore diff --git a/eng/github-ci/test-trigger-map.yml b/eng/github-ci/test-trigger-map.yml index 68f4c9274aa..919903db73e 100644 --- a/eng/github-ci/test-trigger-map.yml +++ b/eng/github-ci/test-trigger-map.yml @@ -352,8 +352,9 @@ path_rules: - .github/workflows/cli-starter-validation.yml - eng/scripts/cli-starter-validation.ps1 - eng/scripts/get-aspire-cli-pr.ps1 + - eng/scripts/stage-cli-starter-validation-diagnostics.ps1 targets: [job:cli-starter-validation] - reason: the reusable workflow, PR installer, and scenario script implement starter validation + reason: the reusable workflow, PR installer, scenario script, and diagnostics staging script implement starter validation - paths: - .github/workflows/native-dashboard-validation.yml - eng/scripts/test-native-dashboard.ps1 @@ -563,6 +564,7 @@ path_rules: # Script behavior and packaging fixtures. - eng/scripts/download-native-archives.ps1 - eng/scripts/stage-native-cli-tool-packages.ps1 + - eng/scripts/stage-cli-starter-validation-diagnostics.ps1 - eng/scripts/stabilization-smoke-init-restore.sh - eng/scripts/validate-npm-package-signatures.ps1 - eng/scripts/validate-npm-release-aliases.ps1 diff --git a/eng/scripts/cli-starter-validation.ps1 b/eng/scripts/cli-starter-validation.ps1 index d7daf886c22..a496d019ebe 100644 --- a/eng/scripts/cli-starter-validation.ps1 +++ b/eng/scripts/cli-starter-validation.ps1 @@ -433,8 +433,9 @@ foreach ($template in $templates) } # The diagnostics artifact preserves the generated project but does not need installed - # dependencies. Remove them after all Aspire processes stop so upload-artifact cannot race - # a package installer cleanup and fail while stat-ing a disappearing node_modules directory. + # dependencies, so free their disk space here. This removal is best effort: a package + # installer can still change node_modules after `aspire stop` returns. The workflow + # uploads an archive without node_modules, so it does not depend on this removal. foreach ($relativeDependencyPath in @('node_modules', 'frontend/node_modules')) { $dependencyPath = Join-Path $projectRoot $relativeDependencyPath diff --git a/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 b/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 new file mode 100644 index 00000000000..49c4b161e5d --- /dev/null +++ b/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 @@ -0,0 +1,77 @@ +<# +.SYNOPSIS + Archives Aspire CLI starter validation diagnostics for artifact upload. + +.DESCRIPTION + actions/upload-artifact first enumerates every path under the upload root and then calls + fs.stat on each result. It does not tolerate an entry that disappears in between and fails with: + + ENOENT: no such file or directory, stat '/aspire-go-starter/AspireCliGoStarterSmoke/frontend/node_modules' + + The generated starter projects contain node_modules trees. A JavaScript package installer, such + as an interrupted `frontend-installer` npm install, can still rewrite or delete them after + `aspire stop` returns. Upload a completed tar archive instead. tar excludes node_modules without + traversing them and stores symbolic links without following them. Hidden files and directories + remain excluded, matching upload-artifact's default. The archive contains the validation root + directory and its retained contents. + +.PARAMETER SourceRoot + The starter validation root that contains generated projects, diagnostics, and CLI logs. + +.PARAMETER DestinationRoot + The directory in which to create starter-validation.tar for upload. +#> +[CmdletBinding()] +param( + [Parameter(Mandatory = $true)] + [string]$SourceRoot, + + [Parameter(Mandatory = $true)] + [string]$DestinationRoot +) + +$ErrorActionPreference = 'Stop' +$PSNativeCommandUseErrorActionPreference = $false + +$archivePath = Join-Path $DestinationRoot 'starter-validation.tar' +$temporaryArchivePath = "$archivePath.partial" +foreach ($path in @($archivePath, $temporaryArchivePath)) +{ + if (Test-Path -LiteralPath $path) + { + Remove-Item -LiteralPath $path -Force + } +} + +if (-not (Test-Path -LiteralPath $SourceRoot -PathType Container)) +{ + Write-Host "No starter validation diagnostics found at '$SourceRoot'." + return +} + +New-Item -ItemType Directory -Path $DestinationRoot -Force | Out-Null +try +{ + # Both GNU tar and BSD/Windows tar exclude this directory name at any depth without entering it. + # Do not dereference links or ignore tar failures: errors in retained diagnostics must stay visible. + # Archive the named root from its parent: using "." lets BSD tar's hidden-file pattern exclude + # the entire "./" tree. ".?*" excludes hidden names at any depth, including "..name". + # https://github.com/actions/upload-artifact#uploading-hidden-files + $sourceDirectory = Get-Item -LiteralPath $SourceRoot + & tar --exclude=node_modules '--exclude=.?*' -cf $temporaryArchivePath -C $sourceDirectory.Parent.FullName $sourceDirectory.Name + if ($LASTEXITCODE -ne 0) + { + throw "Archiving starter validation diagnostics failed with tar exit code $LASTEXITCODE." + } + + Move-Item -LiteralPath $temporaryArchivePath -Destination $archivePath -Force +} +finally +{ + if (Test-Path -LiteralPath $temporaryArchivePath) + { + Remove-Item -LiteralPath $temporaryArchivePath -Force + } +} + +Write-Host "Archived starter validation diagnostics from '$SourceRoot' to '$archivePath'." diff --git a/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs b/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs new file mode 100644 index 00000000000..f9854f54552 --- /dev/null +++ b/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs @@ -0,0 +1,190 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Formats.Tar; +using Aspire.TestUtilities; +using Xunit; + +namespace Infrastructure.Tests; + +/// +/// Tests for eng/scripts/stage-cli-starter-validation-diagnostics.ps1 +/// +public sealed class StageCliStarterValidationDiagnosticsTests : IDisposable +{ + private readonly TemporaryWorkspace _workspace; + private readonly string _scriptPath; + private readonly ITestOutputHelper _output; + + public StageCliStarterValidationDiagnosticsTests(ITestOutputHelper output) + { + _output = output; + _workspace = TemporaryWorkspace.Create(output); + _scriptPath = Path.Combine(RepoRoot.Path, "eng", "scripts", "stage-cli-starter-validation-diagnostics.ps1"); + } + + public void Dispose() => _workspace.Dispose(); + + [Fact] + [RequiresTools(["pwsh", "tar"])] + public async Task ArchivesDiagnosticsWithoutNodeModulesOrHiddenFiles() + { + var sourceRoot = Path.Combine(_workspace.Path, "validation with spaces"); + var destinationRoot = Path.Combine(_workspace.Path, "upload with spaces"); + WriteFile(sourceRoot, "cli-logs/cli.log"); + WriteFile(sourceRoot, ".aspire/settings.json"); + WriteFile(sourceRoot, ".env"); + WriteFile(sourceRoot, "..settings"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/.aspire/settings.json"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/.env"); + WriteFile(sourceRoot, "aspire-go-starter/diagnostics/aspire-stop.log"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/apphost.go"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/package.json"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/node_modules/@babel/core/package.json"); + WriteFile(sourceRoot, "aspire-ts-starter/AspireCliTsStarterSmoke/node_modules/vite/package.json"); + WriteFile(destinationRoot, "starter-validation.tar"); + + var result = await RunScript(sourceRoot, destinationRoot); + + result.EnsureSuccessful(); + Assert.Equal( + [ + "aspire-go-starter/AspireCliGoStarterSmoke/apphost.go", + "aspire-go-starter/AspireCliGoStarterSmoke/frontend/package.json", + "aspire-go-starter/diagnostics/aspire-stop.log", + "cli-logs/cli.log" + ], + ReadArchiveFiles(destinationRoot, sourceRoot)); + Assert.Equal(["starter-validation.tar"], GetRelativeEntries(destinationRoot)); + } + + [Fact] + [RequiresTools(["pwsh", "tar"])] + public async Task ArchivesLinksWithoutFollowingTargets() + { + Assert.SkipWhen(OperatingSystem.IsWindows(), "Creating symbolic links requires extra privileges on Windows."); + + var sourceRoot = Path.Combine(_workspace.Path, "validation"); + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + var outsideDirectory = Path.Combine(_workspace.Path, "outside"); + WriteFile(sourceRoot, "aspire-ts-starter/diagnostics/aspire-stop.log"); + WriteFile(outsideDirectory, "secret.txt"); + + File.CreateSymbolicLink( + Path.Combine(sourceRoot, "aspire-ts-starter", "removed.log"), + Path.Combine(_workspace.Path, "missing.log")); + Directory.CreateSymbolicLink(Path.Combine(sourceRoot, "aspire-ts-starter", "linked"), outsideDirectory); + + var result = await RunScript(sourceRoot, destinationRoot); + + result.EnsureSuccessful(); + Assert.Equal(["aspire-ts-starter/diagnostics/aspire-stop.log"], ReadArchiveFiles(destinationRoot, sourceRoot)); + using var archive = File.OpenRead(Path.Combine(destinationRoot, "starter-validation.tar")); + using var reader = new TarReader(archive); + List links = []; + while (reader.GetNextEntry() is { } entry) + { + if (entry.EntryType is TarEntryType.SymbolicLink) + { + links.Add(entry.Name); + } + } + Assert.Equal(["validation/aspire-ts-starter/linked", "validation/aspire-ts-starter/removed.log"], links.Order(StringComparer.Ordinal)); + } + + [Fact] + [RequiresTools(["pwsh"])] + public async Task MissingSourceLeavesNothingToUpload() + { + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + WriteFile(destinationRoot, "starter-validation.tar"); + WriteFile(destinationRoot, "starter-validation.tar.partial"); + + var result = await RunScript(Path.Combine(_workspace.Path, "missing"), destinationRoot); + + result.EnsureSuccessful(); + Assert.Empty(Directory.EnumerateFileSystemEntries(destinationRoot)); + } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [RequiresTools(["pwsh"])] + public async Task ArchiveFailureLeavesNothingToUpload(int tarExitCode) + { + var sourceRoot = Path.Combine(_workspace.Path, "validation"); + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + WriteFile(sourceRoot, "diagnostics/aspire-stop.log"); + WriteFile(destinationRoot, "starter-validation.tar"); + + // A native command may leave partial output before failing. Exercise the production script + // with that failure without relying on permissions, disk exhaustion, or a timed filesystem race. + var wrapper = Path.Combine(_workspace.Path, "fail-tar.ps1"); + await File.WriteAllTextAsync(wrapper, """ + param($Script, $SourceRoot, $DestinationRoot, $TarExitCode) + function tar + { + [System.IO.File]::WriteAllText((Join-Path $DestinationRoot 'starter-validation.tar.partial'), 'incomplete') + $global:LASTEXITCODE = $TarExitCode + } + & $Script -SourceRoot $SourceRoot -DestinationRoot $DestinationRoot + """); + using var cmd = new PowerShellCommand(wrapper, _output).WithTimeout(TimeSpan.FromMinutes(2)); + var result = await cmd.ExecuteAsync( + "-Script", $"\"{_scriptPath}\"", + "-SourceRoot", $"\"{sourceRoot}\"", + "-DestinationRoot", $"\"{destinationRoot}\"", + "-TarExitCode", tarExitCode.ToString()); + + Assert.NotEqual(0, result.ExitCode); + Assert.Contains($"tar exit code {tarExitCode}", result.Output); + Assert.Empty(Directory.EnumerateFileSystemEntries(destinationRoot)); + } + + private async Task RunScript(string sourceRoot, string destinationRoot) + { + using var cmd = new PowerShellCommand(_scriptPath, _output) + .WithTimeout(TimeSpan.FromMinutes(2)); + + return await cmd.ExecuteAsync( + "-SourceRoot", $"\"{sourceRoot}\"", + "-DestinationRoot", $"\"{destinationRoot}\""); + } + + private static void WriteFile(string root, string relativePath) + { + var path = Path.Combine(root, relativePath); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, relativePath); + } + + private static string[] GetRelativeEntries(string root) => + Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories) + .Select(path => Path.GetRelativePath(root, path).Replace(Path.DirectorySeparatorChar, '/')) + .Order(StringComparer.Ordinal) + .ToArray(); + + private static string[] ReadArchiveFiles(string root, string sourceRoot) + { + using var archive = File.OpenRead(Path.Combine(root, "starter-validation.tar")); + using var reader = new TarReader(archive); + List files = []; + while (reader.GetNextEntry() is { } entry) + { + if (entry.EntryType is not (TarEntryType.RegularFile or TarEntryType.V7RegularFile)) + { + continue; + } + + // Entries include the validation directory, for example "validation/cli-logs/cli.log". + var prefix = Path.GetFileName(sourceRoot) + "/"; + Assert.StartsWith(prefix, entry.Name, StringComparison.Ordinal); + var name = entry.Name[prefix.Length..]; + using var content = new StreamReader(entry.DataStream!); + Assert.Equal(name, content.ReadToEnd()); + files.Add(name); + } + + return files.Order(StringComparer.Ordinal).ToArray(); + } +} diff --git a/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs b/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs index c6088e70b9c..80662d54e0f 100644 --- a/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs +++ b/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs @@ -357,6 +357,10 @@ private static IReadOnlySet LoadSolutionProjectPaths() "eng/scripts/cli-starter-validation.ps1", ["job:cli-starter-validation"] }, + { + "eng/scripts/stage-cli-starter-validation-diagnostics.ps1", + ["test:Infrastructure.Tests", "job:cli-starter-validation"] + }, { "eng/scripts/test-native-dashboard.ps1", ["job:native-dashboard-validation"]