When building through Visual Studio, UBT picks up undesired environment variables

So the reason, and look at the reproduction steps, is that visual studio when it invokes build.bat through the NMake flow, does this through the Visual Studio 2022 command prompt equivalent. This adds INCLUDE and PATH entries. One of the include entries is the current Windows SDK that ships with 2022.

cl.exe by default picks up INCLUDE and PATH environment variables. This means that the compile environment ends up being different when building via the IDE vs external.

This can be avoided by adding /X to cl.exe in VcToolchain.cs. This worked for us.

With VS2026 being released, this issue may rear its head once again as many people will start using a newer IDE. Some times having the different headers included may be ok, other cases there may be obscure issues. Either way, UBT really should work the same inside or outside VS invocation, so I strongly recommend looking at adding /X.

Btw, this will also affect recommendations given [Content removed] - building UE4.27 through 2022’s IDE will be affected in the same way.

[Attachment Removed]

Steps to Reproduce
I hit this issue with building 5.0, but the issue is entirely UE version agnostic. Simple modify build.bat to output the environment variables to stdout.

Note, 5.0.0 (which was originally 2019) failed to build through the 2022 IDE because of

#if __has_include(<sanitizer/asan_interface.h>)

That header only exists in newer Windows SDKs, not the one pinned in 5.0.0. Building from the commandline invoking UBT works, as does building through 2019, but building through 2022 failed.

[Attachment Removed]

We currently clear out the INCLUDE environment variable entirely, and I haven’t ever seen us have issues with PATH before but I can look into passing /X. That won’t make it into a 5.7 hotfix though we’re far to late in the release cycle for that.

[Attachment Removed]

Well regardless, I’ve checked in the change to pass /X, it will be in ue5-main on github soon and released with UE5.8.

[Attachment Removed]

Ah thanks, good to know. With some legacy codebases (4.27 and older 5’s that change wouldn’t have been there).

I just checked, and the code to clean out INCLUDE exists in 4.25, 4.27 and 5.0 (options I had to hand quickly) but clearly I’ve reproduced this issue of cl.exe env polution in at least 4.25 and 5.0. We verified this by replacing cl.exe at our end with a wrapper that logs all environment variables.

And I just double checked using process explorer, there is definitely an INCLUDE env var set for spawned cl.exe processes.

However, on further investigation, under 5.7 this doesn’t appear to happen. So even though older engine versions (inc 4.27) had an attempt at doing so, I suspected it didn’t work back then. At least worth noting for people still compiling 4.27 who may now be using VS2022 as their IDE.

[Attachment Removed]