Skip to content
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

Cargo build --release and cargo install use different features #7302

Closed
nmattia opened this issue Aug 26, 2019 · 4 comments
Closed

Cargo build --release and cargo install use different features #7302

nmattia opened this issue Aug 26, 2019 · 4 comments
Labels
C-bug Category: bug

Comments

@nmattia
Copy link
Contributor

nmattia commented Aug 26, 2019

Problem

The command cargo build --release uses different features than cargo install.

Steps

  1. go in this directory
  2. cargo build --release
   Compiling lazy_static v1.3.0
   Compiling thread_local v0.3.6
   Compiling regex v1.1.8
  ...
  1. cargo install
   Compiling lazy_static v1.3.0
   Compiling thread_local v0.3.6
   Compiling regex v1.1.8
...

Notes

Output of cargo version:

$ ./target/debug/cargo  --version
cargo 1.37.0
$ git rev-parse HEAD
c3ee836e92eed0db7f403ff9954ede138cd57082

I've added some debug logs to compute_metadata, in particular when hashing in the features:

diff --git a/src/cargo/core/compiler/context/compilation_files.rs b/src/cargo/core/compiler/context/compilation_files.rs
index ed3232ea..f63ca6be 100644
--- a/src/cargo/core/compiler/context/compilation_files.rs
+++ b/src/cargo/core/compiler/context/compilation_files.rs
...
@@ -525,12 +531,16 @@ fn compute_metadata<'a, 'cfg>(
         .stable_hash(bcx.ws.root())
         .hash(&mut hasher);
 
 ...
     // Also mix in enabled features to our metadata. This'll ensure that
     // when changing feature sets each lib is separately cached.
     bcx.resolve
         .features_sorted(unit.pkg.package_id())
         .hash(&mut hasher);
 
+    info!("{}: Features: {:#?}", tname, bcx.resolve.features_sorted(unit.pkg.package_id()));
+
     // Mix in the target-metadata of all the dependencies of this target.
     {
         let mut deps_metadata = cx

The cargo build --release gives:

[2019-08-26T19:18:50Z INFO  cargo::core::compiler::context::compilation_files] lazy_static: Features: [
    "spin",
    "spin_no_std",
]

and cargo install gives:

[2019-08-26T19:19:21Z INFO  cargo::core::compiler::context::compilation_files] lazy_static: Features: []
@nmattia nmattia added the C-bug Category: bug label Aug 26, 2019
@ehuss
Copy link
Contributor

ehuss commented Aug 26, 2019

This is caused by cargo install ignoring the Cargo.lock file by default. If you pass --locked, you should get the same behavior. Or run cargo update to use the most current dependencies.

@nmattia
Copy link
Contributor Author

nmattia commented Aug 26, 2019

Unfortunately that doesn't seem to be the issue:

$  cargo build  --release -j 8
   Compiling bar v0.1.0 (/tmp/nix-build-bar-and-others.drv-0/workspace/bar)
   Compiling foo v0.1.0 (/tmp/nix-build-bar-and-others.drv-0/workspace/foo)
    Finished release [optimized] target(s) in 0.51s
running tests
$  cargo test --release
   Compiling bar v0.1.0 (/tmp/nix-build-bar-and-others.drv-0/workspace/bar)
   Compiling foo v0.1.0 (/tmp/nix-build-bar-and-others.drv-0/workspace/foo)
    Finished release [optimized] target(s) in 0.52s
     Running target/release/deps/bar-07370ca6dcd302b1

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/release/deps/foo-95cd32c8408c7a13

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

$ cargo install --locked --path bar --bins --root /nix/store/56wbjv24nifdp3ki61mpl0yy80qs0dch-bar-and-others
  Installing bar v0.1.0 (/tmp/nix-build-bar-and-others.drv-0/workspace/bar)
   Compiling lazy_static v1.3.0
   Compiling thread_local v0.3.6
   Compiling regex v1.1.8
   Compiling bar v0.1.0 (/tmp/nix-build-bar-and-others.drv-0/workspace/bar)
    Finished release [optimized] target(s) in 10.98s

@ehuss
Copy link
Contributor

ehuss commented Aug 26, 2019

Ah, I didn't realize you were comparing to cargo build in the root of the workspace. This happens because foo depends on rand > getrandom > lazy_static, and getrandom enables the spin_no_std feature of lazy_static for the uefi target. This is a known issue where unused targets activate features during feature unification (tracked in issues like #1197 and #2524).

Newer versions of getrandom do not specify its dependencies that way, so that may help.

@nmattia
Copy link
Contributor Author

nmattia commented Aug 28, 2019

You're right, that seems to be the issue. cargo build -p bar ... doesn't help, but pushd bar; cargo build ... does!

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

No branches or pull requests

2 participants