bazelbuild / bazelbuild/bazel

args escaping in executable rules

Open
#11,587 4 comments 0 reactions 0 assignees View on GitHub
not stale P3 team-Rules-Server type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the problem / feature request:

Many executable rules provides an `args` attribute which is supposed to be a list of arguments to the command. However the semantic of it is confusing:

- arguments are passed without escaping to the sub command, so the list semantic is not respected. i.e. one argument in the bazel list can generate many arguments to the sub command
- some chars seems to be ignored.

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

Build the following program:

```c
#include

int main(int argc, char **argv)
{
for(int i = 0; i < argc; ++i)
{
printf("%d) %s\n", i, argv[i]);
}
}
```

using the following `BUILD.bazel`:

```python
cc_binary(
name = "foo",
srcs = ["foo.c"],
args = ["hello world", "you are", "amazing", "'escape'", '"escape"'],
)
````

Then `bazel run //:foo`.

I'm expecting the following output:

```
1) hello world
2) you are
3) amazing
4) 'escape'
5) "escape"
```

Instead, I'm observing:

```
1) hello
2) world
3) you
4) are
5) amazing
6) escape
7) escape
```

See how `hello world` and `you are` are split to two arguments and both `escape` misses their quotes delimiters.

I observed the same thing with others rules, such as `sh_binary`, ....

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

Linux.

### What's the output of `bazel info release`?

Sorry, I closed my docker container. It was a bazel 3.2.0, installed using the following procedure: https://docs.bazel.build/versions/master/install-ubuntu.html#install-with-installer-ubuntu

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

> Replace these lines with your answer.
>
> Places to look:
> - StackOverflow: http://stackoverflow.com/questions/tagged/bazel
> - GitHub issues: https://github.com/bazelbuild/bazel/issues
> - email threads on https://groups.google.com/forum/#!forum/bazel-discuss

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

This unanswered stock overflow question two years ago: https://stackoverflow.com/questions/47957685/patterns-for-passing-args-to-binary-rules-in-bazel

I'll be tempted to submit a pull request, but this may be a breaking change, so it may need a deprecation process.

Contributor guide

Open the contributing guide

Research direction

Start with the BUILD.bazel reproduction using cc_binary and bazel run //:foo, then compare the behavior with sh_binary as reported. The change is done when each args list element reaches the executable as one argument, including spaces and quote characters, with regression coverage for the shown output and any documented compatibility impact.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.