Skip to content

Commit 65384c6

Browse files
authored
Fix dotnet run to accept Windows path separators on Linux (#50889)
2 parents 465132c + 13eafe0 commit 65384c6

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,15 @@ internal static void ThrowUnableToRunError(ProjectInstance project)
534534
projectFileOrDirectoryPath = Directory.GetCurrentDirectory();
535535
}
536536

537+
// Normalize path separators to handle Windows-style paths on non-Windows platforms.
538+
// This is supported for backward compatibility in 'dotnet run' only, not for all CLI commands.
539+
// Converting backslashes to forward slashes allows PowerShell scripts using Windows-style paths
540+
// to work cross-platform, maintaining compatibility with .NET 9 behavior.
541+
if (Path.DirectorySeparatorChar != '\\')
542+
{
543+
projectFileOrDirectoryPath = projectFileOrDirectoryPath.Replace('\\', '/');
544+
}
545+
537546
string? projectFilePath = Directory.Exists(projectFileOrDirectoryPath)
538547
? TryFindSingleProjectInDirectory(projectFileOrDirectoryPath)
539548
: projectFileOrDirectoryPath;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,33 @@ 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+
// Should not throw on Windows
41+
var runCommand = RunCommand.FromArgs(new[] { "--project", projectPath });
42+
runCommand.ProjectFileFullPath.Should().NotBeNull();
43+
}
44+
45+
[UnixOnlyFact]
46+
public void RunParserAcceptsWindowsPathSeparatorsOnLinux()
47+
{
48+
var tam = new TestAssetsManager(output);
49+
var testAsset = tam.CopyTestAsset("HelloWorld").WithSource();
50+
var newWorkingDir = testAsset.Path;
51+
52+
Directory.SetCurrentDirectory(newWorkingDir);
53+
var projectPath = @".\HelloWorld.csproj";
54+
// Should not throw on Linux with backslash separators
55+
var runCommand = RunCommand.FromArgs(new[] { "--project", projectPath });
56+
runCommand.ProjectFileFullPath.Should().NotBeNull();
57+
}
3058
}
3159
}

0 commit comments

Comments
 (0)