bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.94k stars 4.02k forks source link

Windows AutoRun can kybosh bazel's attempts to run bat files #23001

Open peakschris opened 1 month ago

peakschris commented 1 month ago

Description of the bug:

I've just spent a few hours with a user debugging an odd issue with C++ builds on their machine. Bazel's attempts to invoke bat files were failing. Turns out this user had enabled AutoRun, which is a windows feature that invokes their custom bat script at the start of every shell invocation, and their bat script was failing.

This issue was previously opened and closed stale: https://github.com/bazelbuild/bazel/issues/8865

More info about AutoRun commands:

The fix is for Bazel to add the /D parameter to every cmd.exe invocation that it makes

Which category does this issue belong to?

No response

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

Create an autorun command that fails if USERPROFILE is not set Register it HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun Run a Bazel C++ build

Which operating system are you running Bazel on?

windows

What is the output of bazel info release?

bazel 7.2.1

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 HEAD ?

No response

If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

No response

Have you found anything relevant by searching the web?

No response

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

No response

fmeum commented 1 month ago

Bazel does add /D here: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/util/CommandBuilder.java;l=172?q=cmd.exe&ss=bazel%2Fbazel

Could you share a reproducer and/or the error message you get?

peakschris commented 1 month ago

@fmeum interesting! We are seeing this when cc toolchain is invoking bat.

Here is a reproducer: https://github.com/peakschris/bazel-autorun-bug

The reproducer demonstrates the issue during toolchain setup. I hope this is good to reproduce. Our use case is slightly more complex - we have vendored the configured toolchain and introduced a bat wrapper around cl.exe. But I hope that the underlying cause is the same.

D:\workdir\github\peakschris\bazel-autorun-bug>bazel build //... -s
Starting local Bazel server and connecting to it...
INFO: Repository bazel_tools~cc_configure_extension~local_config_cc instantiated at:
  <builtin>: in <toplevel>
Repository rule cc_autoconf defined at:
  D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/cc_configure.bzl:109:30: in <toplevel>
ERROR: An error occurred during the fetch of repository 'bazel_tools~cc_configure_extension~local_config_cc':
   Traceback (most recent call last):
        File "D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/cc_configure.bzl", line 89, column 36, in cc_autoconf_impl
                configure_windows_toolchain(repository_ctx)
        File "D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/windows_cc_configure.bzl", line 830, column 35, in configure_windows_toolchain
                msvc_vars_x64 = _get_msvc_vars(repository_ctx, paths, "x64")
        File "D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/windows_cc_configure.bzl", line 667, column 32, in _get_msvc_vars
                env = setup_vc_env_vars(repository_ctx, vc_path)
        File "D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/windows_cc_configure.bzl", line 383, column 24, in setup_vc_env_vars
                _check_env_vars(env_map, cmd, expected = envvars)
        File "D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/windows_cc_configure.bzl", line 389, column 32, in _check_env_vars
                auto_configure_fail(
        File "D:/udu/b/tqt54br5/external/bazel_tools/tools/cpp/lib_cc_configure.bzl", line 112, column 9, in auto_configure_fail
                fail("\n%sAuto-Configuration Error:%s %s\n" % (red, no_color, msg))
Error in fail:
Auto-Configuration Error: Setting up VC environment variables failed, PATH is not set by the following command:
    "c:\apps\MVS16\VC\Auxiliary\Build\VCVARSALL.BAT" amd64  -vcvars_ver=14.20.27508
ERROR: no such package '@@bazel_tools~cc_configure_extension~local_config_cc//':
Auto-Configuration Error: Setting up VC environment variables failed, PATH is not set by the following command:
    "c:\apps\MVS16\VC\Auxiliary\Build\VCVARSALL.BAT" amd64  -vcvars_ver=14.20.27508
ERROR: D:/workdir/github/peakschris/bazel-autorun-bug/cc_shared_lib/BUILD:3:11: //cc_shared_lib:base_include_dir depends on @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-x64_windows in repository @@bazel_tools~cc_configure_extension~local_config_cc which failed to fetch. no such package '@@bazel_tools~cc_configure_extension~local_config_cc//':
fmeum commented 1 month ago

windows_cc_configure.bat invoked the bat scripts directly. Are you saying that it would need to go through cmd /D to avoid AutoRun?

CC @meteorcloudy

meteorcloudy commented 1 month ago

Probably here: https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/windows_cc_configure.bzl;l=206

@peakschris Can you fix the command above and see if it works? If so, a PR is very appreciated!