Build fails because the environment block used to start a process cannot be longer than X bytes
Problem
Build fails and the following appears inside the log file produced by the build:
build 23-Feb-2016 11:37:01 System.InvalidOperationException: The environment block used to start a process cannot be longer than 65535 bytes. Your environment block is 67476 bytes long. Remove some environment variables and try again.
build 23-Feb-2016 11:37:01 at System.Diagnostics.EnvironmentBlock.ToByteArray(StringDictionary sd, Boolean unicode)
build 23-Feb-2016 11:37:01 at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
build 23-Feb-2016 11:37:01 at System.Diagnostics.Process.Start()
...
Diagnosis
The environment block is where environment variables are stored. Please bear in mind that Bamboo does not impose any limits. Build systems such as Bamboo tend to generate environment variables, and therefore checking this information during the build process would be the best way to determine what the problem is. If you are able to capture the error and output the environment (by running "env" in CMD or Powershell and also creating a script task in Bamboo to execute "env") you can compare the number of variables you have in your system vs what Bamboo is exporting to your system - it may lead to further insights.
Cause
The maximum size of the environment block seems to be 32,767 characters. Assuming every character has 2 bytes, this would give us the ~65k bytes. If you're getting this error it means you're exceeding the environment block size.
Workaround
There are a number of ways to workaround this problem, this will depend on what's actually causing it.
- Check the amount of variables you have in your system (without using Bamboo). Run "env" in CMD or Powershell. If it's not a large list, then it's most likely the variables generated as part of the build process that are causing the problem. If you have a large list, please consider trimming it down.
- If the problem is part of the build process, please check the amount of Global Variables you have listed in Bamboo. Since all Global Variables are exported to the server during the build process, consider using Plan/Deploy environment variables instead. This is exactly why we introduced such feature, so that you don't need to pollute the global scope with everything you got.
- If you are using Plan/Deploy variables and is only keeping Global Variables for what you need across your entire Bamboo instance, you should consider trimming down the amount of Plan/Deploy variables.