Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -470,8 +470,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like removing the WorkingDirectory call just causes the ordinary logic for deciding the working directory of a command to take over? Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, basically it remains null which means "current directory". In the case of the simple CSC-only optimized build path, there is no way to override that, which is fine. It can only be overridden via MSBuild property but that wouldn't hit the optimized CSC-only path.


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 @@ -856,6 +856,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 = 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 = 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 @@ -3214,7 +3332,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 @@ -3225,12 +3343,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
Loading