Skip to content

[Bug] Compilation fails on Windows when MSVC path contains spaces (e.g. C:\Program Files...`) #1

@Strrrrrrrrrrrring

Description

@Strrrrrrrrrrrring

I've been watching this project for months, waiting for it to go open source — and the day finally came! I cloned it almost immediately and couldn't wait to try it out. Unfortunately I hit this bug right out of the gate on my machine.

Fair warning: the body of this issue was drafted with AI assistance. That said, I'm pretty confident my own write-up wouldn't have been any clearer — so here we are.

Summary

On Windows, when the auto-detected MSVC vcvars64.bat path contains spaces (e.g. C:\Program Files\Microsoft Visual Studio\...), the compilation step fails.

Root Cause

The compiler path returned by list_auto_search_compiler_paths() is correctly quoted with double quotes to handle spaces:

"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

However, when this quoted path is passed directly to system(), the C runtime invokes cmd.exe /c <command>. cmd.exe has a well-known parsing quirk: when the entire command string passed to cmd /c starts with a double-quote character, it strips the outermost matching pair of quotes before parsing the rest of the command line.

This means the command:

"C:\Program Files\...\vcvars64.bat" >nul 2>nul && cl ...

gets transformed by cmd.exe into:

C:\Program Files\...\vcvars64.bat >nul 2>nul && cl ...

The space in Program Files then causes C:\Program to be interpreted as the command name, and Files\... as an argument — resulting in the error above.

This affects all three places in the codebase where system() is called with a quoted MSVC path:

  1. Compiler availability check in compiler_utils.cpp (_test_compile_simple_program)
  2. Test compilation command in compiler_utils.cpp (_test_compile_simple_program)
  3. Actual codegen compilation command in Compilation.cpp

Fix

Wrap the entire command string with an explicit cmd /c "..." invocation. When cmd.exe sees cmd /c "...", it correctly treats the outer quotes as delimiters for the full command block, and the inner quoted path is parsed properly.

Before:

// compiler_utils.cpp — availability check
std::string check_cmd = compiler_path + " >nul 2>nul";

// compiler_utils.cpp — test compile
compile_cmd = compiler_path + " >nul 2>nul && cd " + folder_name + " && cl test.cpp /LD /Ox " + avx_flag + " >nul 2>nul";

// Compilation.cpp — actual compile
command = compiler_path + enable_output;
command += " && cd /d \"" + folder + "\"";
command += " && cl " + name + ".cpp /LD /Ox /arch:AVX2 /bigobj" + enable_output;
// ...

After:

// compiler_utils.cpp — availability check
std::string check_cmd = "cmd /c \"" + compiler_path + " >nul 2>nul\"";

// compiler_utils.cpp — test compile
compile_cmd = "cmd /c \"" + compiler_path + " >nul 2>nul && cd " + folder_name + " && cl test.cpp /LD /Ox " + avx_flag + " >nul 2>nul\"";

// Compilation.cpp — actual compile
std::string inner_command;
inner_command  = compiler_path + enable_output;
inner_command += " && cd /d \"" + folder + "\"";
inner_command += " && cl " + name + ".cpp /LD /Ox /arch:AVX2 /bigobj" + enable_output;
inner_command += " && del " + name + ".exp && del " + name + ".lib && del " + name + ".obj";
command = "cmd /c \"" + inner_command + "\"";

Impact

This bug affects any Windows machine where Visual Studio is installed in a path containing spaces, which includes the default installation path (C:\Program Files\...). The symptom is a hard failure at runtime with a misleading error message that looks like a missing compiler rather than a quoting issue.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions