The "SetEnv" task failed unexpectedly

I had just upgraded to UE 5.7 and Visual Studio 2026. I created a fresh new C++ project using the third person template. When I go to build the project in Visual Studio I keep getting this error:

1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: The “SetEnv” task failed unexpectedly.
1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: System.ArgumentException: Environment variable name or value is too long.
1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: at System.Environment.SetEnvironmentVariable(String variable, String value)
1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: at System.Environment.SetEnvironmentVariable(String variable, String value, EnvironmentVariableTarget target)
1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: at Microsoft.Build.CPPTasks.SetEnv.Execute()
1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Execute()
1>C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.Cpp.Current.targets(102,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()

I do see that the project has very long paths in include directories and source directories under project properties → configuration properties → VC++ directories → include directories and source directories.

How can I solve this issue?

2 Likes

Hello there @COLD_BLOOD4!

Those paths in your log do look a bit long, and the most straightforward solution would be to reduce them as much as possible. In order to do so, you would need to move your UE install, and your project’s location, to different locations.

Start with the engine, moving to your root, with a very short folder containing it (for example, C:\UE57). Then do the same with the project, as short as possible (something like C:\Projs\Game1)

Once you complete the move, please make sure you right click your .uproject file, and select “Generate Visual Studio Project Files”. Additionally, if UE pops up with any conflict regarding the move, a reinstall to the new location would be the best method to resolve that.

Hey @brs-sebascova thank you for your reply.

My path for the project is D:\UnrealEngine5\UnrealProjects\BattleBlaster and the path for the engine is D:\Epic Games\UE_5.7\Engine. I dont think they’re as long that they should give an error.

I did read that it has something to do with my include directories setting for the project. Unreal injects hundreds of paths to the include directories, and the setenv function is trying to include all of those paths, and that is what’s causing the failure.

My question is why in the first place unreal inject all these paths? And would removing them do anything to my project?

@COLD_BLOOD4 Definitely not an expert in UE+VS, but faced the same issue as you. I tried a bunch of things, but my installation was already at the disk level.

What worked for me was deleting the VS project and using a different IDE that works directly with the project file (not the .sln one).

I also noticed UE was generating includes that did not exist in my installation for 5.7.

Note: Was also using VS2026 + UE5.7

As a workaround, I deleted the Include Directories field set up in the generated solution file, as described here: Unreal Engine 4.25 source code compile error

This fixes the build, but has the unfortunate side effect that Intellisense suddenly doesn’t know where to look for any engine includes, so it starts flagging all your code with red squigglies.

2 Likes

The same thing happened to me when I did that. I don’t know why this would happen, why would unreal all of a sudden inject a bunch of paths to the include directories? And is there no way to fix it where everything will work

You don’t have to clean them all,just remove some of those entries till you can build the sln,it seems to be a vs2026 bug which it can’t handle too many of include paths.

How do I know how many to clean and which ones? All the paths point to UHT or VI files

For my setup, i delete all the lines above “C:\UE_5.7\Engine\Intermediate\Build\Win64\UnrealEditor\Inc\CoreUObject\VerseVMBytecode“

I recently encountered the same error while building my project from Visual Studio:

After some investigation, I noticed that the Unreal Engine Integration Configuration inside Visual Studio now exposes a setting called Max Include Paths Length (chars). This setting corresponds to the MaxSharedIncludePaths field inside BuildConfiguration.xml (located under UnrealBuildTool/BuildConfiguration, as shown in the screenshot blow).

IMG-20251118-3

Solution:
I removed all MaxSharedIncludePaths entries from every BuildConfiguration.xml, deleted the generated .sln, and regenerated the solution. After doing so, the build completed normally and the MSBuild SetEnv error no longer occurred.

12 Likes

Thanks for the reply, but i only see 1 buildConfiguration.xml file and it didn’t have that line regrading the MaxSharedIncludePaths.

What is weird is I even tried uninstalling everything and reinstalling VS 2022 and UE 5.6.1, for some reason it still happened, I got the same error. Then today it started working building from the IDE. However now that I try to open the uproject it gives me this

I tried everything, the binaries, intermediate, derivedcache, saved folders, regenerating the solution files, building from source. Nothing worked. I’m left with uninstalling UE, uninstalling the epic launcher, and trying from scratch, this is the worst. I wish someone from Epic would comment on this

1 Like

I resolved this issue by deleting %AppData%\Unreal Engine\UnrealBuildTool\BuildConfiguration.xml

Then re-generating the project from the editor. I’m still having other issues with VS2026 and Unreal, personally. But I just keep VS2022 installed alongside it.

1 Like

Did deleting the xml fix the problem fully? Or does it come with downsides (like how removing the include paths breaks intellisense).

Also, out of curiosity, what other issues are you having with Vs 2026 + Unreal?

Please try this workaround:

Open Visual Studio 2026 and install Game Development with C++ workload if needed.

  1. Navigate to the Unreal Engine Integration Configuration Page which will appear when opening a Unreal Engine solution.

  2. Replace Max Include Paths Length (chars) with a character limit that is below 32,000 characters.

  3. Check the box next to “Regenerate the current solution when applying a new Unreal Build Tool config”

  4. Select the “Apply Unreal Build Tool Config” button

  5. Regenerate your solution with the new configuration

  6. Reload your solution

  7. Rebuild your solution and see if SetEnv issue is no longer present

4 Likes

@dorgon_chang This is the fix that worked for me! Thanks!

Deleting my Projects Include entries works too! BUT when I generate a new Solutions file, my project’s Include Directory refreshes all those include back into the file.

Deleting the MaxSharedIncludePath in the UnrealBuildToolBuildConfiguration.xml is a better solution.

I’m getting this same error, but with UE 5.6 and vs2022

Unfortunately (maybe due to the version) there are no MaxSharedIncludePath options in the UnrealBuildTool

I’m hesitant to upgrade to vs2026 - I tried this a few days ago and had numerous errors, including broken Intellisense, when trying to simply build a fresh template project. It seems that vs2026 is not compatible with UE 5.6 - I need to stay on this UE version for the time being

Any other suggestions?

Hi,
That works for me too, thank you a lot!

Perfect

May your pillow forever be cool exactly when you want it to be.
This solved my problem.

1 Like