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

Allow dependencies that only apply to specific cargo targets (bin, example, etc.) #1982

Open
tbu- opened this issue Sep 10, 2015 · 78 comments
Open
Labels
A-crate-dependencies Area: [dependencies] of any kind C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` E-hard Experience: Hard S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@tbu-
Copy link
Contributor

tbu- commented Sep 10, 2015

Use case: I have a library that also produces an executable. The executable needs env_logger in order to be able to log, the library shouldn't carry that dependency though, as it pulls in all sorts of crates (e.g. regex (?)).

@alexcrichton alexcrichton added the A-configuration Area: cargo config files and env vars label Sep 10, 2015
@pwoolcoc
Copy link
Contributor

@tbu- I have a branch with this mostly working, I'm trying to get it cleaned up and committed "soon"

@tbu-
Copy link
Contributor Author

tbu- commented Sep 14, 2015

Sounds great!

@marjakm
Copy link

marjakm commented Dec 7, 2015

Any updates?

@alexbool
Copy link

+1, would be great to have it

@gyscos
Copy link

gyscos commented Feb 22, 2016

Also consider as possible filters for dependencies:

  • Examples
  • Integration tests
  • Benchmarks

Also, should it be per-binary (when multiple are provided)?

@hauleth
Copy link

hauleth commented Mar 25, 2016

@gyscos all of that can fall into dev-dependencies bucket. I see no reason for giving ability to set dependency only for benchmarks or something.

@azerupi
Copy link
Contributor

azerupi commented Aug 2, 2016

@alexcrichton is this a desired feature? Would a PR proposing this get merged?

My personal opinion is that it would be useful for libraries exposing a little cli alongside of a lib (e.g. pulldown-cmark) because it would allow them to, for example, pull in an argument parser (e.g. Clap) without imposing that dependency on all the library users and without the need to create a separate crate for the small binary.

Also, should it be per-binary (when multiple are provided)?

What is your opinion on this and how should it be represented in Cargo.toml?

@fulmicoton
Copy link

Would love to have this.

@alexcrichton
Copy link
Member

@azerupi yeah I suspect Cargo will end up with something like this eventually, it's certainly a well motivated feature! I wouldn't got he route of dependencies-per-binary yet, though, as we don't similarly also have things like dependencies-per-example or dependencies-per-test

@alexreg
Copy link
Contributor

alexreg commented Nov 8, 2016

Any progress on this yet?

@alexcrichton
Copy link
Member

@alexreg unfortunately, no

This would likely require an RFC as it's an extension to Cargo.toml (e.g. a whole new section for dependencies)

@rminderhoud
Copy link

rminderhoud commented Mar 20, 2017

Hello, while this is being worked on is there a temporary "best practices" for handling this kind of project structure (root library crate -> member bin with additional dependencies)? Right now, is the only way to avoid polluting the library dependencies with the binary dependencies is to have each binary be a separate (non-workspaced) crate?

@alexcrichton
Copy link
Member

@rminderhoud in the meantime the "best solution" (it's not really that great, but it works) is to split up into separate Cargo projects which can each have their own set of dependencies.

@steveklabnik
Copy link
Member

Two recent issues where this has cropped up:

@crumblingstatue
Copy link

The most general solution would be to allow optional dependencies for every bin target. TOML makes this relatively easy to express:

[[bin]]
name = "foo"

[bin.dependencies]
clap = "1"
term = "0.5"

[[bin]]
name = "bar"

[bin.dependencies.cooldep]
version = "2.0"
default-features = false
features = ["foo"]

In the future, this general concept could also be expanded to examples/tests/etc., if deemed appropriate.

@alexreg
Copy link
Contributor

alexreg commented Mar 22, 2017

I agree, the syntax & semantics proposed by @crumblingstatue look pretty sensible.

@alexcrichton
Copy link
Member

cc #3870 a sample implementation of this.

jcjones added a commit to mozilla/authenticator-rs that referenced this issue May 17, 2017
Pull in the log crate and move all the OSX println statements to be at appropriate
log levels.

This also pulls in `env_logger`, an implementation of a logger, that obeys an
environment variable RUST_LOG. I added some notes to README.md as to how to
use it.

Optimally, we don't need `env_logger` except for tests and the binary, but
we can't eliminate it from the library form until [this PR for cargo completes](rust-lang/cargo#1982), so we might need to refactor it out of main.rs when this becomes a Gecko lib.

But maybe not? Anyway, it's easy to change down the line.
@Mart-Bogdan
Copy link

@hauleth

all of that can fall into dev-dependencies bucket. I see no reason for giving ability to set dependency only for benchmarks or something.

No, there is benefit -- reduce build time of tests, for example.
And reduce bloat and work on linker to strip uneded code. If bin would require test deps, we would need to buld all tests related stuff for binary.

@segevfiner
Copy link
Contributor

I got hit by this when developing segevfiner/keepawake-rs (segevfiner/keepawake-rs#3), and I'm not sure how to restructure/split my crate in the mean time to avoid this issue. In terms of which part of the crate I should split into a sub-directory in a Cargo workspace and how to handle all the project metadata, README.md and so on, cause I'll have to write two, and/or how to correctly publish in such a configuration...

I tried the users forum but got no answer there as well https://users.rust-lang.org/t/whats-the-convention-for-handling-a-hybrid-library-and-binary-crates-dependencies/84174

@pksunkara
Copy link

New RFC has been proposed: rust-lang/rfcs#3374

@detly
Copy link

detly commented Mar 6, 2023

For the specific use-case of binaries that exist to help with integration tests, I've made a test-binary crate that might help. It's admittedly pretty niche, but it's why I'm watching this issue, so maybe it'll prove useful to others too.

@tae-soo-kim
Copy link

@tbu- I have a branch with this mostly working, I'm trying to get it cleaned up and committed "soon"

"soon"?

@wiiznokes
Copy link

New RFC has been proposed: rust-lang/rfcs#3374

This is not the same feature of this issue

@mccolljr
Copy link

I hate to bump a stale thread, but I was curious about this today and google led me here. Is this a "dead" effort, or is there some blocker to implementing this that is not mentioned here?

@lure23
Copy link

lure23 commented Aug 8, 2024

I would use such feature to enable defmt feature (dependency) for all examples, while keeping it an opt-in for the lib itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-crate-dependencies Area: [dependencies] of any kind C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` E-hard Experience: Hard S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests