Skip to content

Commit 88d3439

Browse files
authored
Fix --no-build flag error message inconsistency for non-existent project files (#49544)
2 parents 145f3c3 + dba3398 commit 88d3439

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ private static void ThrowUnableToRunError(ProjectInstance project)
522522
? TryFindSingleProjectInDirectory(projectFileOrDirectoryPath)
523523
: projectFileOrDirectoryPath;
524524

525+
// Check if the project file actually exists when it's specified as a direct file path
526+
if (projectFilePath is not null && !emptyProjectOption && !File.Exists(projectFilePath))
527+
{
528+
throw new GracefulException(CliCommandStrings.CmdNonExistentFileErrorDescription, projectFilePath);
529+
}
530+
525531
// If no project exists in the directory and no --project was given,
526532
// try to resolve an entry-point file instead.
527533
entryPointFilePath = projectFilePath is null && emptyProjectOption

test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetRunInvocation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public GivenDotnetRunInvocation(ITestOutputHelper log)
2626
[InlineData(new string[] { "-p", "prop1=true" }, new string[] { "--property:prop1=true" })]
2727
[InlineData(new string[] { "-p", "prop1=true", "-p", "prop2=false" }, new string[] { "--property:prop1=true", "--property:prop2=false" })]
2828
[InlineData(new string[] { "-p:prop1=true;prop2=false" }, new string[] { "--property:prop1=true", "--property:prop2=false" })]
29-
[InlineData(new string[] { "-p", "MyProject.csproj", "-p:prop1=true" }, new string[] { "--property:prop1=true" })]
29+
[InlineData(new string[] { "-p", "HelloWorld.csproj", "-p:prop1=true" }, new string[] { "--property:prop1=true" })]
3030
[InlineData(new string[] { "--disable-build-servers" }, new string[] { "--property:UseRazorBuildServer=false", "--property:UseSharedCompilation=false", "/nodeReuse:false" })]
3131
public void MsbuildInvocationIsCorrect(string[] args, string[] expectedArgs)
3232
{

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,5 +1002,31 @@ public void EnvOptionNotAppliedToBuild()
10021002
.And
10031003
.HaveStdOutContaining("env: Configuration=XYZ");
10041004
}
1005+
1006+
[Fact]
1007+
public void ItProvidesConsistentErrorMessageWhenProjectFileDoesNotExistWithNoBuild()
1008+
{
1009+
var tempDir = _testAssetsManager.CreateTestDirectory();
1010+
var nonExistentProject = Path.Combine(tempDir.Path, "nonexistent.csproj");
1011+
1012+
var result = new DotnetCommand(Log, "run")
1013+
.WithWorkingDirectory(tempDir.Path)
1014+
.Execute("--project", nonExistentProject, "--no-build");
1015+
1016+
result.Should().Fail();
1017+
if (!TestContext.IsLocalized())
1018+
{
1019+
// After the fix, we should get a clear error message about the file not existing
1020+
var stderr = result.StdErr;
1021+
1022+
// Should provide a clear error message about the project file not existing
1023+
var hasExpectedErrorMessage = stderr.Contains("does not exist") ||
1024+
stderr.Contains("not found") ||
1025+
stderr.Contains("cannot find") ||
1026+
stderr.Contains("could not find");
1027+
1028+
hasExpectedErrorMessage.Should().BeTrue($"Expected error message to clearly indicate file doesn't exist, but got: {stderr}");
1029+
}
1030+
}
10051031
}
10061032
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ public RunParserTests(ITestOutputHelper output)
1717
[Fact]
1818
public void RunParserCanGetArgumentFromDoubleDash()
1919
{
20-
var runCommand = RunCommand.FromArgs(new[] { "--project", "foo.csproj", "--", "foo" });
20+
var tam = new TestAssetsManager(output);
21+
var testAsset = tam.CopyTestAsset("HelloWorld").WithSource();
22+
var newWorkingDir = testAsset.Path;
23+
24+
Directory.SetCurrentDirectory(newWorkingDir);
25+
var projectPath = Path.Combine(newWorkingDir, "HelloWorld.csproj");
26+
27+
var runCommand = RunCommand.FromArgs(new[] { "--project", projectPath, "--", "foo" });
2128
runCommand.ApplicationArgs.Single().Should().Be("foo");
2229
}
2330
}

0 commit comments

Comments
 (0)