8000 Support launchSettings.json workingDirectory with dotnet run · dotnet/sdk@b9c5846 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9c5846

Browse files
committed
Support launchSettings.json workingDirectory with dotnet run
1 parent a68681b commit b9c5846

File tree

10 files changed

+103
-0
lines changed

10 files changed

+103
-0
lines changed

src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class BuiltInCommand : ICommand
1919

2020
public string CommandName { get; }
2121
public string CommandArgs => string.Join(" ", _commandArgs);
22+
public string CommandWorkingDirectory => _workingDirectory;
2223

2324
public BuiltInCommand(string commandName, IEnumerable<string> commandArgs, Func<string[], int> builtInCommand)
2425
: this(commandName, commandArgs, builtInCommand, new BuiltInCommandEnvironment())

src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public ICommand OnErrorLine(Action<string> handler)
182182

183183
public string CommandArgs => _process.StartInfo.Arguments;
184184

185+
public string CommandWorkingDirectory => _process.StartInfo.WorkingDirectory;
186+
185187
public ICommand SetCommandArgs(string commandArgs)
186188
{
187189
_process.StartInfo.Arguments = commandArgs;

src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public interface ICommand
2828
string CommandName { get; }
2929

3030
string CommandArgs { get; }
31+
32+
string CommandWorkingDirectory { get; }
3133
}
3234
}

src/Cli/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ public class ProjectLaunchSettingsModel
1818
public string DotNetRunMessages { get; set; }
1919

2020
public Dictionary<string, string> EnvironmentVariables { get; } = new Dictionary<string, string>(StringComparer.Ordinal);
21+
22+
public string WorkingDirectory { get; set; }
2123
}
2224
}

src/Cli/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public LaunchSettingsApplyResult TryGetLaunchSettings(string? launchProfileName,
8282
}
8383
}
8484
}
85+
else if (string.Equals(property.Name, nameof(ProjectLaunchSettingsModel.WorkingDirectory), StringComparison.OrdinalIgnoreCase))
86+
{
87+
if (!TryGetStringValue(property.Value, out var workingDirectory))
88+
{
89+
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.CouldNotConvertToString, property.Name));
90+
}
91+
92+
config.WorkingDirectory = workingDirectory;
93+
}
8594
}
8695

8796
return new LaunchSettingsApplyResult(true, null, config);

src/Cli/dotnet/commands/dotnet-run/RunCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ private ICommand ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, Pro
109109
{
110110
targetCommand.SetCommandArgs(launchSettings.CommandLineArgs);
111111
}
112+
if (string.IsNullOrEmpty(targetCommand.CommandWorkingDirectory) && launchSettings.WorkingDirectory != null)
113+
{
114+
targetCommand.WorkingDirectory(launchSettings.WorkingDirectory);
115+
}
112116
}
113117
return targetCommand;
114118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />
3+
4+
<PropertyGroup>
5+
<OutputType>Exe</OutputType>
6+
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
7+
<RuntimeIdentifiers>$(LatestRuntimeIdentifiers)</RuntimeIdentifiers>
8+
</PropertyGroup>
9+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace MSBuildTestApp
7+
{
8+
public class Program
9+
{
10+
public static void Main(string[] args)
11+
{
12+
var message = Environment.CurrentDirectory;
13+
Console.WriteLine(message);
14+
}
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"First":{
4+
"commandName": "Project"
5+
},
6+
"Second":{
7+
"commandName": "Project",
8+
"workingDirectory": "launchSubfolder"
9+
}
10+
}
11+
}

test/dotnet-run.Tests/GivenDotnetRunBuildsCsProj.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,53 @@ public void ItUsesTheValueOfAppUrlIfTheEnvVarIsNotSet()
561561
cmd.StdErr.Should().BeEmpty();
562562
}
563563

564+
[Fact]
565+
public void ItUsesTheValueOfWorkingDirectoryIfTheProjectRunWorkingDirectoryIsNotSet()
566+
{
567+
var testAppName = "AppWithWorkingDirectoryInLaunchSettings";
568+
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
569+
.WithSource();
570+
571+
var testProjectDirectory = testInstance.Path;
572+
573+
Directory.CreateDirectory(Path.Combine(testProjectDirectory, "launchSubfolder"));
574+
575+
var cmd = new DotnetCommand(Log, "run")
576+
.WithWorkingDirectory(testProjectDirectory)
577+
.Execute("--launch-profile", "Second");
578+
579+
cmd.Should().Pass()
580+
.And.HaveStdOutContaining("launchSubfolder");
581+
582+
cmd.StdErr.Should().BeEmpty();
583+
}
584+
585+
[Fact]
586+
public void ItPrefersTheValueOfProjectRunWorkingDirectoryIfSet()
587+
{
588+
var testAppName = "AppWithWorkingDirectoryInLaunchSettings";
589+
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
590+
.WithSource()
591+
.WithProjectChanges(p => {
592+
var ns = p.Root.Name.Namespace;
593+
var propertyGroup = p.Root.Elements(ns + "PropertyGroup").First();
594+
propertyGroup.Add(new XElement(ns + "RunWorkingDirectory", "runSubfolder"));
595+
});
596+
597+
var testProjectDirectory = testInstance.Path;
598+
599+
Directory.CreateDirectory(Path.Combine(testProjectDirectory, "runSubfolder"));
600+
601+
var cmd = new DotnetCommand(Log, "run")
602+
.WithWorkingDirectory(testProjectDirectory)
603+
.Execute("--launch-profile", "Second");
604+
605+
cmd.Should().Pass()
606+
.And.HaveStdOutContaining("runSubfolder");
607+
608+
cmd.StdErr.Should().BeEmpty();
609+
}
610+
564611
[Fact]
565612
public void ItGivesAnErrorWhenTheLaunchProfileNotFound()
566613
{

0 commit comments

Comments
 (0)
0