bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Bazel 0.21.0 flipped --incompatible_strict_action_env to true and broke itself

Open philwo opened this issue 7 years ago • 60 comments

Description of the problem / feature request:

Bazel 0.21.0 flipped --incompatible_strict_action_env to true via https://github.com/bazelbuild/bazel/issues/6648, which broke CI on macOS and Windows CI. On macOS we can no longer find "md5" (which is in /sbin) and on Windows we can no longer find Python, PowerShell and other tools.

We didn't discover these breakages during testing, because apparently we don't actually test Bazel itself in the "Bazel@HEAD + Downstream Projects".

We'll revert this via a flag in the CI config files, but flag owners should work on fixing our tests (and taking user feedback into account whether this new behavior is actually working well for others) so that we can eventually remove the config entry again.

Relevant logs:

  • Shell tests on macOS can't find "md5": https://storage.googleapis.com/bazel-buildkite-artifacts/08029882-a54b-44b4-bb8c-01d3fca82488/src/test/shell/shell_utils_test/attempt_1.log
  • Tests on Windows can't find Python: https://storage.googleapis.com/bazel-buildkite-artifacts/31d11339-45e6-4efa-8eb1-b61f7db6eba8/src%5Ctest%5Cjava%5Ccom%5Cgoogle%5Cdevtools%5Cbuild%5Clib%5Cblackbox%5Ctests%5CPythonBlackBoxTest%5Cattempt_1.log
  • Tests on Windows can't find Git: https://storage.googleapis.com/bazel-buildkite-artifacts/31d11339-45e6-4efa-8eb1-b61f7db6eba8/src%5Ctest%5Cshell%5Cbazel%5Cgit_repository_test%5Cattempt_1.log
  • Tests on Windows can't find Powershell: https://storage.googleapis.com/bazel-buildkite-artifacts/31d11339-45e6-4efa-8eb1-b61f7db6eba8/src%5Ctest%5Cjava%5Ccom%5Cgoogle%5Cdevtools%5Cbuild%5Clib%5Cblackbox%5Cframework%5Cframework_tests%5Cattempt_1.log

philwo avatar Jan 02 '19 15:01 philwo

Philipp, thanks for finding, filing, and working around this bug.

We (@philwo , @hlopko , and myself) argued about this flag today and realized that, while sh_* rules should probably not run python directly (why would a shell script need to do that?), they should be able to run their py_binary data-dependencies, which rightfully expect to have python / python.exe on the PATH.

It's unclear to me what should Bazel do. It seems that PATH's whole purpose is to contain the locations of these common tools, in which case PATH should be exempt from the "strict" env and be available to the action. On the other hand, PATH is inherently machine-specific and as such it poisons remote caches.

Another important observation @philwo made is that the "strict" action env is just as non-hermetic as the non-strict one; we just replace the clientenv's PATH with a hardcoded PATH string, but whatever is in those directories is out of our control (and not tracked by Bazel). And on one machine they might have python (and point to python2) but on another they might have no python at all or have python pointing to python3.

laszlocsomor avatar Jan 02 '19 15:01 laszlocsomor

One option would be to make PATH work more like TMPDIR, where it's not part of the action key and injected by the spawn runner at the last moment. That would be a step towards fixing #6392, too.

benjaminp avatar Jan 02 '19 16:01 benjaminp

To prevent this from happening again, I filed https://github.com/bazelbuild/continuous-integration/pull/434

meteorcloudy avatar Jan 02 '19 16:01 meteorcloudy

they should be able to run their py_binary data-dependencies, which rightfully expect to have python / python.exe on the PATH. [...] On the other hand, PATH is inherently machine-specific and as such it poisons remote caches.

I can't speak for PATH in general, but from the point of view of the Python interpreter: The long-term solution is to define the interpreter as a toolchain rule, where a particular toolchain target is appropriate for a specific system / path installation. The toolchain target for the current host system could be generated by a repository rule.

Currently py_runtime is the closest thing we have to a toolchain. The Python stub script only runs the python on PATH if no py_runtime was specified via --python_top.

brandjon avatar Jan 04 '19 19:01 brandjon

@brandjon : Can you explain how executing a py_binary-generated target would work if the python interpreter is a toolchain? As I see it, running bazel-bin/foo/py/bin (which is currently a symlink to the stub python script, whose shebang line is #!/usr/bin/env python) requires an interpreter on the PATH.

laszlocsomor avatar Jan 07 '19 09:01 laszlocsomor

And this is only the Linux/macOS stub script. On Windows we have another stub that is a C++ program (called the "launcher"). @meteorcloudy could correct me on this, but IIRC the local python interpreter's path is embedded in the launcher.

laszlocsomor avatar Jan 07 '19 09:01 laszlocsomor

@laszlocsomor The native launcher also only uses python on PATH if no py_runtime was specified, basically it always uses the same python binary as the python stub template, see https://github.com/bazelbuild/bazel/blob/f451cd89f1b5a3c2d1ef266d5444d0c42f93e322/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java#L200

That said, the python binary (including the native launcher for python) won't be a problem if python path is explicitly specified even with --incompatible_strict_action_env

meteorcloudy avatar Jan 07 '19 10:01 meteorcloudy

I think we need to change the default PATH on macos and Windows. It might be worthwhile to roll back the flag flip, except it may be too late now?

ulfjack avatar Jan 07 '19 12:01 ulfjack

@meteorcloudy : Thanks, that's comforting to know. @ulfjack : I agree that it's probably too late to roll back the flag flip commit now. What do you think the default PATH should contain on macOS and Windows?

laszlocsomor avatar Jan 07 '19 12:01 laszlocsomor

Regarding Windows:

filipesilva avatar Jan 07 '19 14:01 filipesilva

I don't have a mac available right now, but I take it /sbin is on the PATH by default, so we should add it to the default path in Bazel as well.

It's /Users/[user]/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin: according to https://www.reddit.com/r/MacOS/comments/5zabo3/question_what_is_the_default_path_for_macos/, although we should leave out the users home bin directory for hermeticity.

ulfjack avatar Jan 07 '19 15:01 ulfjack

For a specific proposal, I'd say we should at least set it to /usr/bin:/bin:/usr/sbin:/sbin on macOS.

ulfjack avatar Jan 07 '19 15:01 ulfjack

I assume the strict PATH env should contain the most common native tools available on that platform. For Windows, I think they should be:

  • C:\Windows and C:\Windows\System32, for common Windows cmd commands, eg. findstr.exe, whoami.exe, where.exe etc.
  • C:\Windows\System32\WindowsPowerShell\v1.0, for powershell.exe. @philwo pointed out different powershell versions are all installed in this same path, even if v1.0 is in the path.
  • C:\tools\msys64\usr\bin and C:\tools\msys64\bin, for bash bin tools. This will change if users specify a different bash executable by --shell_executable or shell toolchain.

But there are two issues to be considered on Windows:

  1. System root could change. Although the most common system root is C:\Windows, users could have different system root. If that's not C:\Windows, the default PATH env is wrong. One possible solution is to replace C:\Windows with %SYSTEMROOT%. We always set %SYSTEMROOT% for every action on Windows and it's a special env var set before launching the process, which won't affect any action cache. See https://github.com/bazelbuild/bazel/blob/bd78bc54c8b66750cf97f704889432a9680bd3a0/src/main/java/com/google/devtools/build/lib/windows/WindowsSubprocessFactory.java#L121
  2. File paths are case insensitive on Windows. Currently, if you change PATH from C:\foo\bar to C:\Foo\bar, the action will be re-executed, I think we should fix this.

meteorcloudy avatar Jan 07 '19 15:01 meteorcloudy

We need to achieve three things here:

  1. Have a default that's generally usable for local builds
  2. The default must work with remote execution (e.g., no local usernames, machine-specific directories, etc.); this is to avoid cache thrashing / cross-user cache misses by default
  3. Be easy to override as needed (with the caveat that different settings across users can cause cache misses / thrashing)

I think the images that will be built / are already being built for remote execution will largely put binaries in the locations matching the default PATH.

(I don't think I'm saying anything new, just trying to clarify the situation. Using %SYSTEMROOT% in PATH should be fine; however, we do need to figure out how to handle %SYSTEMROOT% with remote execution to prevent remote cache thrashing.)

ulfjack avatar Jan 08 '19 10:01 ulfjack

Re @meteorcloudy : I agree those paths (powershell, system32, msys2 bin and usr/bin) should be on the strict PATH, and nothing more. Challenge 1 seems to be workaround-able. I don't see why challenge 2 is a problem in the strict env case (or did you mean this for non-strict env builds?) What am I missing?

Also, it's not clear to me anymore who's waiting on whose input and who are decision makers on this bug. How can we move forward?

I'm happy to volunteer to add an incompatible flag enabling the strict PATH that @meteorcloudy proposed.

laszlocsomor avatar Jan 08 '19 15:01 laszlocsomor

@laszlocsomor Oh, you're right, the second point should not be a problem in strict env case.

That would be great if you can make a change to add those path for Windows. @ulfjack Can you take care of this problem on Mac?

meteorcloudy avatar Jan 08 '19 16:01 meteorcloudy

@philwo and I talked offline about this issue.

I think it's important to keep in mind that the only reason why we flipped this flag is to improve the usability for remote execution/caching users. At the expense of local execution users who do not have tools installed outside of Bazel's default PATH.

A possible solution that we discussed is to declare PATH as special and to not include it in the remote action definition at all. Instead we argue that the remote execution system should be responsible for providing a default PATH that works for its environment - just as the local system does. The remote system ultimately knows best where all the programs that it wants to make available are installed.

Now, if a user explicitly sets --action_env=PATH=... then we should forward the value to the remote execution system.

I don't really see any downside with this approach. Can anyone talk me out of this?

buchgr avatar Jan 08 '19 18:01 buchgr

@buchgr wouldn't two users with two different local paths (e.g. windows installed in different disks) be unable to use each others remote cache?

I think this topic a bit confusing. My local environment is unlikely to be similar to the remote executors. Especially on windows where I have to make a decision of where to install python/msys. Nothing tells me I have to install those things in default paths in order to be in the happy path. I wouldn't expect that my local path for python would prevent me from using the remote cache that my coworker set.

filipesilva avatar Jan 08 '19 18:01 filipesilva

@meteorcloudy : I'll wait with implementing this until @buchgr decides whether PATH will be part of the action signature at all. IIUC, if PATH is not part of the action then we can simply disable strict action env with local execution, and my change to the strict PATH would be unnecessary.

laszlocsomor avatar Jan 09 '19 07:01 laszlocsomor

@filipesilva The most recent idea would solve that problem, because the PATH (and possibly LD_LIBRARY_PATH and HOME) would not factor into the remote action cache key at all. Instead, the execution strategies would be responsible for setting PATH, LD_LIBRARY_PATH and HOME to good values (similar to what @benjaminp suggested). For example, on a local system, they'd be taken from the client environment (unless overridden by --action_env). On a remote system, the backend knows best what good values for these variables are. For example, when using Docker, it's usual to just specify PATH, HOME etc. inside the Dockerfile, because they have to be tailored to the container. There's no way Bazel could know what the correct variables are for a given container (unless you repeat the definition in some kind of Bazel-specific platform configuration, which would violate DRY and not be great usability).

Thus, two users with different PATHs would be able to get cache hits if everything else is the same for the actions. Whether that's safe or not is not something Bazel can decide, but the user can (by carefully controlling their environment, or by making developer workstations read only from the cache and only let CI machines populate it).

philwo avatar Jan 09 '19 08:01 philwo