dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.44k stars 10.02k forks source link

launchSettings.json ASPNETCORE_ENVIRONMENT should not be overridden by environment variable #53916

Open mrpmorris opened 8 months ago

mrpmorris commented 8 months ago

Is there an existing issue for this?

Describe the bug

If ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT are set on the development machine as environment variables, they override the ASPNETCORE_ENVIRONMENT in launchSettings.json

The environment for local machine development can be set in the Properties\launchSettings.json file of the project. Environment values set in launchSettings.json override values set in the system environment.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0

Expected Behavior

The setting in launchSettings.json should take priority.

Steps To Reproduce

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => builder.Environment.EnvironmentName);
app.Run();
{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7034",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "I should see this text"
      }
    }
  }
}
  1. Run the app and see the correct value.
  2. Set either ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT on your machine's Environment variables
  3. (Might need VS restart)
  4. Run again and see the incorrect value

Exceptions (if any)

No response

.NET Version

8.0.200-preview.23624.5

Anything else?

.NET SDK: Version: 8.0.200-preview.23624.5 Commit: 8065b9770c Workload version: 8.0.200-manifests.66f5ce51

Runtime Environment: OS Name: Windows OS Version: 10.0.22621 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.200-preview.23624.5\

.NET workloads installed: Workload version: 8.0.200-manifests.66f5ce51 [maui-windows] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 8.0.3/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json Install Type: Msi

[maccatalyst] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 17.2.8004/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.2.8004\WorkloadManifest.json Install Type: Msi

[ios] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 17.2.8004/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.2.8004\WorkloadManifest.json Install Type: Msi

[android] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 34.0.52/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.52\WorkloadManifest.json Install Type: Msi

[wasm-tools-net6] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 8.0.1/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.net6\8.0.1\WorkloadManifest.json Install Type: Msi

[wasm-tools-net7] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 8.0.1/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.net7\8.0.1\WorkloadManifest.json Install Type: Msi

[wasm-tools] Installation Source: VS 17.8.34525.116, VS 17.9.34526.213 Manifest Version: 8.0.1/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.1\WorkloadManifest.json Install Type: Msi

[aspire] Installation Source: VS 17.9.34526.213 Manifest Version: 8.0.0-preview.1.23557.2/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.0-preview.1.23557.2\WorkloadManifest.json Install Type: Msi

Host: Version: 8.0.1 Architecture: x64 Commit: bf5e279d92

.NET SDKs installed: 5.0.408 [C:\Program Files\dotnet\sdk] 7.0.203 [C:\Program Files\dotnet\sdk] 8.0.101 [C:\Program Files\dotnet\sdk] 8.0.200-preview.23624.5 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0-rc.2.23480.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0-rc.2.23479.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0-rc.2.23479.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

jscarle commented 8 months ago

Although I agree with you that this is confusing, ‪I believe you'll have a hard time getting that to change due to the dependencies across different core elements of .NET. This type of change could have unintended ‬consequences depending on which part of the execution chain is reponsible for this behavior.

‪launchSettings.json is actually parsed by "dotnet run" (which is part of the SDK). The environment variables are then parsed and fed into a .NET Process to launch the application (which is part of the runtime).‬

‪Its only then that the WebApplicationBuilder will kick in and start going through its configuration process.‬

‪So any one of those steps could be responsible for ordering of the environment variables.‬

Perhaps a change similar to what was done for prioritizing ASPNETCORE_URLS could be done, but I'm unsure of how effective that would be.

mrpmorris commented 8 months ago

Perhaps a change similar to what was done for prioritizing ASPNETCORE_URLS could be done, but I'm unsure of how effective that would be.

Could change the template of launchSettings.json to pass in the environment name in the commandLineArgs if not possible to make the behaviour match the docs.

gay003 commented 6 months ago

I agree that the default launchSettings.json template should be modified to include the necessary, expected environment variables. In my case, adding "DOTNET_ENVIRONMENT": "Development" to the "environmentVariables" section of profiles appropriately overrides the DOTNET_ENVIRONMENT variable set on my computer and allows the default, sample project to run without any further modifications. I suggest adding that environment variable to the default launch profiles.

As someone who is new to Blazor (or for anyone on an affected framework), it's off-putting when the sample project that Microsoft/VisualStudio provides as a learning tool is unable to run out-of-the-box, because it then puts new users in a situation where they have to attempt to investigate an issue involving a technology they don't understand. Combined with how obscure this issue is (the error message isn't clear and googling the error doesn't return results containing the relevant solution), I didn't find this GitHub issue until after I happened to identify and resolve the problem on my own after successfully testing the sample project on a separate, personal computer and comparing Visual Studio installation and project differences. If I hadn't been the one to setup the environment variable on my machine (as opposed to someone else like my organization's system administrators) and thus noticed/remembered the name mentioned in the console window it would've taken much, much longer to resolve the issue.