bazelbuild / bazelbuild/bazel

copt arguments that include spaces don't work with MSVC

Open
#19,623 5 comments 0 reactions 0 assignees View on GitHub
P3 team-Rules-CPP type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 18h
Merged PRs (30d)
75

Description

### Description of the bug:

When compiling a C++ program with Bazel, `--copt="-D FOO"` works for gcc and clang, but not for MSVC.

### Which category does this issue belong to?

C++/Objective-C Rules

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

1. On a Windows machine with MSVC installed, clone https://github.com/bazelbuild/examples
2. Replace the contents of `examples/cpp-tutorial/stage1/main/hello-world.cc` with

```c
#ifdef FOO
#pragma message("FOO is defined!")
#else
#pragma message("FOO isn't defined :(")
#endif
int main() { return 0; }
```

4. Observe that `bazel build --copt="-D FOO" ...` prints "FOO isn't defined :("

---

Note that all of these commands correctly print "FOO is defined!":
- `bazel build --copt="-DFOO" ...`
- `bazel build --copt="-D FOO" --compiler=clang-cl ...`
- `cl.exe -D FOO main/hello-world.cc`

### Which operating system are you running Bazel on?

Windows 11 Pro

### What is the output of `bazel info release`?

release 6.3.2

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?

_No response_

### Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No

### Have you found anything relevant by searching the web?

No

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproduction in examples/cpp-tutorial/stage1/main/hello-world.cc and run the reported bazel build --copt="-D FOO" command on Windows with MSVC. Trace how Bazel passes spaced --copt arguments to MSVC, then verify the build prints "FOO is defined!" while preserving the working gcc and clang behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.