bazel: Expansion of $(location) on path with spaces quotes too many times

Tested with Bazel 0.18.0 and master as of d4e3ad8951.

Small test to reproduce at: https://github.com/ob/bazel-tests/tree/master/spaces

If I run:

$ bazel build --spawn_strategy=standalone -s --sandbox_debug //spaces:no_spaces

I get this output (paths removed and output elided for clarity):

DEBUG: arule.bzl:3:5: Expanding Location Placeholders...
DEBUG: arule.bzl:4:5: Values: ["-c", "$(location adir/foo.c)"]
DEBUG: arule.bzl:5:5: Expansion: ["-c", "spaces/adir/foo.c"]

SUBCOMMAND: # //spaces:no_spaces [action 'SkylarkAction spaces/adir/foo.o']
(cd /private/var/tmp/_bazel_obonilla/124df79d9592378b17b94075fb66c278/execroot/__main__ && \
  exec env - \
  cc -c spaces/adir/foo.c -o bazel-out/darwin-fastbuild/bin/spaces/adir/foo.o)

And the build succeeds. However, if I run:

$ bazel build --spawn_strategy=standalone -s --sandbox_debug //spaces:spaces

I get this output:

DEBUG: arule.bzl:3:5: Expanding Location Placeholders...
DEBUG: arule.bzl:4:5: Values: ["-c", "$(location a dir/foo.c)"]
DEBUG: arule.bzl:5:5: Expansion: ["-c", "'spaces/a dir/foo.c'"]

SUBCOMMAND: # //spaces:spaces [action 'SkylarkAction spaces/a dir/foo.o']
(cd /private/var/tmp/_bazel_obonilla/124df79d9592378b17b94075fb66c278/execroot/__main__ && \
  exec env - \
  cc -c ''\''spaces/a dir/foo.c'\''' -o 'bazel-out/darwin-fastbuild/bin/spaces/a dir/foo.o')
ERROR: BUILD:13:1: SkylarkAction spaces/a dir/foo.o failed (Exit 1): cc failed: error executing command 
  (cd /private/var/tmp/_bazel_obonilla/124df79d9592378b17b94075fb66c278/execroot/__main__ && \
  exec env - \
  cc -c ''\''spaces/a dir/foo.c'\''' -o 'bazel-out/darwin-fastbuild/bin/spaces/a dir/foo.o')
clang: error: no such file or directory: ''spaces/a dir/foo.c''
clang: error: no input files
Target //spaces:spaces failed to build

Note that the input_file is double-quoted. The ctx.expand_location() function seems to have realized there are spaces, and it single quoted the path. Later on, the action quoted it again.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (13 by maintainers)

Most upvoted comments

I think this functionality deserves a revamp, however not sure how it looks like.

I agree the sane fix is to remove the quotes.

+1 to revamp. I’ve thought about this many times. My strawman proposal is.

  • genrule.cmd and migrate to genrule.args.
  • args mimics the ctx.action.args api
  • The first args is not always what we execute.
    • There should be an explicit executable. If provided, executable is what we execv. args[0] is what we pass to the executable in argv. Feels like busybox a bit.
    • If you explicitly want bash, then we provide a label (e.g. @bazel_shells//bash:bash).
    • hand wavy ideas about bash on windows? do we allow .bat?
  • in genrule and ctx we stop constructing a command line and passing it to the command interpreter.
    • instead, we use exec and pass unquoted strings to exec, since we are not going through a shell.
    • this still can have problems on windows, but works pretty well for *ix-ish systems.

As a lower cost solution, as a rule author I never pass files paths on the command line. I spill them into a file through that capability in ctx.action.args and have my tools read list of paths from command files. Yes, this can be slightly more overhead, but I don’t have to worry about shell escaping. That doesn’t help the quick and easy generule.