Nvcc fatal: Could not set up the environment for Microsoft Visual Studio using vcvars64.bat during JIT compilation of bias_act.cu (CUDA 12.6, VS2022 BuildTools)

I’m encountering an error while a PyTorch-based project tries to JIT compile bias_act.cu. The build fails with the following message from nvcc:

nvcc fatal   : Could not set up the environment for Microsoft Visual Studio using 'C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.43.34808/bin/HostX86/x64/../../../../../../../VC/Auxiliary/Build/vcvars64.bat'
ninja: build stopped: subcommand failed.

The path to vcvars64.bat seems correct, and running it manually in a terminal sets up the environment successfully (e.g., cl.exe becomes available). However, nvcc fails when attempting to use it automatically.

Details:

  • This happens during JIT compilation of bias_act.cu
  • CUDA version: 12.6
  • Visual Studio Build Tools 2022 are installed (no full IDE, just Build Tools)
  • The following is included in my system PATH:
    C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.43.34808\bin\Hostx86\x64