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

Optional dependency is not omitted at package building. #10604

Closed
meltedblocks opened this issue Apr 27, 2022 · 6 comments
Closed

Optional dependency is not omitted at package building. #10604

meltedblocks opened this issue Apr 27, 2022 · 6 comments
Labels
A-dependency-resolution Area: dependency resolution and the resolver C-bug Category: bug

Comments

@meltedblocks
Copy link

Problem

I'm building a library that needs to work with two versions of dependencies:

  • version 1.9
  • version 1.10

The user should decide which version he likes to use based on provided features flag. I have the following Cargo.toml file:

[package]
name = "cargo-bug-poc"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["solana_1_10"]
solana_1_9 = ["solana-banks-client-1-9",
"solana-program-1-9"]
solana_1_10= ["solana-banks-client-1-10",
"solana-program-1-10"]

[dependencies]
solana-banks-client-1-9 = {package = "solana-banks-client", version = "=1.9", optional = true }
solana-program-1-9 = {package = "solana-program", version = "=1.9", optional = true }
solana-banks-client-1-10= {package = "solana-banks-client", version = "=1.10", optional = true }
solana-program-1-10= {package = "solana-program", version = "=1.10", optional = true }

I expected that building it with just cargo build should work, and it will build it with 1.10 version (as it's marked as default). However, it does not work.
I also tried different combinations like:

  1. cargo build --features solana_1_9 --no-default-features
  2. cargo build --features solana_1_10 --no-default-features

None of these commands works. Command output suggests version collisions:

error: failed to select a version for `solana-banks-client`.
    ... required by package `cargo-bug-poc v0.1.0 (/Users/przem/code/hal_tools/cargo-bug-poc)`
versions that meet the requirements `=1.9` are: 1.9.18, 1.9.17, 1.9.16, 1.9.15, 1.9.14, 1.9.13, 1.9.12, 1.9.11, 1.9.10, 1.9.9, 1.9.8, 1.9.7, 1.9.6, 1.9.5, 1.9.4, 1.9.3, 1.9.2, 1.9.1, 1.9.0

all possible versions conflict with previously selected packages.

  previously selected package `solana-banks-client v1.10.0`
    ... which satisfies dependency `solana-banks-client-1-10 = "=1.10"` of package `cargo-bug-poc v0.1.0 (/Users/przem/code/hal_tools/cargo-bug-poc)`

failed to select a version for `solana-banks-client` which could resolve this conflict

Also it's working perfectly fine when I declare it as external dependency in other project like:
cargo-bug-poc = { version = "0.1.0", default-features = false, features=["solana_1_9"], path = "../cargo-bug-poc" }.

Steps

I created a repo to test this potential bug: link

  1. Try to build lib in the lib folder (with default-features and features flags) and expect an error.
  2. Try to run the program in prog with different features as dependency flags and see that it's working.

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.60.0 (d1fd9fe2c 2022-03-01)
release: 1.60.0
commit-hash: d1fd9fe2c40a1a56af9132b5c92ab963ac7ae422
commit-date: 2022-03-01
host: x86_64-apple-darwin
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.79.1 (sys:0.4.51+curl-7.80.0 system ssl:(SecureTransport) LibreSSL/3.3.5)
os: Mac OS 12.3.1 [64-bit]
@meltedblocks meltedblocks added the C-bug Category: bug label Apr 27, 2022
@meltedblocks meltedblocks changed the title Optional dependency is not omitted at compilation. Optional dependency is not omitted at package building. Apr 27, 2022
@epage
Copy link
Contributor

epage commented Apr 27, 2022

The challenge here comes from two parts

  • The lock file for your project is generated independent of any feature flags you pass in on the command line
  • Cargo is only letting you lock one compatible version

Is there a reason you are declaring feature flags per semver-compatible version? I normally see something like this for semver-incompatible versions.

Independent of the error, there are also challenges with #2980

@meltedblocks
Copy link
Author

Thank you for the response @epage.

It's a specific situation. I can create separate package versions for different dependency versions. However, there is almost no difference between these packages (they only differ in dependencies and a couple of lines of code). Then by adding new features, I will have to support both versions (building, releases, etc.). Using features was more convenient for me, but I see your point and these challenges. So for now, I will create and support separate package versions.

@Eh2406
Copy link
Contributor

Eh2406 commented Apr 27, 2022

Is solana not following semver? 1.9 and 1.10 are semver compatible version numbers. If the code works with 1.9 then the same code should work with 1.10.

@meltedblocks
Copy link
Author

Unfortunately, not in that case. We are using it to modify the Solana test program - to make it easier to use for us. It's touching Solana internals that is working differently in 1.9 and 1.10.

@dndll
Copy link

dndll commented May 6, 2022

I have this exact issue with parity-scale-codec. I can, for sure deploy two separate binaries but at the moment we are not publishing packages. Otherwise I have the same approach here https://github.com/TrueLayer/reqwest-middleware/blob/main/reqwest-tracing/src/otel.rs

@ehuss ehuss added the A-dependency-resolution Area: dependency resolution and the resolver label May 24, 2022
@ehuss
Copy link
Contributor

ehuss commented May 24, 2022

I'm going to close as this is working as intended. Cargo's resolution is designed to prevent two edges to two different semver-compatible versions of the same package. I don't think this will be relaxed or changed in the foreseeable future.

@ehuss ehuss closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dependency-resolution Area: dependency resolution and the resolver C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

5 participants