8000 Support launchSettings.json workingDirectory with dotnet run by skybaks · Pull Request #42534 · dotnet/sdk · GitHub
[go: up one dir, main page]

Skip to content

Support launchSettings.json workingDirectory with dotnet run #42534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class BuiltInCommand : ICommand

public string CommandName { get; }
public string CommandArgs => string.Join(" ", _commandArgs);
public string CommandWorkingDirectory => _workingDirectory;

public BuiltInCommand(string commandName, IEnumerable<string> commandArgs, Func<string[], int> builtInCommand)
: this(commandName, commandArgs, builtInCommand, new BuiltInCommandEnvironment())
Expand Down
2 changes: 2 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public ICommand OnErrorLine(Action<string> handler)

public string CommandArgs => _process.StartInfo.Arguments;

public string CommandWorkingDirectory => _process.StartInfo.WorkingDirectory;

public ICommand SetCommandArgs(string commandArgs)
{
_process.StartInfo.Arguments = commandArgs;
Expand Down
2 changes: 2 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ public interface ICommand
string CommandName { get; }

string CommandArgs { get; }

string CommandWorkingDirectory { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class ProjectLaunchSettingsModel
public string DotNetRunMessages { get; set; }

public Dictionary<string, string> EnvironmentVariables { get; } = new Dictionary<string, string>(StringComparer.Ordinal);

public string WorkingDirectory { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public LaunchSettingsApplyResult TryGetLaunchSettings(string? launchProfileName,
}
}
}
else if (string.Equals(property.Name, nameof(ProjectLaunchSettingsModel.WorkingDirectory), StringComparison.OrdinalIgnoreCase))
{
if (!TryGetStringValue(property.Value, out var workingDirectory))
{
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.CouldNotConvertToString, property.Name));
}

config.WorkingDirectory = workingDirectory;
}
}

return new LaunchSettingsApplyResult(true, null, config);
Expand Down
4 changes: 4 additions & 0 deletions src/Cli/dotnet/commands/dotnet-run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ private ICommand ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, Pro
{
targetCommand.SetCommandArgs(launchSettings.CommandLineArgs);
}
if (launchSettings.WorkingDirectory != null)
{
targetCommand.WorkingDirectory(launchSettings.WorkingDirectory);
}
}
return targetCommand;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<RuntimeIdentifiers>$(LatestRuntimeIdentifiers)</RuntimeIdentifiers>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace MSBuildTestApp
{
public class Program
{
public static void Main(string[] args)
{
var message = Environment.CurrentDirectory;
Console.WriteLine(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"First":{
"commandName": "Project"
},
"Second":{
"commandName": "Project",
"workingDirectory": "launchSubfolder"
}
}
}
47 changes: 47 additions & 0 deletions test/dotnet-run.Tests/GivenDotnetRunBuildsCsProj.cs
713A
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,53 @@ public void ItUsesTheValueOfAppUrlIfTheEnvVarIsNotSet()
cmd.StdErr.Should().BeEmpty();
}

[Fact]
public void ItUsesTheValueOfWorkingDirectoryIfSet()
{
var testAppName = "AppWithWorkingDirectoryInLaunchSettings";
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
.WithSource();

var testProjectDirectory = testInstance.Path;

Directory.CreateDirectory(Path.Combine(testProjectDirectory, "launchSubfolder"));

var cmd = new DotnetCommand(Log, "run")
.WithWorkingDirectory(testProjectDirectory)
.Execute("--launch-profile", "Second");

cmd.Should().Pass()
.And.HaveStdOutContaining("launchSubfolder");

cmd.StdErr.Should().BeEmpty();
}

[Fact]
public void ItPrefersTheValueOfWorkingDirectoryFromLaunchSettingsOverProjectRunWorkingDirectory()
{
var testAppName = "AppWithWorkingDirectoryInLaunchSettings";
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
.WithSource()
.WithProjectChanges(p => {
var ns = p.Root.Name.Namespace;
var propertyGroup = p.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "RunWorkingDirectory", "expectThisSubfolderIsOverridden"));
});

var testProjectDirectory = testInstance.Path;

Directory.CreateDirectory(Path.Combine(testProjectDirectory, "launchSubfolder"));

var cmd = new DotnetCommand(Log, "run")
.WithWorkingDirectory(testProjectDirectory)
.Execute("--launch-profile", "Second");

cmd.Should().Pass()
.And.HaveStdOutContaining("launchSubfolder");

cmd.StdErr.Should().BeEmpty();
}

[Fact]
public void ItGivesAnErrorWhenTheLaunchProfileNotFound()
{
Expand Down
0