dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.75k stars 1.07k forks source link

Add a RunEnvironmentVariables property to the sdk's Run target #11402

Open ViktorHofer opened 4 years ago

ViktorHofer commented 4 years ago

We currently define the RunCommand, RunArguments and RunWorkingDirectory properties which are used when Visual Studio F5 invokes the Run target. In case environment variables need to be passed in to the process invocation, a launchSettings.json file needs to be created in which you can specify environmentVariables.

Adding a RunEnvironmentVariables property removes the necessity of adding a launchSettings.json file for such scenarios and works outside of Visual Studio as well (launchSettings.json are VS specific files).

The proposed change would be added to the RunInformation section in the Microsoft.NET.Sdk.targets file: https://github.com/dotnet/sdk/blob/36ef8b2aa8e5d579c921704bdab69a7407936889/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets#L706-L758

Example runsettings file with environment variables specified


{
    "profiles": {
        ".NET Framework xUnit Console": {
            "commandName": "Executable",
            "executablePath": "$(RunWorkingDirectory)$(RunCommand)",
            "commandLineArgs": "$(RunArguments) -parallel none",
            "workingDirectory": "$(RunWorkingDirectory)",
            "environmentVariables": {
                "DEVPATH": "$(TestHostRootPath)"
            }
        }
    }
}

cc @dsplaisted @eerhardt

eerhardt commented 4 years ago

When we add this, we will also need to update VS to respect it as well. It doesn't directly use the Run target, but instead looks for the $(RunCommand), $(RunWorkingDirectory), and $(RunArguments) properties itself and then starts the process that way.

https://github.com/dotnet/project-system/blob/64939b1ae7196e5f18eeb6a9292f8c576f8bd63f/src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Debug/ProjectLaunchTargetsProvider.cs#L401-L403

FYI - @davkean @BillHiebert

ViktorHofer commented 4 years ago

Thanks Eric for the correction. Do we know the intent of the Run target and its potential consumers?

eerhardt commented 4 years ago

Looks like it came in with https://github.com/dotnet/sdk/pull/3820. Apparently there is a Run target in the core MSBuild that people can run with msbuild /t:Run. The above target is overriding the core Run target, so it works correctly.

baronfel commented 1 year ago

Noting here that since this issue was raised we've added Launch Profile support to dotnet run - the --lp/--launch-profile option takes the name of the launch profile to use, and launch profiles support environment variable settings, etc. VS, VSCode and Rider all support these profiles.