{"record":{"id":"a71d1c4481216158","repo":"nodejs/node","slug":"s-failed-with-error-d","errorCode":null,"errorMessage":"\"%s\" failed with error %d","messagePattern":"\"(.+?)\" failed with error (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"tools/gyp/pylib/gyp/msvs_emulation.py","lineNumber":1194,"sourceCode":"    generation and use custom environment files prepared by yourself.\"\"\"\n    archs = (\"x86\", \"x64\", \"arm64\")\n    if generator_flags.get(\"ninja_use_custom_environment_files\", 0):\n        cl_paths = {}\n        for arch in archs:\n            cl_paths[arch] = \"cl.exe\"\n        return cl_paths\n    vs = GetVSVersion(generator_flags)\n    cl_paths = {}\n    for arch in archs:\n        # Extract environment variables for subprocesses.\n        args = vs.SetupScript(arch)\n        args.extend((\"&&\", \"set\"))\n        popen = subprocess.Popen(\n            args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT\n        )\n        variables = popen.communicate()[0].decode(\"utf-8\")\n        if popen.returncode != 0:\n            raise Exception('\"%s\" failed with error %d' % (args, popen.returncode))\n        env = _ExtractImportantEnvironment(variables)\n\n        # Inject system includes from gyp files into INCLUDE.\n        if system_includes:\n            system_includes = system_includes | OrderedSet(\n                env.get(\"INCLUDE\", \"\").split(\";\")\n            )\n            env[\"INCLUDE\"] = \";\".join(system_includes)\n\n        env_block = _FormatAsEnvironmentBlock(env)\n        f = open_out(os.path.join(toplevel_build_dir, \"environment.\" + arch), \"w\")\n        f.write(env_block)\n        f.close()\n\n        # Find cl.exe location for this architecture.\n        args = vs.SetupScript(arch)\n        args.extend(\n            (\"&&\", \"for\", \"%i\", \"in\", \"(cl.exe)\", \"do\", \"@echo\", \"LOC:%~$PATH:i\")","sourceCodeStart":1176,"sourceCodeEnd":1212,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/gyp/pylib/gyp/msvs_emulation.py#L1176-L1212","documentation":"To capture the MSVC environment for ninja, gyp runs vs.SetupScript(arch) followed by '&& set' via subprocess.Popen and checks popen.returncode. A non-zero return code means the VS environment setup itself failed; gyp raises with the failing command and its exit code for diagnosis.","triggerScenarios":"The combined 'vcvarsall.bat <arch> && set' (or equivalent setup script) exits with a non-zero status when gyp invokes it to build environment.<arch>.","commonSituations":"The requested VS version/architecture is not installed; vcvarsall.bat errors due to a missing Windows SDK; GYP_MSVS_VERSION selects a toolchain that was uninstalled; running on a system where the VS command prompt shortcut resolves differently.","solutions":["Run the exact command shown in the error message by hand to see the underlying error text from vcvarsall.bat.","Confirm the requested architecture (e.g. x64) and VS version are actually installed.","Install/repair the Visual Studio C++ Build Tools and the matching Windows SDK.","Point GYP_MSVS_VERSION at an installed version, or let gyp auto-detect."],"exampleFix":"// before\n// gyp fails: \"vcvarsall.bat x64 && set\" failed with error 1\n// diagnose\n// $ cmd /c \"C:\\Program Files (x86)\\...\\vcvarsall.bat x64\"\n// fix: install x64 build tools, then\n// $ set GYP_MSVS_VERSION=2022\n// $ gyp --depth=. my.gyp","handlingStrategy":"try-catch","validationCode":"# Probe the VS setup script standalone before driving a full gyp run.\nargs = vs.SetupScript(arch) + ['&&', 'set']\nrc = subprocess.run(args, shell=True, stdout=subprocess.DEVNULL).returncode\nif rc != 0:\n    raise RuntimeError(f'VS setup script exited {rc}; install/repair the {arch} toolchain')","typeGuard":"def vs_setup_succeeds(vs, arch) -> bool:\n    args = vs.SetupScript(arch) + ['&&', 'set']\n    return subprocess.run(args, shell=True, stdout=subprocess.DEVNULL).returncode == 0","tryCatchPattern":"try:\n    env = _GenerateEnvironment(...)\nexcept Exception as e:\n    if 'failed with error' in str(e):\n        # Surface the failing command and run it manually for the real error.\n        print('VS environment setup failed; run the command in the error manually.', file=sys.stderr)\n    raise","preventionTips":["Install the matching VS Build Tools + Windows SDK for your target arch.","Validate GYP_MSVS_VERSION against installed versions before building.","In CI, add a health-check step that runs vcvarsall.bat once per pipeline."],"tags":["gyp","msvs","subprocess","toolchain"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}