-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix CS0006 errors when NuGet cache cleared during file-based app recompilation #51609
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
Open
Copilot
wants to merge
8
commits into
release/10.0.2xx
Choose a base branch
from
copilot/fix-nuget-cache-recompile-issue
base: release/10.0.2xx
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−9
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fb103fc
Initial plan
Copilot 6df7445
Add CS0006 error detection and fallback to MSBuild
Copilot 29a87ea
Add preemptive check for NuGet cache files when reusing CSC arguments
Copilot c70b64d
Address code review comments
Copilot 6dd39de
Clarify locale-independent error code detection
Copilot 30ca406
Address PR review comments
Copilot 7fbe9ca
Simplify test to avoid restore recreating deleted files
Copilot 7ff3523
Add proper tests
jjonescz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3128,7 +3128,13 @@ Release config | |
| Build(testInstance, BuildLevel.Csc); | ||
| } | ||
|
|
||
| private void Build(TestDirectory testInstance, BuildLevel expectedLevel, ReadOnlySpan<string> args = default, string expectedOutput = "Hello from Program", string programFileName = "Program.cs") | ||
| private void Build( | ||
| TestDirectory testInstance, | ||
| BuildLevel expectedLevel, | ||
| ReadOnlySpan<string> args = default, | ||
| string expectedOutput = "Hello from Program", | ||
| string programFileName = "Program.cs", | ||
| Func<TestCommand, TestCommand>? customizeCommand = null) | ||
| { | ||
| string prefix = expectedLevel switch | ||
| { | ||
|
|
@@ -3138,9 +3144,15 @@ private void Build(TestDirectory testInstance, BuildLevel expectedLevel, ReadOnl | |
| _ => throw new ArgumentOutOfRangeException(paramName: nameof(expectedLevel)), | ||
| }; | ||
|
|
||
| new DotnetCommand(Log, ["run", programFileName, "-bl", .. args]) | ||
| .WithWorkingDirectory(testInstance.Path) | ||
| .Execute() | ||
| var command = new DotnetCommand(Log, ["run", programFileName, "-bl", .. args]) | ||
| .WithWorkingDirectory(testInstance.Path); | ||
|
|
||
| if (customizeCommand != null) | ||
| { | ||
| command = customizeCommand(command); | ||
| } | ||
|
|
||
| command.Execute() | ||
| .Should().Pass() | ||
| .And.HaveStdOut(prefix + expectedOutput); | ||
|
|
||
|
|
@@ -3787,6 +3799,102 @@ public void CscOnly_AfterMSBuild_AuxiliaryFilesNotReused() | |
| Build(testInstance, BuildLevel.Csc, expectedOutput: "v3 "); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Testing <see cref="CscOnly"/> optimization when the NuGet cache is cleared between builds. | ||
| /// See <see href="https://github.com/dotnet/sdk/issues/45169"/>. | ||
| /// </summary> | ||
| [Fact] | ||
| public void CscOnly_NuGetCacheCleared() | ||
| { | ||
| var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory); | ||
|
|
||
| var code = """ | ||
| Console.Write("v1"); | ||
| """; | ||
|
|
||
| var programPath = Path.Join(testInstance.Path, "Program.cs"); | ||
| File.WriteAllText(programPath, code); | ||
|
|
||
| var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programPath); | ||
| if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
|
||
| var packageDir = Path.Join(testInstance.Path, "packages"); | ||
| TestCommand CustomizeCommand(TestCommand command) => command.WithEnvironmentVariable("NUGET_PACKAGES", packageDir); | ||
|
|
||
| Assert.False(Directory.Exists(packageDir)); | ||
|
|
||
| // Ensure the packages exist first. | ||
| Build(testInstance, BuildLevel.All, expectedOutput: "v1", customizeCommand: CustomizeCommand); | ||
|
|
||
| Assert.True(Directory.Exists(packageDir)); | ||
|
|
||
| // Now clear the build outputs (but not packages) to verify CSC is used even from "first run". | ||
| if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
|
||
| code = code.Replace("v1", "v2"); | ||
| File.WriteAllText(programPath, code); | ||
|
|
||
| Build(testInstance, BuildLevel.Csc, expectedOutput: "v2", customizeCommand: CustomizeCommand); | ||
|
|
||
| code = code.Replace("v2", "v3"); | ||
| File.WriteAllText(programPath, code); | ||
|
|
||
| // Clear NuGet cache. | ||
| Directory.Delete(packageDir, recursive: true); | ||
| Assert.False(Directory.Exists(packageDir)); | ||
|
|
||
| Build(testInstance, BuildLevel.All, expectedOutput: "v3", customizeCommand: CustomizeCommand); | ||
|
|
||
| Assert.True(Directory.Exists(packageDir)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Combination of <see cref="CscOnly_NuGetCacheCleared"/> and <see cref="CscOnly_AfterMSBuild"/>. | ||
| /// </summary> | ||
| [Fact] | ||
| public void CscOnly_AfterMSBuild_NuGetCacheCleared() | ||
| { | ||
| var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory); | ||
|
|
||
| var code = """ | ||
| #:property PublishAot=false | ||
| #:package [email protected] | ||
| new System.CommandLine.RootCommand("v1"); | ||
| Console.WriteLine("v1"); | ||
| """; | ||
|
|
||
| var programPath = Path.Join(testInstance.Path, "Program.cs"); | ||
| File.WriteAllText(programPath, code); | ||
|
|
||
| var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programPath); | ||
| if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
|
||
| var packageDir = Path.Join(testInstance.Path, "packages"); | ||
| TestCommand CustomizeCommand(TestCommand command) => command.WithEnvironmentVariable("NUGET_PACKAGES", packageDir); | ||
|
|
||
| Assert.False(Directory.Exists(packageDir)); | ||
|
|
||
| Build(testInstance, BuildLevel.All, expectedOutput: "v1", customizeCommand: CustomizeCommand); | ||
|
|
||
| Assert.True(Directory.Exists(packageDir)); | ||
|
|
||
| code = code.Replace("v1", "v2"); | ||
| File.WriteAllText(programPath, code); | ||
|
|
||
| Build(testInstance, BuildLevel.Csc, expectedOutput: "v2", customizeCommand: CustomizeCommand); | ||
|
|
||
| code = code.Replace("v2", "v3"); | ||
| File.WriteAllText(programPath, code); | ||
|
|
||
| // Clear NuGet cache. | ||
| Directory.Delete(packageDir, recursive: true); | ||
| Assert.False(Directory.Exists(packageDir)); | ||
|
|
||
| Build(testInstance, BuildLevel.All, expectedOutput: "v3", customizeCommand: CustomizeCommand); | ||
|
|
||
| Assert.True(Directory.Exists(packageDir)); | ||
| } | ||
|
|
||
| private static string ToJson(string s) => JsonSerializer.Serialize(s); | ||
|
|
||
| /// <summary> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.