Skip to content

Commit de25601

Browse files
CopilotmarcpopMSFT
andcommitted
Fix --no-build flag error message inconsistency for non-existent project files
Co-authored-by: marcpopMSFT <[email protected]>
1 parent 5028977 commit de25601

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,12 @@ private static void ThrowUnableToRunError(ProjectInstance project)
459459
? TryFindSingleProjectInDirectory(projectFileOrDirectoryPath)
460460
: projectFileOrDirectoryPath;
461461

462+
// Check if the project file actually exists when it's specified as a direct file path
463+
if (projectFilePath is not null && !emptyProjectOption && !File.Exists(projectFilePath))
464+
{
465+
throw new GracefulException(CliCommandStrings.CmdNonExistentFileErrorDescription, projectFilePath);
466+
}
467+
462468
// If no project exists in the directory and no --project was given,
463469
// try to resolve an entry-point file instead.
464470
entryPointFilePath = projectFilePath is null && emptyProjectOption

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,5 +987,33 @@ public void EnvOptionNotAppliedToBuild()
987987
.And
988988
.HaveStdOutContaining("env: Configuration=XYZ");
989989
}
990+
991+
[Fact]
992+
public void ItProvidesConsistentErrorMessageWhenProjectFileDoesNotExistWithNoBuild()
993+
{
994+
var tempDir = _testAssetsManager.CreateTestDirectory();
995+
var nonExistentProject = Path.Combine(tempDir.Path, "nonexistent.csproj");
996+
997+
var result = new DotnetCommand(Log, "run")
998+
.WithWorkingDirectory(tempDir.Path)
999+
.Execute("--project", nonExistentProject, "--no-build");
1000+
1001+
result.Should().Fail();
1002+
if (!TestContext.IsLocalized())
1003+
{
1004+
// After the fix, we should get a clear error message about the file not existing
1005+
var stderr = result.StdErr;
1006+
1007+
Log.WriteLine($"Actual stderr: '{stderr}'");
1008+
1009+
// Should provide a clear error message about the project file not existing
1010+
var hasExpectedErrorMessage = stderr.Contains("does not exist") ||
1011+
stderr.Contains("not found") ||
1012+
stderr.Contains("cannot find") ||
1013+
stderr.Contains("could not find");
1014+
1015+
hasExpectedErrorMessage.Should().BeTrue($"Expected error message to clearly indicate file doesn't exist, but got: {stderr}");
1016+
}
1017+
}
9901018
}
9911019
}

0 commit comments

Comments
 (0)