dotnet / dotnet/msbuild

MSBuild crashes if environment variable too long

Open
#1,765 7 comments 2 reactions 0 assignees View on GitHub
help wanted triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 13h
Merged PRs (30d)
133

Description

.NET rejects env var values over 32KB with ArgumentException. MSBuild sets environment varibles in several places and in this place at least does not handle that nicely. In this particular callstack it should perhaps just not set that environment variable. Possibly it should fail at initialization though if there's an environment variable that is so long it wouldn't be able to reset it when the build is done.

Note there's a (shorter) limit on env var name also.

https://github.com/dotnet/coreclr/issues/9793
```
06:57:53 Starting Build at 6:57:53.56
06:57:53 BUILD: Commencing CoreCLR Repo build
06:57:53 BUILD: Checking prerequisites
06:57:56 BUILD: Using environment: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
06:58:02 Installing .NET Version Manager to C:\Users\dotnet-bot.dnx\bin
06:58:02 Creating destination folder 'C:\Users\dotnet-bot.dnx\bin' ...
06:58:02 Installing 'dnvm.ps1' to 'C:\Users\dotnet-bot.dnx\bin' ...
06:58:02 Installing 'dnvm.cmd' to 'C:\Users\dotnet-bot.dnx\bin' ...
06:58:02 Adding C:\Users\dotnet-bot.dnx\bin to Process PATH
06:58:02 Adding C:\Users\dotnet-bot.dnx\bin to User PATH
06:58:06 Installing dotnet cli...
06:58:13 Restoring BuildTools version 1.0.27-prerelease-01209-01...
06:58:13 Initializing BuildTools ...
06:58:52 Updating CLI NuGet Frameworks map...
06:58:52 Done initializing tools.
06:58:52 Running: D:\j\workspace\x86_checked_w---b3a226f6\Tools\dotnetcli\dotnet.exe D:\j\workspace\x86_checked_w---b3a226f6\Tools\run.exe D:\j\workspace\x86_checked_w---b3a226f6\config.json build -Project=D:\j\workspace\x86_checked_w---b3a226f6\build.proj -generateHeaderWindows -NativeVersionHeaderFile="D:\j\workspace\x86_checked_w---b3a226f6\bin\obj_version.h" -BuildOS=Windows_NT -BuildType=Checked -BuildArch=x86
06:58:52 Running: D:\j\workspace\x86_checked_w---b3a226f6\Tools\msbuild.cmd /nologo /verbosity:minimal /clp:Summary /maxcpucount /nodeReuse:false /l:BinClashLogger,Tools/net45/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log D:\j\workspace\x86_checked_w---b3a226f6\build.proj /p:__BuildType=Checked /p:__BuildArch=x86 /p:__BuildOS=Windows_NT /t:GenerateVersionHeader /p:GenerateVersionHeader=true /p:NativeVersionHeaderFile=D:\j\workspace\x86_checked_w---b3a226f6\bin\obj_version.h
06:59:00
06:59:00 Unhandled Exception: Microsoft.Build.Shared.InternalErrorException: MSB0001: Internal MSBuild Error: Environment variable name or value is too long.
06:59:00 =============
06:59:00 System.ArgumentException: Environment variable name or value is too long.
06:59:00 at System.Environment.SetEnvironmentVariable(String variable, String value)
06:59:00 at Microsoft.Build.BackEnd.InProcNode.HandleShutdown(Exception& exception)
06:59:00 at Microsoft.Build.BackEnd.InProcNode.Run(Exception& shutdownException)
06:59:00
06:59:00 ---> System.ArgumentException: Environment variable name or value is too long.
06:59:00 at System.Environment.SetEnvironmentVariable(String variable, String value)
06:59:00 at Microsoft.Build.BackEnd.InProcNode.HandleShutdown(Exception& exception)
06:59:00 at Microsoft.Build.BackEnd.InProcNode.Run(Exception& shutdownException)
06:59:00 --- End of inner exception stack trace ---
06:59:00 at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Exception innerException, Object[] args)
06:59:00 at Microsoft.Build.BackEnd.InProcNode.Run(Exception& shutdownException)
06:59:00 at Microsoft.Build.BackEnd.NodeProviderInProc.InProcNodeThreadProc()
06:59:00 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
06:59:00 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
06:59:00 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
06:59:00 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
06:59:00 at System.Threading.ThreadHelper.ThreadStart()
06:59:01 Command execution failed with exit code -532462766.
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at Microsoft.Build.BackEnd.InProcNode.HandleShutdown, the entry point named in the call stack, and inspect the environment-variable cleanup path. Reproduce with an environment variable whose name or value exceeds the stated Windows limit, then determine and test the intended handling so shutdown no longer crashes; the issue leaves the exact behavior open.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.