title: Environment variables from launchsettings.json are being used in tests?
body: <p>I have a .NET Core build pipeline that is used to run some tests on Azure DevOps. Settings for each environment are stored in configuration files such as:</p>
  <ul>
  <li><code>appsettings.json</code></li>
  <li><code>appsettings.qa.json</code></li>
  <li><code>appsettings.test.json</code></li>
  </ul>
  <p>Build is very basic - it contains a <code>dotnet restore</code>, <code>dotnet build</code> and <code>dotnet test</code> tasks:</p>
  <p><a href="https://i.stack.imgur.com/uvjHW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uvjHW.png" alt="Build definition" /></a></p>
  <p>The <code>ASPNETCORE_ENVIRONMENT</code> environment variable is set in the <strong>Variables</strong> section of the build pipeline:</p>
  <p><a href="https://i.stack.imgur.com/ByxLJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ByxLJ.png" alt="Build variables" /></a></p>
  <p>These builds are running on VMs that contains multiple build agents (private, not hosted).</p>
  <p>Now the weird part - <strong>sometimes the build is picking up the wrong settings!</strong>.</p>
  <p>After some investigation and adding more logging we realized that <code>ASPNETCORE_ENVIRONMENT</code> value sometimes is <code>Development-Selfhost</code>, instead of <code>QA</code>. That value seems to come from the <code>launchsettings.json</code> file of a project that is referenced by the tests project:</p>
  <pre><code>{
    &quot;profiles&quot;: {
      &quot;MyProject.PublicApi&quot;: {
        &quot;commandName&quot;: &quot;Project&quot;,
        &quot;launchBrowser&quot;: false,
        &quot;environmentVariables&quot;: {
          &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development-Selfhost&quot;
        },
        &quot;applicationUrl&quot;: &quot;http://localhost:5028/&quot;
      }
    }
  }
  </code></pre>
  <p>According to <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1" rel="nofollow noreferrer">Use multiple environments in ASP.NET Core</a>, <code>launchSettings.json</code> file is used when an app is launched with <code>dotnet run</code>:</p>
  <blockquote>
  <p><code>launchSettings.json</code> is read if available. <code>environmentVariables</code> settings in <code>launchSettings.json</code> override environment variables.</p>
  </blockquote>
  <p>Adding the <code>launchSettings.json</code> to <code>.gitignore</code> solves my issue, but I'm trying to understand why it's using these settings if I don't execute a <code>dotnet run</code> command in my build pipeline. Also, why is this behaviour so random? Sometimes it uses the right settings, sometimes it doesn't.</p>
  <h2>UPDATE 1 (13/12/2019):</h2>
  <p>I've checked the logs and I can confirm file <code>launchSettings.json</code> is copied into the <code>bin</code> folder of the test project.</p>
  <p>I've tried setting a different value for <code>ASPNETCORE_ENVIRONMENT</code> that is not set anywhere else:</p>
  <pre><code>{
    &quot;profiles&quot;: {
      &quot;MyProject.PublicApi&quot;: {
        &quot;commandName&quot;: &quot;Project&quot;,
        &quot;launchBrowser&quot;: false,
        &quot;environmentVariables&quot;: {
          &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Rui&quot;
        },
        &quot;applicationUrl&quot;: &quot;http://localhost:5028/&quot;
      }
    }
  }
  </code></pre>
  <p>Behaviour is basically the same - sometimes it uses the value set in the pipeline and other times it will use the value set in <code>launchSettings.json</code> and will fail, because there is no correspondent configuration file:</p>
  <blockquote>
  <p>System.IO.FileNotFoundException : The configuration file 'appsettings.Rui.base.json' was not found and is not optional.</p>
  </blockquote>