Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Cli/dotnet/Commands/Run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ static ICommand CreateCommandForCscBuiltProgram(string entryPointFileFullPath, s
var artifactsPath = VirtualProjectBuildingCommand.GetArtifactsPath(entryPointFileFullPath);
var exePath = Path.Join(artifactsPath, "bin", "debug", Path.GetFileNameWithoutExtension(entryPointFileFullPath) + FileNameSuffixes.CurrentPlatform.Exe);
var commandSpec = new CommandSpec(path: exePath, args: ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args));
var command = CommandFactoryUsingResolver.Create(commandSpec)
.WorkingDirectory(Path.GetDirectoryName(entryPointFileFullPath));
var command = CommandFactoryUsingResolver.Create(commandSpec);

SetRootVariableName(
command,
Expand Down
124 changes: 121 additions & 3 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,124 @@ public void EmptyFile()
.And.HaveStdOutContaining("error CS5001:"); // Program does not contain a static 'Main' method suitable for an entry point
}

/// <summary>
/// See <see href="https://github.com/dotnet/sdk/issues/51778"/>.
/// </summary>
[Theory, CombinatorialData]
public void WorkingDirectory(bool cscOnly)
{
var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: cscOnly ? OutOfTreeBaseDirectory : null);
var programPath = Path.Join(testInstance.Path, "Program.cs");

var code = """
Console.WriteLine("v1");
Console.WriteLine(Environment.CurrentDirectory);
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine(new DirectoryInfo(".").FullName);
Console.WriteLine(AppContext.GetData("EntryPointFileDirectoryPath"));
""";

File.WriteAllText(programPath, code);

var workDir = TestPathUtility.ResolveTempPrefixLink(Path.GetTempPath()).TrimEnd(Path.DirectorySeparatorChar);

var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programPath);
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true);

Build(testInstance,
expectedLevel: cscOnly ? BuildLevel.Csc : BuildLevel.All,
programFileName: programPath,
workDir: workDir,
expectedOutput: GetExpectedOutput("v1", workDir));

code = code.Replace("v1", "v2");
File.WriteAllText(programPath, code);

Build(testInstance,
expectedLevel: BuildLevel.Csc,
programFileName: programPath,
workDir: workDir,
expectedOutput: GetExpectedOutput("v2", workDir));

string GetExpectedOutput(string version, string workDir) => $"""
{version}
{workDir}
{workDir}
{workDir}
{Path.GetDirectoryName(programPath)}
""";
}

/// <summary>
/// Combination of <see cref="WorkingDirectory"/> and <see cref="CscOnly_AfterMSBuild"/>.
/// </summary>
[Fact]
public void WorkingDirectory_CscOnly_AfterMSBuild()
{
var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory);
var programPath = Path.Join(testInstance.Path, "Program.cs");

var code = """
#:property Configuration=Release
Console.WriteLine("v1");
Console.WriteLine(Environment.CurrentDirectory);
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine(new DirectoryInfo(".").FullName);
Console.WriteLine(AppContext.GetData("EntryPointFileDirectoryPath"));
""";

File.WriteAllText(programPath, code);

var workDir = TestPathUtility.ResolveTempPrefixLink(Path.GetTempPath()).TrimEnd(Path.DirectorySeparatorChar);

var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programPath);
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true);

Build(testInstance,
expectedLevel: BuildLevel.All,
programFileName: programPath,
workDir: workDir,
expectedOutput: GetExpectedOutput("v1", workDir));

Build(testInstance,
expectedLevel: BuildLevel.None,
programFileName: programPath,
workDir: workDir,
expectedOutput: GetExpectedOutput("v1", workDir));

code = code.Replace("v1", "v2");
File.WriteAllText(programPath, code);

Build(testInstance,
expectedLevel: BuildLevel.Csc,
programFileName: programPath,
workDir: workDir,
expectedOutput: GetExpectedOutput("v2", workDir));

// Can be overridden with a #:property.
var workDir2 = Path.Join(testInstance.Path, "dir2");
Directory.CreateDirectory(workDir2);
code = $"""
#:property RunWorkingDirectory={workDir2}
{code}
""";
File.WriteAllText(programPath, code);

Build(testInstance,
expectedLevel: BuildLevel.All,
programFileName: programPath,
workDir: workDir,
expectedOutput: GetExpectedOutput("v2", workDir2));

string GetExpectedOutput(string version, string workDir) => $"""
{version}
{workDir}
{workDir}
{workDir}
{Path.GetDirectoryName(programPath)}
""";
}

/// <summary>
/// Implicit build files have an effect.
/// </summary>
Expand Down Expand Up @@ -3238,7 +3356,7 @@ Release config
Build(testInstance, BuildLevel.Csc);
}

private void Build(TestDirectory testInstance, BuildLevel expectedLevel, ReadOnlySpan<string> args = default, string expectedOutput = "Hello from Program", string programFileName = "Program.cs")
private void Build(TestDirectory testInstance, BuildLevel expectedLevel, ReadOnlySpan<string> args = default, string expectedOutput = "Hello from Program", string programFileName = "Program.cs", string? workDir = null)
{
string prefix = expectedLevel switch
{
Expand All @@ -3249,12 +3367,12 @@ private void Build(TestDirectory testInstance, BuildLevel expectedLevel, ReadOnl
};

new DotnetCommand(Log, ["run", programFileName, "-bl", .. args])
.WithWorkingDirectory(testInstance.Path)
.WithWorkingDirectory(workDir ?? testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut(prefix + expectedOutput);

var binlogs = new DirectoryInfo(testInstance.Path)
var binlogs = new DirectoryInfo(workDir ?? testInstance.Path)
.EnumerateFiles("*.binlog", SearchOption.TopDirectoryOnly);

binlogs.Select(f => f.Name)
Expand Down