autocfg icon indicating copy to clipboard operation
autocfg copied to clipboard

Fails to detect crate std on NixOS

Open shimunn opened this issue 1 year ago • 7 comments

Hi,

I've just ran into an issue with autocfg when trying to use zbus which depends on autocfg in my project. I'm developing on NixOS which has an bit of an unusual directory structure, which may lead autocfg astray. Which is why indexmap is led to belive it's being built in a nostd environment.

Is there any way to make autocfg belive that an certain crate is available or some other workaround?

shimunn avatar Feb 01 '23 17:02 shimunn

For indexmap in particular, it also has a cargo feature "std" that bypasses the autocfg check. If you add that dependency to your project with the feature enabled, cargo will unify features with the rest of your dependency tree.

But autocfg is just running rustc, and the expectation is that std should be found in the sysroot. What does NixOS do differently there? If you manually run rustc on a simple fn main() {}, does that find std?

cuviper avatar Feb 01 '23 18:02 cuviper

For indexmap in particular, it also has a cargo feature "std" that bypasses the autocfg check. If you add that dependency to your project with the feature enabled, cargo will unify features with the rest of your dependency tree.

That's a fix but not an solution.

But autocfg is just running rustc, and the expectation is that std should be found in the sysroot. What does NixOS do differently there? If you manually run rustc on a simple fn main() {}, does that find std?

Yes I can compile using lain rustc just fine.

shimunn avatar Feb 03 '23 08:02 shimunn

Is there a wrapper or something that sets the sysroot or library search path? I'm not sure what's missing here...

cuviper avatar Feb 03 '23 18:02 cuviper

Seems to be my dev environment, with an shell(nix shell nixpkgs#cargo nixpkgs#rustc) just containing cargo & rustc everything works as expected.

Could you point out the env variables I would have to check to get my dev environment working once again? My sysroot looks fine in my eyes:

rustc --print sysroot
/nix/store/l49zzww7wl3qxnvx61jbcb5kwpcp31ln-rust-nightly-complete-2023-02-06

tree /nix/store/l49zzww7wl3qxnvx61jbcb5kwpcp31ln-rust-nightly-complete-2023-02-06 -L 2
/nix/store/l49zzww7wl3qxnvx61jbcb5kwpcp31ln-rust-nightly-complete-2023-02-06
├── bin
│   ├── cargo
│   ├── cargo-clippy
│   ├── cargo-fmt
│   ├── cargo-miri
│   ├── clippy-driver
│   ├── miri
│   ├── rls
│   ├── rust-analyzer
│   ├── rustc
│   ├── rustdoc
│   ├── rustfmt
│   ├── rust-gdb
│   ├── rust-gdbgui
│   └── rust-lldb
├── etc
│   └── bash_completion.d
├── lib
│   ├── libLLVM-15-rust-1.69.0-nightly.so -> /nix/store/hd29v2rlzvhi1snx0178475s0ffb2x9j-rustc-nightly-complete-2023-02-06/lib/libLLVM-15-rust-1.69.0-nightly.so
│   ├── librustc_driver-0d5965989a9dd9e8.so
│   ├── libstd-6e3768d17ed5c91f.so -> /nix/store/hd29v2rlzvhi1snx0178475s0ffb2x9j-rustc-nightly-complete-2023-02-06/lib/libstd-6e3768d17ed5c91f.so
│   ├── libtest-5df56ae6cfa4ea18.so -> /nix/store/hd29v2rlzvhi1snx0178475s0ffb2x9j-rustc-nightly-complete-2023-02-06/lib/libtest-5df56ae6cfa4ea18.so
│   └── rustlib
├── libexec
│   ├── cargo-credential-1password -> /nix/store/jpwjibja78sy7wq99l232ynm34hxka11-cargo-nightly-complete-2023-02-06/libexec/cargo-credential-1password
│   └── rust-analyzer-proc-macro-srv -> /nix/store/hd29v2rlzvhi1snx0178475s0ffb2x9j-rustc-nightly-complete-2023-02-06/libexec/rust-analyzer-proc-macro-srv
└── share
    ├── doc
    ├── man
    └── zsh

echo $RUST_SRC_PATH
/nix/store/l49zzww7wl3qxnvx61jbcb5kwpcp31ln-rust-nightly-complete-2023-02-06/lib/rustlib

# running autocfg tests
cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.01s
     Running unittests src/lib.rs (target/debug/deps/autocfg-7098375cc8c07875)
error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error: aborting due to previous error
error: aborting due to previous error
error: aborting due to previous error


error[E0463]: can't find crate for `std`


error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.

For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `std`
error: aborting due to previous error
error: aborting due to previous error



For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `std`
error: aborting due to previous error


For more information about this error, try `rustc --explain E0463`.
error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `core`

error[E0463]: can't find crate for `core`

error[E0463]: can't find crate for `core`
error[E0463]: can't find crate for `core`
error[E0463]: can't find crate for `core`
error[E0463]: can't find crate for `core`



error[E0463]: can't find crate for `compiler_builtins`


error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `core`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `core`

error[E0463]: can't find crate for `core`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `core`

error: aborting due to 2 previous errors
error: aborting due to 2 previous errors
error: aborting due to 2 previous errors
error: aborting due to 2 previous errors




error: aborting due to 2 previous errors
error: aborting due to 2 previous errors
error: aborting due to 2 previous errors



For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.error[E0463]: can't find crate for `compiler_builtins`


For more information about this error, try `rustc --explain E0463`.For more information about this error, try `rustc --explain E0463`.For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.


error: aborting due to 2 previous errors
error: aborting due to 2 previous errors


For more information about this error, try `rustc --explain E0463`.For more information about this error, try `rustc --explain E0463`.

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `core`

error[E0463]: can't find crate for `compiler_builtins`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
warning: autocfg could not probe for `std`
error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`
error[E0463]: can't find crate for `std`


error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `doesnt_exist`
 --> <anon>:1:1
  |
1 | extern crate doesnt_exist as probe;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`
 --> <anon>:1:1
  |
1 | extern crate std as probe;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to previous error


error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.For more information about this error, try `rustc --explain E0463`.

error: requires `sized` lang_item

error[E0463]: can't find crate for `std`

error[E0463]: can't find crate for `std`

error: aborting due to 2 previous errors
error: aborting due to 2 previous errors
error: aborting due to previous error



For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
error: aborting due to previous error
error[E0463]: can't find crate for `std`


For more information about this error, try `rustc --explain E0463`.
error: requires `sized` lang_item

error[E0463]: can't find crate for `alloc`
 --> <anon>:1:1
  |
1 | extern crate alloc as probe;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
error: test failed, to rerun pass `--lib`

unsetting RUST_SRC_PATH doesn't change anything though.

shimunn avatar Feb 06 '23 21:02 shimunn

Did you truncate that tree at rustlib, or is that empty? The $sysroot/lib is used at runtime, but for builds rustc will look in $sysroot/lib/rustlib/$target/lib by default.

As for environment, RUSTFLAGS would be the most obvious, but that should be supported. RUST_WRAPPER would be my next bet, and that is not supported yet -- #26, but I do have a local branch for that. Beyond that, I don't know, check for anything with RUST or CARGO in the name.

cuviper avatar Feb 07 '23 22:02 cuviper

rustc will look in $sysroot/lib/rustlib/$target/lib by default.

I just remembered there's a concise command to find this, rustc --print target-libdir.

cuviper avatar Feb 08 '23 20:02 cuviper

If you use toolchains shipped by rust-overlay, this should be fixed with https://github.com/oxalica/rust-overlay/pull/134.

zhaofengli avatar Aug 10 '23 18:08 zhaofengli