Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-57436

Environment variable too long error when building .NET Core project

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None

      I cannot build a windows service .NET Classic (v4.6.1) project that references ASP.NET Core libraries (for REST API functionality)

      Pipeline code:

      bat "\"${tool 'MSBuild 2017'}\" /p:Configuration=Release /p:Platform=\"${targetPlatform}\" /p:TargetFrameworkVersion=${dotnetVersion} /p:OutputPath=\"${WORKSPACE}\\${buildOutputPath}\""
      
      

      Jenkins runs this command

      "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe" /p:Configuration=Release /p:Platform="x86" /p:TargetFrameworkVersion=v4.6.1 /p:OutputPath="E:\Jenkins\workspace\repo_name_branch\output"
      

      I get the following error:

      [2019-05-13T14:43:55.488Z] Done executing task "MSBuild". (TaskId:228)
      [2019-05-13T14:43:55.488Z] Done building target "Build" in project "Service.sln".: (TargetId:5)
      [2019-05-13T14:43:55.488Z] Done Building Project "E:\Jenkins\workspace\repo_name_branch\Service.sln" (default targets).
      [2019-05-13T14:43:55.488Z] 
      [2019-05-13T14:43:55.488Z] Unhandled Exception: Microsoft.Build.Shared.InternalErrorException: MSB0001: Internal MSBuild Error: Environment variable name or value is too long.
      [2019-05-13T14:43:55.488Z] =============
      [2019-05-13T14:43:55.488Z] System.ArgumentException: Environment variable name or value is too long.
      [2019-05-13T14:43:55.488Z]    at System.Environment.SetEnvironmentVariable(String variable, String value)
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.BackEnd.InProcNode.HandleShutdown(Exception& exception)
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.BackEnd.InProcNode.Run(Exception& shutdownException)
      [2019-05-13T14:43:55.488Z] 
      [2019-05-13T14:43:55.488Z]  ---> System.ArgumentException: Environment variable name or value is too long.
      [2019-05-13T14:43:55.488Z]    at System.Environment.SetEnvironmentVariable(String variable, String value)
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.BackEnd.InProcNode.HandleShutdown(Exception& exception)
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.BackEnd.InProcNode.Run(Exception& shutdownException)
      [2019-05-13T14:43:55.488Z]    --- End of inner exception stack trace ---
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Exception innerException, Object[] args)
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.BackEnd.InProcNode.Run(Exception& shutdownException)
      [2019-05-13T14:43:55.488Z]    at Microsoft.Build.BackEnd.NodeProviderInProc.InProcNodeThreadProc()
      [2019-05-13T14:43:55.488Z]    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
      [2019-05-13T14:43:55.488Z]    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      [2019-05-13T14:43:55.488Z]    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      [2019-05-13T14:43:55.488Z]    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
      [2019-05-13T14:43:55.488Z]    at System.Threading.ThreadHelper.ThreadStart()
      script returned exit code -532462766

      So I get an Environment variable name or value is too long. error from MsBuild.

      The funny thing is, that running the same command in cmd terminal on the very same computer works. Only under Jenkins seems the build not working. I tried to run Jenkins as a windows service (under SYSTEM user), or just as a regular process under my own user account. Both times this fails for the same reason.

          [JENKINS-57436] Environment variable too long error when building .NET Core project

          Péter Kovács added a comment - - edited

          I also tried using powershell step instead of bat step, but the same error occures.

          Péter Kovács added a comment - - edited I also tried using powershell step instead of bat step, but the same error occures.

          James Withers added a comment -

          I had this issue whilst using MSBuild and MSDeploy, was a very long build parameter that was being added to the env variables.

          I found this using the verbose flag and was able to null the env variable by parsing the info I needed out of it and then disposing of it by setting it to an empty string.

          James Withers added a comment - I had this issue whilst using MSBuild and MSDeploy, was a very long build parameter that was being added to the env variables. I found this using the verbose flag and was able to null the env variable by parsing the info I needed out of it and then disposing of it by setting it to an empty string.

          Daniel Wu added a comment -

          runnging the following cmd via jenkins gives me the exactly error, but the same command works on this machine, I have tested, I also tried devenv to build the solution, nothing changes
          C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" Server/Project.sln -restore /p:Configuration="Release" /p:Platform="Any CPU"

          Daniel Wu added a comment - runnging the following cmd via jenkins gives me the exactly error, but the same command works on this machine, I have tested, I also tried devenv to build the solution, nothing changes C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" Server/Project.sln -restore /p:Configuration="Release" /p:Platform="Any CPU"

          lacostej added a comment -

          Changing component to the one owning org.jenkinsci.plugins.durabletask.WindowsBatchScript.

          lacostej added a comment - Changing component to the one owning org.jenkinsci.plugins.durabletask.WindowsBatchScript.

          Tilo added a comment -

          Are there any updates on this issue? Does the issue still persist?

          Tilo added a comment - Are there any updates on this issue? Does the issue still persist?

          From the stack trace, it seems the exception was thrown from the environment-variable-restoring code here: https://github.com/dotnet/msbuild/blob/9802d43bc30b997b75c5efa310a9d379bd56162f/src/Build/BackEnd/Node/InProcNode.cs#L326-L338. Which is strange, as this code attempts to set the same environment variables that were already set before, i.e. their names and values should not be too long. According to the SetEnvironmentVariableW documentation, Windows Server 2012 R2 has no limit on the size of the whole environment block.

          If anyone is still hitting this error, then here are some ideas for possible workarounds:

          • Insert a bat 'SET' pipeline step to check which environment variables are defined before the `msbuild` command and whether any of them have excessively long values. Then add commands to delete those environment variables or set shorter values.
          • Alternatively, set MSBUILDNOINPROCNODE=1 in the environment. I think this would prevent MSBuild from using the Microsoft.Build.BackEnd.InProcNode class in which the error occurred. However, there might be side effects such as Building solution with forced out-of-proc nodes fails · Issue #3517 · dotnet/msbuild.

          Kalle Niemitalo added a comment - From the stack trace, it seems the exception was thrown from the environment-variable-restoring code here: https://github.com/dotnet/msbuild/blob/9802d43bc30b997b75c5efa310a9d379bd56162f/src/Build/BackEnd/Node/InProcNode.cs#L326-L338 . Which is strange, as this code attempts to set the same environment variables that were already set before, i.e. their names and values should not be too long. According to the SetEnvironmentVariableW documentation, Windows Server 2012 R2 has no limit on the size of the whole environment block. If anyone is still hitting this error, then here are some ideas for possible workarounds: Insert a bat 'SET' pipeline step to check which environment variables are defined before the `msbuild` command and whether any of them have excessively long values. Then add commands to delete those environment variables or set shorter values. Alternatively, set MSBUILDNOINPROCNODE=1 in the environment. I think this would prevent MSBuild from using the Microsoft.Build.BackEnd.InProcNode class in which the error occurred. However, there might be side effects such as Building solution with forced out-of-proc nodes fails · Issue #3517 · dotnet/msbuild .

            kohsuke Kohsuke Kawaguchi
            elektromodulator Péter Kovács
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: