Skip to content

Rustc failed to locate rc.exe on VS2022 Command Prompt, causing panic #146693

@zeroomega

Description

@zeroomega

After #146018 was landed, we start seeing rustc panics on Windows when we build rustc from source. As the compiler failed to locate the rc.exe

The rust revision we test on is eec6bd9 . We did a small instrumentation for debugging purpose:

diff --git a/compiler/rustc_windows_rc/src/lib.rs b/compiler/rustc_windows_rc/src/lib.rs
index caa5e5ef276..ea3759dd3ae 100644
--- a/compiler/rustc_windows_rc/src/lib.rs
+++ b/compiler/rustc_windows_rc/src/lib.rs
@@ -145,6 +145,7 @@ fn find_windows_sdk_tool(arch_or_target: &str, tool_name: &str) -> Option<path::
     // cc does include the Windows SDK tools in the PATH environment of MSVC tools.

     let msvc_linker = windows_registry::find_tool(arch_or_target, "link.exe")?;
+    eprintln!("tool: {:?}", msvc_linker);
     let path = &msvc_linker.env().iter().find(|(k, _)| k == "PATH")?.1;
     find_tool_in_path(tool_name, path)
 }

Steps we run:

  • Assume VS2022 Community or VS2022 build tools is installed.
  • Assume a recent clang-cl is available. Either built from source or from LLVM release. In our case, we built from source and it is located in C:/src/llvm-prebuilt/clang/windows-amd64
  • Checkout rust with submodules to C:\src\rust , to revision eec6bd9d69832f57341c6de6a93fa7b9f47e2111 which includes the PR compiler: Add Windows resources to rustc-main and rustc_driver #146018 .
  • Start x64 Native Tools Command Prompt for VS 2022 terminal from the start menu
  • Navigate to C:\src\rust. create config.toml
  • Start the build with python x.py install -v

Failure message with RUST_BACKTRACE=1

   Compiling rustc_span v0.0.0 (C:\src\rust\compiler\rustc_span)
error: failed to run custom build command for `rustc_driver v0.0.0 (C:\src\rust\compiler\rustc_driver)`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `C:\src\rust\fuchsia-build\x86_64-pc-windows-msvc\stage1-rustc\release\build\rustc_driver-388d58225b8919fc\build-script-build` (exit code: 101)
  --- stderr
  tool: Tool { path: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.38.33130\\bin\\HostX64\\x64\\link.exe", cc_wrapper_path: None, cc_wrapper_args: [], args: [], env: [], family: Msvc { clang_cl: false }, cuda: false, removed_args: [], has_internal_target_arg: false }

  thread 'main' panicked at compiler\rustc_windows_rc\src\lib.rs:39:77:
  found rc.exe
  stack backtrace:
     0: std::panicking::begin_panic_handler
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panicking.rs:697
     1: core::panicking::panic_fmt
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\core\src\panicking.rs:75
     2: core::panicking::panic_display
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\core\src\panicking.rs:268
     3: core::option::expect_failed
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\core\src\option.rs:2139
     4: core::option::Option<T>::expect
     5: rustc_windows_rc::compile_windows_resource_file
     6: build_script_build::main
     7: build_script_build::main
     8: std::rt::lang_start
     9: std::rt::lang_start
    10: std::rt::lang_start
    11: std::rt::lang_start_internal::closure$0
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\rt.rs:175
    12: std::panicking::catch_unwind::do_call
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panicking.rs:589
    13: std::panicking::catch_unwind
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panicking.rs:552
    14: std::panic::catch_unwind
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panic.rs:359
    15: std::rt::lang_start_internal
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\rt.rs:171
    16: std::rt::lang_start
    17: main
    18: invoke_main
               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    19: __scrt_common_main_seh
               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    20: BaseThreadInitThunk
    21: RtlUserThreadStart
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
warning: build failed, waiting for other jobs to finish...
error: failed to run custom build command for `rustc-main v0.0.0 (C:\src\rust\compiler\rustc)`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `C:\src\rust\fuchsia-build\x86_64-pc-windows-msvc\stage1-rustc\release\build\rustc-main-12e8bac0d7cbd909\build-script-build` (exit code: 101)
  --- stderr
  tool: Tool { path: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.38.33130\\bin\\HostX64\\x64\\link.exe", cc_wrapper_path: None, cc_wrapper_args: [], args: [], env: [], family: Msvc { clang_cl: false }, cuda: false, removed_args: [], has_internal_target_arg: false }

  thread 'main' panicked at compiler\rustc_windows_rc\src\lib.rs:39:77:
  found rc.exe
  stack backtrace:
     0: std::panicking::begin_panic_handler
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panicking.rs:697
     1: core::panicking::panic_fmt
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\core\src\panicking.rs:75
     2: core::panicking::panic_display
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\core\src\panicking.rs:268
     3: core::option::expect_failed
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\core\src\option.rs:2139
     4: core::option::Option<T>::expect
     5: rustc_windows_rc::compile_windows_resource_file
     6: build_script_build::main
     7: build_script_build::main
     8: build_script_build::main
     9: std::rt::lang_start
    10: std::rt::lang_start
    11: std::rt::lang_start
    12: std::rt::lang_start_internal::closure$0
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\rt.rs:175
    13: std::panicking::catch_unwind::do_call
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panicking.rs:589
    14: std::panicking::catch_unwind
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panicking.rs:552
    15: std::panic::catch_unwind
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\panic.rs:359
    16: std::rt::lang_start_internal
               at /rustc/788da80fcfcef3f34c90def5baa32813e39a1a41/library\std\src\rt.rs:171
    17: std::rt::lang_start
    18: main
    19: invoke_main
               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    20: __scrt_common_main_seh
               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    21: BaseThreadInitThunk
    22: RtlUserThreadStart
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
command did not execute successfully: "C:\\src\\rust\\fuchsia-build\\x86_64-pc-windows-msvc\\stage0\\bin\\cargo.exe" "build" "--target" "x86_64-pc-windows-msvc" "-Zbinary-dep-depinfo" "-j" "64" "-Zroot-dir=C:\\src\\rust" "--release" "--features" "llvm max_level_info" "--manifest-path" "C:\\src\\rust\\compiler/rustc/Cargo.toml" "--message-format" "json-render-diagnostics" (failure_mode=Exit)
expected success, got: exit code: 101
Traceback (most recent call last):
  File "C:\src\rust\x.py", line 53, in <module>
    bootstrap.main()
  File "C:\src\rust\src\bootstrap\bootstrap.py", line 1393, in main
    bootstrap(args)
  File "C:\src\rust\src\bootstrap\bootstrap.py", line 1368, in bootstrap
    run(args, env=env, verbose=build.verbose, is_bootstrap=True)
  File "C:\src\rust\src\bootstrap\bootstrap.py", line 237, in run
    raise RuntimeError(err)
RuntimeError: failed to run: C:\src\rust\fuchsia-build\bootstrap\debug\bootstrap.exe install -v

As you can see the rc.exe failed to be found (with a unclear "found rc.exe" message in stderr) and caused a panic. The msvc_linker object has an empty env map, which is why it eventually the following step to find rc.exe from env['PATH'] failed and triggered the panic. The bug is probably in find_msvc_environment function in https://github.com/rust-lang/cc-rs/blob/ac2192ced40bc82d69dd500bd561aa65c186610e/find-msvc-tools/src/find_tools.rs#L464 as it returned a tool object with empty env map.

The config.toml we used is


change-id = 146435


[llvm]
download-ci-llvm = true
#optimize = true
#thin-lto = false
#ninja = true
#targets = "X86"
#build-config = {  CMAKE_SYSROOT="C:/src/winsdk" }

#use-libcxx = true
# TODO(https://github.com/rust-lang/rust/issues/94983)
# We shouldn't have to set this to false to enable statically linking libc++
#static-libstdcpp = false

[build]
ccache = false
target = ["x86_64-pc-windows-msvc"]
build = "x86_64-pc-windows-msvc"
build-dir = "fuchsia-build"
submodules = false
docs = false
extended = true
cargo-native-static = true
tools = ["cargo", "rustfmt", "cargo-fmt", "clippy", "src", "llvm-tools", "rustdoc", "rust-analyzer-proc-macro-srv"]


[install]
prefix = "C:/src/rust/install_prefix"
sysconfdir = "etc"

[rust]
download-rustc = false
optimize = true
codegen-units = 1
codegen-units-std = 1
channel = "nightly"
lld = false
llvm-libunwind = "in-tree"
jemalloc = false

verbose-tests = true

# Enable full debuginfo for libstd.
debuginfo-level-std = 2

[dist]

[target.x86_64-pc-windows-msvc]
cc = "C:/src/llvm-prebuilt/clang/windows-amd64/bin/clang-cl.exe"
cxx = "C:/src/llvm-prebuilt/clang/windows-amd64/bin/clang-cl.exe"
ar = "C:/src/llvm-prebuilt/clang/windows-amd64/bin/llvm-lib.exe"
ranlib = "C:/src/llvm-prebuilt/clang/windows-amd64/bin/llvm-ranlib.exe"
linker = "C:/src/llvm-prebuilt/clang/windows-amd64/bin/lld-link.exe"
profiler = true
llvm-has-rust-patches = true

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-windowsOperating system: WindowsP-criticalCritical priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions