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

default-features = false not working for dependency inside workspace. #8366

Closed
reitermarkus opened this issue Jun 16, 2020 · 9 comments
Closed
Labels
C-bug Category: bug

Comments

@reitermarkus
Copy link
Contributor

reitermarkus commented Jun 16, 2020

Problem

Setting default-features = false for a dependency of one member of a workspace on another workspace member does not work.

Steps

  1. Create the following workspace directory structure.
├── Cargo.toml
├── dep
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
└── lib
    ├── Cargo.toml
    └── src
        └── lib.rs

using

echo '[workspace]' > Cargo.toml
echo 'members = ["lib", "dep"]' >> Cargo.toml

cargo new --lib lib
echo 'dep = { path = "../dep", default-features = false }' >> lib/Cargo.toml

cargo new --lib dep
echo '[features]' >> dep/Cargo.toml
echo 'default = ["should_not_be_enabled"]' >> dep/Cargo.toml
echo 'should_not_be_enabled = []' >> dep/Cargo.toml
  1. Run cargo build -vv and see that dep is compiled with --cfg 'feature="default"' --cfg 'feature="should_not_be_enabled"'.

Possible Solution(s)

Notes

Output of cargo version:

cargo 1.44.0 (05d080faa 2020-05-06)

as well as

cargo 1.46.0-nightly (79c769c3d 2020-06-11)

The same thing also happens if dep is not a workspace member but a plain subdirectory.

Also, the problem does not happen when running cargo build -vv inside of lib after a cd lib.

@reitermarkus reitermarkus added the C-bug Category: bug label Jun 16, 2020
@ehuss
Copy link
Contributor

ehuss commented Jun 16, 2020

Features in a workspace are unified across all roots selected on the command-line. In your example, you have selected both dep and lib with the default features enabled, so it is built with default features.

You can use cargo build -p lib or change into the lib directory and run cargo build or use cargo build --no-default-features -Zpackage-features (see #5364).

The behavior of "workspace features unify based on what is being built" is tracked in #4463.

@reitermarkus
Copy link
Contributor Author

You're right, in the first case when just running cargo build it basically builds all workspace members, this can indeed be fixed by specifying a specific package using cargo build -p lib.

But this still leaves the case where dep is not a workspace member but just a subdirectory.

@ehuss
Copy link
Contributor

ehuss commented Jun 17, 2020

I'm not sure I understand your last comment. Using your example, the following does not select the default features for the dependency:

cargo new foo
cd foo

echo 'dep = { path = "dep", default-features = false }' >> Cargo.toml

cargo new --lib dep
echo '[features]' >> dep/Cargo.toml
echo 'default = ["should_not_be_enabled"]' >> dep/Cargo.toml
echo 'should_not_be_enabled = []' >> dep/Cargo.toml
cargo build -v

@dubiousjim
Copy link

I think he means that this still builds dep with --cfg 'feature="should_not_be_enabled"', and the justification about workspace members having their features unified doesn't apply:

echo '[workspace]' > Cargo.toml

# as in original instructions
# except "dep" is no longer a workspace member, merely a subdirectory
echo 'members = ["lib"]' >> Cargo.toml

cargo new --lib lib
echo 'dep = { path = "../dep", default-features = false }' >> lib/Cargo.toml

cargo new --lib dep
echo '[features]' >> dep/Cargo.toml
echo 'default = ["should_not_be_enabled"]' >> dep/Cargo.toml
echo 'should_not_be_enabled = []' >> dep/Cargo.toml

cargo build -v # issued in workspace root

@ehuss
Copy link
Contributor

ehuss commented Jun 22, 2020

Ah. Path dependencies are automatically added as workspace members, so it doesn't matter whether or not it is listed.

@dubiousjim
Copy link

Oh right.

@reitermarkus
Copy link
Contributor Author

Path dependencies are automatically added as workspace members

So is this working as intended? It is quite surprising that behaviour changes simply by switching to a path dependency for local debugging.

@ehuss
Copy link
Contributor

ehuss commented Jul 2, 2020

For now that is the intended behavior. I would recommend choosing the specific packages you want to build (with -p) if the unification is a problem.

@ehuss
Copy link
Contributor

ehuss commented Jul 29, 2020

Closing as this is the intended behavior for now. There are links above to issues tracking things like workspace unification.

@ehuss ehuss closed this as completed Jul 29, 2020
JohnAZoidberg added a commit to JohnAZoidberg/zune-image that referenced this issue Apr 27, 2023
Trying to build in an no-std doesn't work.
Example with zune-jpg and x86_64-unknown-uefi:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-core v0.2.13 (/home/zoid/clone/active/zune-image/zune-core)
error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-unknown-uefi` target may not support the standard library
  = note: `std` is required by `zune_core` because it does not declare `#![no_std]`
[...]
```

This seems to be due to a bug (or expected behavior) in the cargo
worktrees: rust-lang/cargo#8366

From zune-jpeg we can't disable the default features of zune-core.
But that's no problem, since zune-jpeg already enabled the std feature
in zune-core, whenver its std feature is enabled.

With this patch, compilation succeeds:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-jpeg v0.3.16 (/home/zoid/clone/active/zune-image/zune-jpeg)
warning: dropping unsupported crate type `cdylib` for target `x86_64-unknown-uefi`

warning: `zune-jpeg` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
```

Signed-off-by: Daniel Schaefer <[email protected]>
etemesi254 pushed a commit to etemesi254/zune-image that referenced this issue Apr 27, 2023
Trying to build in an no-std doesn't work.
Example with zune-jpg and x86_64-unknown-uefi:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-core v0.2.13 (/home/zoid/clone/active/zune-image/zune-core)
error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-unknown-uefi` target may not support the standard library
  = note: `std` is required by `zune_core` because it does not declare `#![no_std]`
[...]
```

This seems to be due to a bug (or expected behavior) in the cargo
worktrees: rust-lang/cargo#8366

From zune-jpeg we can't disable the default features of zune-core.
But that's no problem, since zune-jpeg already enabled the std feature
in zune-core, whenver its std feature is enabled.

With this patch, compilation succeeds:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-jpeg v0.3.16 (/home/zoid/clone/active/zune-image/zune-jpeg)
warning: dropping unsupported crate type `cdylib` for target `x86_64-unknown-uefi`

warning: `zune-jpeg` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
```

Signed-off-by: Daniel Schaefer <[email protected]>
etemesi254 pushed a commit to etemesi254/zune-image that referenced this issue Jun 11, 2023
Trying to build in an no-std doesn't work.
Example with zune-jpg and x86_64-unknown-uefi:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-core v0.2.13 (/home/zoid/clone/active/zune-image/zune-core)
error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-unknown-uefi` target may not support the standard library
  = note: `std` is required by `zune_core` because it does not declare `#![no_std]`
[...]
```

This seems to be due to a bug (or expected behavior) in the cargo
worktrees: rust-lang/cargo#8366

From zune-jpeg we can't disable the default features of zune-core.
But that's no problem, since zune-jpeg already enabled the std feature
in zune-core, whenver its std feature is enabled.

With this patch, compilation succeeds:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-jpeg v0.3.16 (/home/zoid/clone/active/zune-image/zune-jpeg)
warning: dropping unsupported crate type `cdylib` for target `x86_64-unknown-uefi`

warning: `zune-jpeg` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
```

Signed-off-by: Daniel Schaefer <[email protected]>
etemesi254 pushed a commit to etemesi254/zune-image that referenced this issue Oct 21, 2023
Trying to build in an no-std doesn't work.
Example with zune-jpg and x86_64-unknown-uefi:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-core v0.2.13 (/home/zoid/clone/active/zune-image/zune-core)
error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-unknown-uefi` target may not support the standard library
  = note: `std` is required by `zune_core` because it does not declare `#![no_std]`
[...]
```

This seems to be due to a bug (or expected behavior) in the cargo
worktrees: rust-lang/cargo#8366

From zune-jpeg we can't disable the default features of zune-core.
But that's no problem, since zune-jpeg already enabled the std feature
in zune-core, whenver its std feature is enabled.

With this patch, compilation succeeds:

```
> cargo build -p zune-jpeg --target x86_64-unknown-uefi --no-default-features
   Compiling zune-jpeg v0.3.16 (/home/zoid/clone/active/zune-image/zune-jpeg)
warning: dropping unsupported crate type `cdylib` for target `x86_64-unknown-uefi`

warning: `zune-jpeg` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
```

Signed-off-by: Daniel Schaefer <[email protected]>
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

3 participants