Skip to content

Commit 7fc65b1

Browse files
authored
Allow build --no-restore for file-based programs (#49013)
2 parents 00768c6 + 76956e3 commit 7fc65b1

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/Cli/dotnet/Commands/Run/RunCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ public int Execute()
110110
}
111111
else
112112
{
113-
if (EntryPointFileFullPath is not null)
113+
if (NoCache)
114114
{
115-
projectFactory = CreateVirtualCommand().PrepareProjectInstance().CreateProjectInstance;
115+
throw new GracefulException(CliCommandStrings.InvalidOptionCombination, RunCommandParser.NoCacheOption.Name, RunCommandParser.NoBuildOption.Name);
116116
}
117117

118-
if (NoCache)
118+
if (EntryPointFileFullPath is not null)
119119
{
120-
throw new GracefulException(CliCommandStrings.InvalidOptionCombination, RunCommandParser.NoCacheOption.Name, RunCommandParser.NoBuildOption.Name);
120+
projectFactory = CreateVirtualCommand().PrepareProjectInstance().CreateProjectInstance;
121121
}
122122
}
123123

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ public override int Execute()
9797
{
9898
if (NoCache)
9999
{
100-
if (NoRestore)
101-
{
102-
throw new GracefulException(CliCommandStrings.InvalidOptionCombination, RunCommandParser.NoCacheOption.Name, RunCommandParser.NoRestoreOption.Name);
103-
}
104-
105100
cacheEntry = ComputeCacheEntry(out _);
106101
}
107102
else if (!NeedsToBuild(out cacheEntry))

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ Resource not found
719719
}
720720

721721
[Fact]
722-
public void NoRestore()
722+
public void NoRestore_01()
723723
{
724724
var testInstance = _testAssetsManager.CreateTestDirectory();
725725
var programFile = Path.Join(testInstance.Path, "Program.cs");
@@ -750,6 +750,43 @@ public void NoRestore()
750750
.And.HaveStdOut("Hello from Program");
751751
}
752752

753+
[Fact]
754+
public void NoRestore_02()
755+
{
756+
var testInstance = _testAssetsManager.CreateTestDirectory();
757+
var programFile = Path.Join(testInstance.Path, "Program.cs");
758+
File.WriteAllText(programFile, s_program);
759+
760+
// Remove artifacts from possible previous runs of this test.
761+
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile);
762+
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true);
763+
764+
// It is an error when never restored before.
765+
new DotnetCommand(Log, "build", "--no-restore", "Program.cs")
766+
.WithWorkingDirectory(testInstance.Path)
767+
.Execute()
768+
.Should().Fail()
769+
.And.HaveStdOutContaining("NETSDK1004"); // error NETSDK1004: Assets file '...\obj\project.assets.json' not found. Run a NuGet package restore to generate this file.
770+
771+
// Run restore.
772+
new DotnetCommand(Log, "restore", "Program.cs")
773+
.WithWorkingDirectory(testInstance.Path)
774+
.Execute()
775+
.Should().Pass();
776+
777+
// --no-restore works.
778+
new DotnetCommand(Log, "build", "--no-restore", "Program.cs")
779+
.WithWorkingDirectory(testInstance.Path)
780+
.Execute()
781+
.Should().Pass();
782+
783+
new DotnetCommand(Log, "run", "--no-build", "Program.cs")
784+
.WithWorkingDirectory(testInstance.Path)
785+
.Execute()
786+
.Should().Pass()
787+
.And.HaveStdOut("Hello from Program");
788+
}
789+
753790
[Fact]
754791
public void NoBuild_01()
755792
{
@@ -1168,12 +1205,6 @@ public void UpToDate_InvalidOptions()
11681205
.Execute()
11691206
.Should().Fail()
11701207
.And.HaveStdErrContaining(string.Format(CliCommandStrings.InvalidOptionCombination, RunCommandParser.NoCacheOption.Name, RunCommandParser.NoBuildOption.Name));
1171-
1172-
new DotnetCommand(Log, "run", "Program.cs", "--no-cache", "--no-restore")
1173-
.WithWorkingDirectory(testInstance.Path)
1174-
.Execute()
1175-
.Should().Fail()
1176-
.And.HaveStdErrContaining(string.Format(CliCommandStrings.InvalidOptionCombination, RunCommandParser.NoCacheOption.Name, RunCommandParser.NoRestoreOption.Name));
11771208
}
11781209

11791210
private static string ToJson(string s) => JsonSerializer.Serialize(s);

0 commit comments

Comments
 (0)