Skip to content

Commit 8eb2ee5

Browse files
CopilotmarcpopMSFT
andcommitted
Fix Windows path separator handling in dotnet run on Linux
Co-authored-by: marcpopMSFT <[email protected]>
1 parent ee0b514 commit 8eb2ee5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,13 @@ internal static void ThrowUnableToRunError(ProjectInstance project)
529529
projectFileOrDirectoryPath = Directory.GetCurrentDirectory();
530530
}
531531

532+
// Normalize path separators to handle Windows-style paths on non-Windows platforms
533+
// First convert backslashes to forward slashes on non-Windows, then get the full path
534+
if (Path.DirectorySeparatorChar != '\\')
535+
{
536+
projectFileOrDirectoryPath = projectFileOrDirectoryPath.Replace('\\', '/');
537+
}
538+
532539
string? projectFilePath = Directory.Exists(projectFileOrDirectoryPath)
533540
? TryFindSingleProjectInDirectory(projectFileOrDirectoryPath)
534541
: projectFileOrDirectoryPath;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,35 @@ public void RunParserCanGetArgumentFromDoubleDash()
2727
var runCommand = RunCommand.FromArgs(new[] { "--project", projectPath, "--", "foo" });
2828
runCommand.ApplicationArgs.Single().Should().Be("foo");
2929
}
30+
31+
[WindowsOnlyFact]
32+
public void RunParserAcceptsWindowsPathSeparatorsOnWindows()
33+
{
34+
var tam = new TestAssetsManager(output);
35+
var testAsset = tam.CopyTestAsset("HelloWorld").WithSource();
36+
var newWorkingDir = testAsset.Path;
37+
38+
Directory.SetCurrentDirectory(newWorkingDir);
39+
var projectPath = @".\HelloWorld.csproj";
40+
41+
// Should not throw on Windows
42+
var runCommand = RunCommand.FromArgs(new[] { "--project", projectPath });
43+
runCommand.ProjectFileFullPath.Should().NotBeNull();
44+
}
45+
46+
[UnixOnlyFact]
47+
public void RunParserAcceptsWindowsPathSeparatorsOnLinux()
48+
{
49+
var tam = new TestAssetsManager(output);
50+
var testAsset = tam.CopyTestAsset("HelloWorld").WithSource();
51+
var newWorkingDir = testAsset.Path;
52+
53+
Directory.SetCurrentDirectory(newWorkingDir);
54+
var projectPath = @".\HelloWorld.csproj";
55+
56+
// Should not throw on Linux with backslash separators
57+
var runCommand = RunCommand.FromArgs(new[] { "--project", projectPath });
58+
runCommand.ProjectFileFullPath.Should().NotBeNull();
59+
}
3060
}
3161
}

0 commit comments

Comments
 (0)