Skip to content

Commit 9d6caae

Browse files
authored
Show specific error message for dotnet <app_path> where app_path is a non-dll/exe file that exists (#116940)
Make running `dotnet <path>` where path exists but is not a dll/exe show "The application '<path>' does not exist or is not a managed .dll" and avoid invoking the SDK.
1 parent 8429820 commit 9d6caae

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/installer/tests/HostActivation.Tests/DotnetArgValidation.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,41 @@ public void MissingArgumentValue_Fails()
6868
.And.HaveStdErrContaining($"Failed to parse supported options or their values:");
6969
}
7070

71+
[Fact]
72+
public void File_ExistsNoDirectorySeparator_RoutesToSDK()
73+
{
74+
// Create a file named "build" in the current directory to simulate a file that happens to match the name of a command
75+
string buildFile = Path.Combine(sharedTestState.BaseDirectory.Location, "build");
76+
File.WriteAllText(buildFile, string.Empty);
77+
78+
// Test that "dotnet build" still routes to SDK, not to the file
79+
TestContext.BuiltDotNet.Exec("build")
80+
.WorkingDirectory(sharedTestState.BaseDirectory.Location)
81+
.EnableTracingAndCaptureOutputs()
82+
.Execute()
83+
.Should().Fail()
84+
.And.HaveStdErrContaining("The command could not be loaded, possibly because:") // This is a generic error for when we can't tell what exactly the user intended to do
85+
.And.HaveStdErrContaining("Resolving SDKs");
86+
}
87+
88+
[Fact]
89+
public void RelativePathToNonManagedFile_ShowsSpecificError()
90+
{
91+
// Test relative path with directory separator
92+
Directory.CreateDirectory(Path.Combine(sharedTestState.BaseDirectory.Location, "subdir"));
93+
string testFile = Path.Combine(sharedTestState.BaseDirectory.Location, "subdir", "test.json");
94+
File.WriteAllText(testFile, "{}");
95+
96+
string relativePath = Path.GetRelativePath(sharedTestState.BaseDirectory.Location, testFile);
97+
TestContext.BuiltDotNet.Exec(relativePath)
98+
.WorkingDirectory(sharedTestState.BaseDirectory.Location)
99+
.CaptureStdOut()
100+
.CaptureStdErr()
101+
.Execute()
102+
.Should().Fail()
103+
.And.HaveStdErrContaining($"The application '{relativePath}' is not a managed .dll.");
104+
}
105+
71106
[Fact]
72107
public void InvalidFileOrCommand_NoSDK_ListsPossibleIssues()
73108
{

src/installer/tests/HostActivation.Tests/FrameworkDependentAppLaunch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void Muxer_AssemblyWithDifferentFileExtension_Fails()
5858
.CaptureStdErr()
5959
.Execute()
6060
.Should().Fail()
61-
.And.HaveStdErrContaining($"The application '{appOtherExt}' does not exist or is not a managed .dll or .exe");
61+
.And.HaveStdErrContaining($"The application '{appOtherExt}' is not a managed .dll.");
6262
}
6363

6464
[Fact]

src/native/corehost/fxr/command_line.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ namespace
164164
trace::verbose(_X("Application '%s' is not a managed executable."), app_candidate.c_str());
165165
if (!exec_mode)
166166
{
167+
// Check if this is a non-managed file with directory separator that exists
168+
// This should show a specific error instead of routing to CLI
169+
bool has_dir_separator = app_candidate.find(DIR_SEPARATOR) != pal::string_t::npos;
170+
if (has_dir_separator)
171+
{
172+
if (pal::file_exists(app_candidate))
173+
{
174+
trace::error(_X("The application '%s' is not a managed .dll."), app_candidate.c_str());
175+
return StatusCode::InvalidArgFailure;
176+
}
177+
}
167178
// Route to CLI.
168179
return StatusCode::AppArgNotRunnable;
169180
}

0 commit comments

Comments
 (0)