Skip to content

unused [patch] in $CARGO_HOME/config.toml failed loading sysroot metadata #19667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
weihanglo opened this issue Apr 23, 2025 · 19 comments
Open
Labels
A-cargo cargo related issues C-bug Category: bug

Comments

@weihanglo
Copy link
Member

How did it happen?

The cargo metadata invocation for querying libstd metadata fired with the --locked flag. However, when cargo attempted to write a lockfile because there were some unused [patch] entries under CARGO_HOME/config.toml, the command failed due to the presence of the --locked flag.

Code snippet to reproduce

export CARGO_HOME=/tmp/ra-bug/cargo-home

cargo new --lib /tmp/ra-bug/foo
cd /tmp/ra-bug/foo
cargo add empty-library
cat <<EOF > src/lib.rs
fn f() {
    let x = Some(1);
}
EOF

cargo new --lib /tmp/ra-bug/empty-library
cat <<EOF > /tmp/ra-bug/empty-library/Cargo.toml
[package]
name = "empty-library"
version = "1.0.0"
edition = "2015"
EOF

cat <<EOF > "$CARGO_HOME/config.toml"
[patch.crates-io]
empty-library = { path = "/tmp/ra-bug/empty-library" }
EOF

And the open any editor and hover on x
It'll show the following hint:

let x: {unknown}
───────────────────────────────────────────────────
Implements notable traits: Read, Write
───────────────────────────────────────────────────
does not contain types with destructors (drop glue)

Possible solutions

Maybe Cargo can have an option to stop writing unused patches? I am not sure if such an option is useful though.

We can perhaps leverage the unstable --lockfile-path flag. And write it to somewhere else so we won't modify the original one. Something like (though I don't like it):

diff --git a/crates/project-model/src/cargo_workspace.rs b/crates/project-model/src/cargo_workspace.rs
index 6e730b1aea..148b2a4c2d 100644
--- a/crates/project-model/src/cargo_workspace.rs
+++ b/crates/project-model/src/cargo_workspace.rs
@@ -389,7 +389,9 @@ impl CargoWorkspace {
             other_options.push("-Zscript".to_owned());
         }
         if locked {
-            other_options.push("--locked".to_owned());
+            other_options.push("-Zunstable-options".into());
+            other_options.push("--lockfile-path".into());
+            other_options.push("/tmp/rust-analyzer-hack/Cargo.lock".into());
         }
         if no_deps {
             other_options.push("--no-deps".to_owned());

Version information

rust-analyzer version: 4e4aee4

rustc version: 1.86.0

relevant settings: all defaults

Logs

The fallback to cargo metadata --no-deps succeeded, but didn't really help. See the log

Logs of env RA_LOG=project_model::sysroot=debug nvim

[START]LSP logging initiated
DEBUG Discovering sysroot by cd "/home/user/foo" && "/home/user/.nix-profile/bin/rustc" "--print" "sysroot"\n'
DEBUG checking sysroot library: /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library\n2025-04-23T05:44:46.798914-04:00  INFO Using sysroot workspace=/home/user/foo/Cargo.toml src_root=Some(AbsPath("/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library")) root=Some(AbsPath("/home/user/.rustup/toolchains/stable-aarch64-apple-darwin"))\n'
DEBUG Loading library metadata: /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml\n"
DEBUG Discovering cargo config env by cd "/home/user/foo" && RUSTC_BOOTSTRAP="1" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "-Z" "unstable-options" "config" "get" "env"\n'
 WARN rustc_cfg::get: failed to run `cd "/home/user/foo" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "rustc" "-Z" "unstable-options" "--print" "cfg" "--target" "aarch64-apple-darwin" "--" "-O"`, falling back to invoking rustc directly e=cd "/home/user/foo" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "rustc" "-Z" "unstable-options" "--print" "cfg" "--target" "aarch64-apple-darwin" "--" "-O" failed, exit status: 101\nstderr:\nerror: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel\nSee https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information about Rust release channels.\n\n'
DEBUG rustc_cfg::get: rustc cfgs found rustc_cfgs=[KeyValue { key: "panic", value: "unwind" }, KeyValue { key: "target_abi", value: "" }, KeyValue { key: "target_arch", value: "aarch64" }, KeyValue { key: "target_endian", value: "little" }, KeyValue { key: "target_env", value: "" }, KeyValue { key: "target_family", value: "unix" }, KeyValue { key: "target_feature", value: "aes" }, KeyValue { key: "target_feature", value: "crc" }, KeyValue { key: "target_feature", value: "dit" }, KeyValue { key: "target_feature", value: "dotprod" }, KeyValue { key: "target_feature", value: "dpb" }, KeyValue { key: "target_feature", value: "dpb2" }, KeyValue { key: "target_feature", value: "fcma" }, KeyValue { key: "target_feature", value: "fhm" }, KeyValue { key: "target_feature", value: "flagm" }, KeyValue { key: "target_feature", value: "fp16" }, KeyValue { key: "target_feature", value: "frintts" }, KeyValue { key: "target_feature", value: "jsconv" }, KeyValue { key: "target_feature", value: "lor" }, KeyValue { key: "target_feature", value: "lse" }, KeyValue { key: "target_feature", value: "neon" }, KeyValue { key: "target_feature", value: "paca" }, KeyValue { key: "target_feature", value: "pacg" }, KeyValue { key: "target_feature", value: "pan" }, KeyValue { key: "target_feature", value: "pmuv3" }, KeyValue { key: "target_feature", value: "ras" }, KeyValue { key: "target_feature", value: "rcpc" }, KeyValue { key: "target_feature", value: "rcpc2" }, KeyValue { key: "target_feature", value: "rdm" }, KeyValue { key: "target_feature", value: "sb" }, KeyValue { key: "target_feature", value: "sha2" }, KeyValue { key: "target_feature", value: "sha3" }, KeyValue { key: "target_feature", value: "ssbs" }, KeyValue { key: "target_feature", value: "vh" }, KeyValue { key: "target_has_atomic", value: "128" }, KeyValue { key: "target_has_atomic", value: "16" }, KeyValue { key: "target_has_atomic", value: "32" }, KeyValue { key: "target_has_atomic", value: "64" }, KeyValue { key: "target_has_atomic", value: "8" }, KeyValue { key: "target_has_atomic", value: "ptr" }, KeyValue { key: "target_os", value: "macos" }, KeyValue { key: "target_pointer_width", value: "64" }, KeyValue { key: "target_vendor", value: "apple" }, Flag("unix"), KeyValue { key: "target_has_atomic_equal_alignment", value: "8" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "16" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "32" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "64" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "128" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "ptr" }, Flag("target_has_atomic_load_store"), KeyValue { key: "target_has_atomic_load_store", value: "8" }, KeyValue { key: "target_has_atomic_load_store", value: "16" }, KeyValue { key: "target_has_atomic_load_store", value: "32" }, KeyValue { key: "target_has_atomic_load_store", value: "64" }, KeyValue { key: "target_has_atomic_load_store", value: "128" }, KeyValue { key: "target_has_atomic_load_store", value: "ptr" }, Flag("target_thread_local"), Flag("target_has_atomic")]\n'
DEBUG Failed to discover cargo config env: cd "/home/user/foo" && RUSTC_BOOTSTRAP="1" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "-Z" "unstable-options" "config" "get" "env" failed, exit status: 101\nstderr:\nerror: config value `env` is not set\n\n\nStack backtrace:\n   0: std::backtrace::Backtrace::create\n   1: anyhow::error::<impl anyhow::Error>::msg\n   2: project_model::utf8_stdout\n   3: project_model::env::cargo_config_env\n   4: std::sys::backtrace::__rust_begin_short_backtrace\n   5: core::ops::function::FnOnce::call_once{{vtable.shim}}\n   6: std::sys::pal::unix::thread::Thread::new::thread_start\n   7: __pthread_deallocate\n'
 WARN `cargo metadata` failed, but retry with `--no-deps` succeeded cargo_toml=/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml e=Failed to run `cd "/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" "/home/user/.cargo/bin/cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml" "--filter-platform" "aarch64-apple-darwin" "--locked"`\n\nCaused by:\n    `cargo metadata` exited with an error: warning: Patch `empty-library v1.0.0 (/tmp/empty-library)` was not used in the crate graph.\n    Check that the patched package version and available features are compatible\n    with the dependency requirements. If the patch has a different version from\n    what is locked in the Cargo.lock file, run `cargo update` to use the new\n    version. This may also occur with an optional dependency that is not enabled.\n    error: the lock file /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.lock needs to be updated but --locked was passed to prevent this\n    If you want to try to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.\n    \n\nStack backtrace:\n   0: std::backtrace::Backtrace::create\n   1: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from\n   2: project_model::cargo_workspace::CargoWorkspace::fetch_metadata_\n   3: project_model::cargo_workspace::CargoWorkspace::fetch_metadata\n   4: project_model::sysroot::Sysroot::load_workspace\n   5: std::sys::backtrace::__rust_begin_short_backtrace\n   6: core::ops::function::FnOnce::call_once{{vtable.shim}}\n   7: std::sys::pal::unix::thread::Thread::new::thread_start\n   8: __pthread_deallocate\n'
DEBUG Discovering sysroot by cd "/home/user/foo" && "/home/user/.nix-profile/bin/rustc" "--print" "sysroot"\n'
DEBUG checking sysroot library: /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library\n2025-04-23T05:44:47.147551-04:00  INFO Using sysroot workspace=/home/user/foo/Cargo.toml src_root=Some(AbsPath("/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library")) root=Some(AbsPath("/home/user/.rustup/toolchains/stable-aarch64-apple-darwin"))\n'
DEBUG Loading library metadata: /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml\n2025-04-23T05:44:47.192894-04:00 DEBUG Discovering cargo config env by cd "/home/user/foo" && RUSTC_BOOTSTRAP="1" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "-Z" "unstable-options" "config" "get" "env"\n'
DEBUG Failed to discover cargo config env: cd "/home/user/foo" && RUSTC_BOOTSTRAP="1" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "-Z" "unstable-options" "config" "get" "env" failed, exit status: 101\nstderr:\nerror: config value `env` is not set\n\n\nStack backtrace:\n   0: std::backtrace::Backtrace::create\n   1: anyhow::error::<impl anyhow::Error>::msg\n   2: project_model::utf8_stdout\n   3: project_model::env::cargo_config_env\n   4: std::sys::backtrace::__rust_begin_short_backtrace\n   5: core::ops::function::FnOnce::call_once{{vtable.shim}}\n   6: std::sys::pal::unix::thread::Thread::new::thread_start\n   7: __pthread_deallocate\n'
 WARN rustc_cfg::get: failed to run `cd "/home/user/foo" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "rustc" "-Z" "unstable-options" "--print" "cfg" "--target" "aarch64-apple-darwin" "--" "-O"`, falling back to invoking rustc directly e=cd "/home/user/foo" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "rustc" "-Z" "unstable-options" "--print" "cfg" "--target" "aarch64-apple-darwin" "--" "-O" failed, exit status: 101\nstderr:\nerror: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel\nSee https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information about Rust release channels.\n\n'
DEBUG rustc_cfg::get: rustc cfgs found rustc_cfgs=[KeyValue { key: "panic", value: "unwind" }, KeyValue { key: "target_abi", value: "" }, KeyValue { key: "target_arch", value: "aarch64" }, KeyValue { key: "target_endian", value: "little" }, KeyValue { key: "target_env", value: "" }, KeyValue { key: "target_family", value: "unix" }, KeyValue { key: "target_feature", value: "aes" }, KeyValue { key: "target_feature", value: "crc" }, KeyValue { key: "target_feature", value: "dit" }, KeyValue { key: "target_feature", value: "dotprod" }, KeyValue { key: "target_feature", value: "dpb" }, KeyValue { key: "target_feature", value: "dpb2" }, KeyValue { key: "target_feature", value: "fcma" }, KeyValue { key: "target_feature", value: "fhm" }, KeyValue { key: "target_feature", value: "flagm" }, KeyValue { key: "target_feature", value: "fp16" }, KeyValue { key: "target_feature", value: "frintts" }, KeyValue { key: "target_feature", value: "jsconv" }, KeyValue { key: "target_feature", value: "lor" }, KeyValue { key: "target_feature", value: "lse" }, KeyValue { key: "target_feature", value: "neon" }, KeyValue { key: "target_feature", value: "paca" }, KeyValue { key: "target_feature", value: "pacg" }, KeyValue { key: "target_feature", value: "pan" }, KeyValue { key: "target_feature", value: "pmuv3" }, KeyValue { key: "target_feature", value: "ras" }, KeyValue { key: "target_feature", value: "rcpc" }, KeyValue { key: "target_feature", value: "rcpc2" }, KeyValue { key: "target_feature", value: "rdm" }, KeyValue { key: "target_feature", value: "sb" }, KeyValue { key: "target_feature", value: "sha2" }, KeyValue { key: "target_feature", value: "sha3" }, KeyValue { key: "target_feature", value: "ssbs" }, KeyValue { key: "target_feature", value: "vh" }, KeyValue { key: "target_has_atomic", value: "128" }, KeyValue { key: "target_has_atomic", value: "16" }, KeyValue { key: "target_has_atomic", value: "32" }, KeyValue { key: "target_has_atomic", value: "64" }, KeyValue { key: "target_has_atomic", value: "8" }, KeyValue { key: "target_has_atomic", value: "ptr" }, KeyValue { key: "target_os", value: "macos" }, KeyValue { key: "target_pointer_width", value: "64" }, KeyValue { key: "target_vendor", value: "apple" }, Flag("unix"), KeyValue { key: "target_has_atomic_equal_alignment", value: "8" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "16" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "32" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "64" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "128" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "ptr" }, Flag("target_has_atomic_load_store"), KeyValue { key: "target_has_atomic_load_store", value: "8" }, KeyValue { key: "target_has_atomic_load_store", value: "16" }, KeyValue { key: "target_has_atomic_load_store", value: "32" }, KeyValue { key: "target_has_atomic_load_store", value: "64" }, KeyValue { key: "target_has_atomic_load_store", value: "128" }, KeyValue { key: "target_has_atomic_load_store", value: "ptr" }, Flag("target_thread_local"), Flag("target_has_atomic")]\n'
 WARN `cargo metadata` failed, but retry with `--no-deps` succeeded cargo_toml=/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml e=Failed to run `cd "/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" "/home/user/.cargo/bin/cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml" "--filter-platform" "aarch64-apple-darwin" "--locked"`\n\nCaused by:\n    `cargo metadata` exited with an error: warning: Patch `empty-library v1.0.0 (/tmp/empty-library)` was not used in the crate graph.\n    Check that the patched package version and available features are compatible\n    with the dependency requirements. If the patch has a different version from\n    what is locked in the Cargo.lock file, run `cargo update` to use the new\n    version. This may also occur with an optional dependency that is not enabled.\n    error: the lock file /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.lock needs to be updated but --locked was passed to prevent this\n    If you want to try to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.\n    \n\nStack backtrace:\n   0: std::backtrace::Backtrace::create\n   1: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from\n   2: project_model::cargo_workspace::CargoWorkspace::fetch_metadata_\n   3: project_model::cargo_workspace::CargoWorkspace::fetch_metadata\n   4: project_model::sysroot::Sysroot::load_workspace\n   5: std::sys::backtrace::__rust_begin_short_backtrace\n   6: core::ops::function::FnOnce::call_once{{vtable.shim}}\n   7: std::sys::pal::unix::thread::Thread::new::thread_start\n   8: __pthread_deallocate\n'
DEBUG Discovering sysroot by cd "/home/user/foo" && "/home/user/.nix-profile/bin/rustc" "--print" "sysroot"\n'
DEBUG checking sysroot library: /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library\n"
 INFO Using sysroot workspace=/home/user/foo/Cargo.toml src_root=Some(AbsPath("/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library")) root=Some(AbsPath("/home/user/.rustup/toolchains/stable-aarch64-apple-darwin"))\n'
DEBUG Loading library metadata: /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml\n2025-04-23T05:44:47.501683-04:00 DEBUG Discovering cargo config env by cd "/home/user/foo" && RUSTC_BOOTSTRAP="1" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "-Z" "unstable-options" "config" "get" "env"\n'
 WARN rustc_cfg::get: failed to run `cd "/home/user/foo" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "rustc" "-Z" "unstable-options" "--print" "cfg" "--target" "aarch64-apple-darwin" "--" "-O"`, falling back to invoking rustc directly e=cd "/home/user/foo" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "rustc" "-Z" "unstable-options" "--print" "cfg" "--target" "aarch64-apple-darwin" "--" "-O" failed, exit status: 101\nstderr:\nerror: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel\nSee https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information about Rust release channels.\n\n'
DEBUG Failed to discover cargo config env: cd "/home/user/foo" && RUSTC_BOOTSTRAP="1" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "-Z" "unstable-options" "config" "get" "env" failed, exit status: 101\nstderr:\nerror: config value `env` is not set\n\n\nStack backtrace:\n   0: std::backtrace::Backtrace::create\n   1: anyhow::error::<impl anyhow::Error>::msg\n   2: project_model::utf8_stdout\n   3: project_model::env::cargo_config_env\n   4: std::sys::backtrace::__rust_begin_short_backtrace\n   5: core::ops::function::FnOnce::call_once{{vtable.shim}}\n   6: std::sys::pal::unix::thread::Thread::new::thread_start\n   7: __pthread_deallocate\n'
DEBUG rustc_cfg::get: rustc cfgs found rustc_cfgs=[KeyValue { key: "panic", value: "unwind" }, KeyValue { key: "target_abi", value: "" }, KeyValue { key: "target_arch", value: "aarch64" }, KeyValue { key: "target_endian", value: "little" }, KeyValue { key: "target_env", value: "" }, KeyValue { key: "target_family", value: "unix" }, KeyValue { key: "target_feature", value: "aes" }, KeyValue { key: "target_feature", value: "crc" }, KeyValue { key: "target_feature", value: "dit" }, KeyValue { key: "target_feature", value: "dotprod" }, KeyValue { key: "target_feature", value: "dpb" }, KeyValue { key: "target_feature", value: "dpb2" }, KeyValue { key: "target_feature", value: "fcma" }, KeyValue { key: "target_feature", value: "fhm" }, KeyValue { key: "target_feature", value: "flagm" }, KeyValue { key: "target_feature", value: "fp16" }, KeyValue { key: "target_feature", value: "frintts" }, KeyValue { key: "target_feature", value: "jsconv" }, KeyValue { key: "target_feature", value: "lor" }, KeyValue { key: "target_feature", value: "lse" }, KeyValue { key: "target_feature", value: "neon" }, KeyValue { key: "target_feature", value: "paca" }, KeyValue { key: "target_feature", value: "pacg" }, KeyValue { key: "target_feature", value: "pan" }, KeyValue { key: "target_feature", value: "pmuv3" }, KeyValue { key: "target_feature", value: "ras" }, KeyValue { key: "target_feature", value: "rcpc" }, KeyValue { key: "target_feature", value: "rcpc2" }, KeyValue { key: "target_feature", value: "rdm" }, KeyValue { key: "target_feature", value: "sb" }, KeyValue { key: "target_feature", value: "sha2" }, KeyValue { key: "target_feature", value: "sha3" }, KeyValue { key: "target_feature", value: "ssbs" }, KeyValue { key: "target_feature", value: "vh" }, KeyValue { key: "target_has_atomic", value: "128" }, KeyValue { key: "target_has_atomic", value: "16" }, KeyValue { key: "target_has_atomic", value: "32" }, KeyValue { key: "target_has_atomic", value: "64" }, KeyValue { key: "target_has_atomic", value: "8" }, KeyValue { key: "target_has_atomic", value: "ptr" }, KeyValue { key: "target_os", value: "macos" }, KeyValue { key: "target_pointer_width", value: "64" }, KeyValue { key: "target_vendor", value: "apple" }, Flag("unix"), KeyValue { key: "target_has_atomic_equal_alignment", value: "8" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "16" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "32" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "64" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "128" }, KeyValue { key: "target_has_atomic_equal_alignment", value: "ptr" }, Flag("target_has_atomic_load_store"), KeyValue { key: "target_has_atomic_load_store", value: "8" }, KeyValue { key: "target_has_atomic_load_store", value: "16" }, KeyValue { key: "target_has_atomic_load_store", value: "32" }, KeyValue { key: "target_has_atomic_load_store", value: "64" }, KeyValue { key: "target_has_atomic_load_store", value: "128" }, KeyValue { key: "target_has_atomic_load_store", value: "ptr" }, Flag("target_thread_local"), Flag("target_has_atomic")]\n'
 WARN `cargo metadata` failed, but retry with `--no-deps` succeeded cargo_toml=/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml e=Failed to run `cd "/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library" && RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" "/home/user/.cargo/bin/cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.toml" "--filter-platform" "aarch64-apple-darwin" "--locked"`\n\nCaused by:\n    `cargo metadata` exited with an error: warning: Patch `empty-library v1.0.0 (/tmp/empty-library)` was not used in the crate graph.\n    Check that the patched package version and available features are compatible\n    with the dependency requirements. If the patch has a different version from\n    what is locked in the Cargo.lock file, run `cargo update` to use the new\n    version. This may also occur with an optional dependency that is not enabled.\n    error: the lock file /home/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/Cargo.lock needs to be updated but --locked was passed to prevent this\n    If you want to try to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.\n    \n\nStack backtrace:\n   0: std::backtrace::Backtrace::create\n   1: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from\n   2: project_model::cargo_workspace::CargoWorkspace::fetch_metadata_\n   3: project_model::cargo_workspace::CargoWorkspace::fetch_metadata\n   4: project_model::sysroot::Sysroot::load_workspace\n   5: std::sys::backtrace::__rust_begin_short_backtrace\n   6: core::ops::function::FnOnce::call_once{{vtable.shim}}\n   7: std::sys::pal::unix::thread::Thread::new::thread_start\n   8: __pthread_deallocate\n'
 INFO Running build scripts: cd "/home/user/foo" && RA_RUSTC_WRAPPER="1" RUSTC_WRAPPER="/home/user/rust-analyzer/target/release/rust-analyzer" RUSTUP_TOOLCHAIN="/home/user/.rustup/toolchains/stable-aarch64-apple-darwin" "/home/user/.cargo/bin/cargo" "check" "--quiet" "--workspace" "--message-format=json" "--manifest-path" "/home/user/foo/Cargo.toml" "--all-targets" "--keep-going"\n'

@weihanglo weihanglo added the C-bug Category: bug label Apr 23, 2025
@Veykril Veykril added the A-cargo cargo related issues label Apr 23, 2025
@Veykril
Copy link
Member

Veykril commented Apr 23, 2025

Using --lockfile-path sounds reasonable to me. That makes our sysroot loading a bit more error resilient in general it seems. Do you know roughly when --lockfile-path was implemented? Mainly of interest so we know our MSRV for projects.

@weihanglo
Copy link
Member Author

1.82 unfortunately (see the nightly section). IIRC rust-analyzer's MSRV was just reverted to 1.78 from 1.82.

@Veykril
Copy link
Member

Veykril commented Apr 23, 2025

We should have the workspace's toolchain version at this point already so we can toggle this conditionally for the time being

@Veykril
Copy link
Member

Veykril commented Apr 23, 2025

The annoyance with --lockfile-path is the need for us to allocate a temp dir / file though :/

@Veykril
Copy link
Member

Veykril commented Apr 23, 2025

@weihanglo
Copy link
Member Author

cries in cargo metadata still not being a read-only operation

😂😂😂

Finally came back to bite me.

@weihanglo
Copy link
Member Author

Maybe Cargo should additionally allow --lockfile-path=/dev/null or something.

@bjorn3
Copy link
Member

bjorn3 commented Apr 24, 2025

Maybe the sysroot cargo metadata should be made to ignore ~/.cargo/config.toml? Does cargo have an option for that?

@arielb1
Copy link

arielb1 commented Apr 27, 2025

Would we want a cargo metadata --metadata-for-sysroot=<sysroot> that returns the metadata for a sysroot? The sysroot is already special. The sysroot is special since the sysroot's Cargo.lock has been created by a Cargo that is not our own, which violates the normal assumption that Cargo only works on a Cargo.lock that is the same as one that would have been created by it, but it's an assumption violation that we live with.

@arielb1
Copy link

arielb1 commented Apr 28, 2025

The other option would be for the sysroot to come bundled with a rust-project.json and have r-a load that.

@Veykril
Copy link
Member

Veykril commented Apr 28, 2025

The rust-project.json file layout isn't really stable though, I'd rather not have that be committed here.

@epage
Copy link

epage commented Apr 28, 2025

because there were some unused [patch] entries under CARGO_HOME/config.toml,

@weihanglo why is it that you have a patch in your user config? Doesn't that cause every lockfile you interact with to have an unused patches section? Focusing on the use case and its impact could likely lead us to either (1) provide a better alternative to user-wide patches or (2) focus in on the impact of unused user-wide patches generally and not just in this use case.

@weihanglo
Copy link
Member Author

weihanglo commented Apr 28, 2025

why is it that you have a patch in your user config?

The tool I am building leverages [patch] in config for overriding a dependency with a local copy shared between “Cargo workspaces”. You can see it as the tool one level above which interacts and manages multiple Cargo workspaces. The tool use CARGO_HOME to achieve that as @arielb1 has described.

However, the lockfile in every package doesn't care about whether the actual lockfile content is changing. But loading metadata for std workspace is an outlier because

  • it passes --locked
  • we don't really want to fetch std's dependencies or re-resolve std.

@arielb1
Copy link

arielb1 commented Apr 28, 2025

Really we use patching for both OSS dependencies that need to be patched for whatever reason, and also for local-but-out-of-workspace dependencies to allow easier local development when developing multiple workspaces that depend on each other.

The sysroot is special because it hasn't been compiled by our Cargo but instead the Cargo.lock has been generated by whichever built the sysroot.

@epage
Copy link

epage commented Apr 28, 2025

To summarize some clarifications I got from @weihanglo: When there are unused patches, a section gets added to all Cargo.lock files that are affected. Where unexpected, this could be problematic. When asking how this is dealt with, the answer is basically

  • They do their own locking for their internal projects so Cargo.lock is ignored
  • This is a special CARGO_HOME setup just for internal projects by their build tool so this does not negatively affect other projects that do care about their Cargo.lock

Now, looking back on when .cargo/config.tomls [patch] table was under discussion, Alex was concerned about the negative impact on global config (source). Jon had said this was for project-local config and not global config (source). Unsure if the custom CARGO_HOME didn't exist yet or if how it was being used was "local" in Jon's mind but I think there was a disconnect here and we are now dealing with the after affects of that. Alex even expressed concern over "misuse" of this feature (source) and my feeling is that we are in that territory.

Personally, I'd rather we not develop a one-off solution for what feels like an extreme corner case. I feel like we should evaluate unused patches in CARGO_HOME on a case-by-case basis to evaluate what the intended workflow should be. Take #15130 for example. Our options include

  • Have the user patch the source and install that
  • Create an install-only config and special handle its unused patches
  • Find a general solution to unused patches in the user config

@arielb1
Copy link

arielb1 commented Apr 28, 2025

It's "local" since every project has its own CARGO_HOME setup. The sysroot is special since it's both a part of every project, while also being resolved by a different Cargo.

If Cargo was the one compiling (and resolving the dependencies of) std as well, this problem would not have existed since there would have been a single lockfile.

OTOH, maybe today we have different tooling to handle the situation of "we have a fixed project P, and we want to see how it behaves as we vary the 'state of the rest of the world'". Setting CARGO_HOME to vary the state of the rest of the world sounds to me like a sane way to express it.

@arielb1
Copy link

arielb1 commented Apr 28, 2025

The other thing is that for the "vary the state of the rest of the world" use case, the unused patch entries in Cargo.lock don't make sense, so if it was possible to avoid creating them it would help as well.

Tho in that case, a patch that hits the sysroot would case things to fail, even tho conceptually the sysroot is "unpatchable" since it's not actually built per project.

@davidbarsky
Copy link
Contributor

davidbarsky commented Apr 29, 2025

The rust-project.json file layout isn't really stable though, I'd rather not have that be committed here.

For what it's worth, after I write up my long-promised issue about changing how rust-project.json declares dependencies, I think we could start versioning the format. Having used the an older version of the Cargo-wrapping tool in under discussion, I don't think it'd be a bad idea for said tool to use a rust-project.json. Granted, I'm also using with a Buckified sysroot, but having everything in terms of a rust-project.json simplifies things dramatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cargo cargo related issues C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

6 participants