cargo icon indicating copy to clipboard operation
cargo copied to clipboard

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

Open tbu- opened this issue 8 years ago • 76 comments

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 (?)).

tbu- avatar Sep 10 '15 15:09 tbu-

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

pwoolcoc avatar Sep 14 '15 20:09 pwoolcoc

Sounds great!

tbu- avatar Sep 14 '15 23:09 tbu-

Any updates?

marjakm avatar Dec 07 '15 17:12 marjakm

+1, would be great to have it

alexbool avatar Dec 16 '15 07:12 alexbool

Also consider as possible filters for dependencies:

  • Examples
  • Integration tests
  • Benchmarks

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

gyscos avatar Feb 22 '16 23:02 gyscos

@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.

hauleth avatar Mar 25 '16 00:03 hauleth

@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?

azerupi avatar Aug 02 '16 11:08 azerupi

Would love to have this.

fulmicoton avatar Aug 03 '16 23:08 fulmicoton

@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

alexcrichton avatar Aug 05 '16 06:08 alexcrichton

Any progress on this yet?

alexreg avatar Nov 08 '16 16:11 alexreg

@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)

alexcrichton avatar Nov 08 '16 16:11 alexcrichton

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?

rminderhoud avatar Mar 20 '17 03:03 rminderhoud

@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.

alexcrichton avatar Mar 20 '17 14:03 alexcrichton

Two recent issues where this has cropped up:

  • https://github.com/steveklabnik/semver/pull/101
  • https://github.com/google/pulldown-cmark/pull/55 and https://github.com/google/pulldown-cmark/pull/56

steveklabnik avatar Mar 22 '17 20:03 steveklabnik

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.

crumblingstatue avatar Mar 22 '17 20:03 crumblingstatue

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

alexreg avatar Mar 22 '17 21:03 alexreg

cc https://github.com/rust-lang/cargo/pull/3870 a sample implementation of this.

alexcrichton avatar Mar 27 '17 21:03 alexcrichton

Another workaround I found to work well, is add an optional feature binaries, and add a required-features field with that to the bin. As far as I know this hasn't been mentioned here before.

Example:

[[bin]]
name = "mybinary"
path = "src/main.rs"
required-features = ["binaries"]

[dependencies]
docopt = { version = "0.8.1", optional = true }

[features]
binaries = ["docopt"]

Trying to install the crate with cargo install mycrate fails with a pretty descriptive error, and as long as you document that it should be installed with cargo install mycrate --features=binaries, I don't really see a big downside to it.

A more consice way would of course be nice though ;)

hobofan avatar Aug 02 '17 12:08 hobofan

@hobofan this is sweet

whitequark avatar Aug 02 '17 12:08 whitequark

@alexcrichton #3870 was closed with

we may wish to continue to discuss this externally outside of a PR before resubmitting.

Has any more discussion taken place?

jonhoo avatar Nov 09 '17 23:11 jonhoo

Not a significant amount as far as I know at least

alexcrichton avatar Nov 18 '17 08:11 alexcrichton

To build on this, it would be handy to have a way of declaring dependencies for only some binaries. For example, I have one binary that does EC2 orchestration, and depends on much of rusoto. It'd be sad to have to compile all of that if you're not running that binary.

@hobofan's workaround with required-features is nice, but it's inconvenient to use features for this as it also forces the library crate to be recompiled, which can take a non-trivial amount of time for larger crates. It of course also means that you need to add an additional flag for every binary you want to compile.

jonhoo avatar Feb 14 '18 23:02 jonhoo

This issue has been open some time now. Does anyone plan on tackling it?

alexreg avatar Feb 15 '18 00:02 alexreg

@crumblingstatue For consistency, wouldn't "[cooldep.bin.dependencies]" make more sense as it keeps the most specific item on the left, getting more general as you go to the right?

spease avatar Feb 25 '18 23:02 spease

As there hasn't been any activity here in over 6 months I've marked this as stale and if no further activity happens for 7 days I will close it.

I'm a bot so this may be in error! If this issue should remain open, could someone (the author, a team member, or any interested party) please comment to that effect?

The team would be especially grateful if such a comment included details such as:

  • Is this still relevant?
  • If so, what is blocking it?
  • Is it known what could be done to help move this forward?

Thank you for contributing!

If you're reading this comment from the distant future, fear not if this was closed automatically. If you believe it's still an issue please leave a comment and a team member can reopen this issue. Opening a new issue is also acceptable!

stale[bot] avatar Sep 16 '18 16:09 stale[bot]

@stale bump. (whoops, apparently bots live in a separate namespace)

There is real demand for this feature, so let's keep this open as a reminder that somebody should implement it someday.

crumblingstatue avatar Sep 16 '18 16:09 crumblingstatue

@crumblingstatue Looks like it will need RFC. Fancy writing one? I'd be glad to assist and review.

alexreg avatar Sep 16 '18 17:09 alexreg

@alexreg Well, I do want this feature, but I'm not sure if I'm up to writing an RFC with a concrete implementation plan.

I think it's useful to track features that are in demand, but I understand if this issue tracker isn't for feature requests.

crumblingstatue avatar Sep 16 '18 18:09 crumblingstatue

What do you mean? All I'm saying is that if you really want this feature, go write an RFC. That's the best way to get it. People will assist you too.

alexreg avatar Sep 16 '18 19:09 alexreg

@alexreg I'm interested in writing an RFC if you're still up for helping.

hwchen avatar Sep 27 '18 15:09 hwchen

@hwchen Great. And yes, I'd be glad to. Would you like to draft it then get my feedback, or discuss it on Discord/Zulip first? (That's where a lot of we Rust maintainers meet.)

alexreg avatar Sep 27 '18 16:09 alexreg

@alexreg awesome! let me read over this thread more carefully this weekend, then I'll ping you.

hwchen avatar Sep 29 '18 01:09 hwchen

Is there an RFC for this now?

AldaronLau avatar Jan 09 '19 21:01 AldaronLau

@OxyDeadbeef Afraid not. @hwchen How are things with you? I forgot about this thread until now. Are you interested in drafting something up still, with my assistance?

alexreg avatar Jan 10 '19 02:01 alexreg

@alexreg if hwchen isn't interested, I would be

pwoolcoc avatar Jan 10 '19 14:01 pwoolcoc

Is there anywhere I can be a fly on the wall for that so I can understand how that process works?

On Thu, 10 Jan 2019 at 08:29, Paul Woolcock [email protected] wrote:

@alexreg https://github.com/alexreg if hwchen isn't interested, I would be

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rust-lang/cargo/issues/1982#issuecomment-453114689, or mute the thread https://github.com/notifications/unsubscribe-auth/AAr4yO2zpnTIZStuK146wlljxVaQ9EGAks5vB05cgaJpZM4F7GK6 .

paulevans avatar Jan 10 '19 16:01 paulevans

Sorry, I let this drop off. If anybody else would like to take this up right now, please feel free.

On Thu, Jan 10, 2019 at 11:15 AM Paul Evans [email protected] wrote:

Is there anywhere I can be a fly on the wall for that so I can understand how that process works?

On Thu, 10 Jan 2019 at 08:29, Paul Woolcock [email protected] wrote:

@alexreg https://github.com/alexreg if hwchen isn't interested, I would be

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rust-lang/cargo/issues/1982#issuecomment-453114689, or mute the thread < https://github.com/notifications/unsubscribe-auth/AAr4yO2zpnTIZStuK146wlljxVaQ9EGAks5vB05cgaJpZM4F7GK6

.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rust-lang/cargo/issues/1982#issuecomment-453154461, or mute the thread https://github.com/notifications/unsubscribe-auth/ADm0i2l4chTz_L3Vxo3WDN7YwYcJj5X5ks5vB2crgaJpZM4F7GK6 .

hwchen avatar Jan 10 '19 18:01 hwchen

Ok, I'm going to try and get a rough draft of an RFC to @alexreg in the next couple days

pwoolcoc avatar Jan 10 '19 20:01 pwoolcoc

@pwoolcoc That's super. I look forward to seeing it. If you have any questions in the meanwhile, tag me here or pop into the #design channel on Rust's Discord server.

alexreg avatar Jan 10 '19 23:01 alexreg

Is there anywhere I can be a fly on the wall for that so I can understand how that process works?

@paulevans The process of RFC creation? The README on the RFCs repo provides a good overview, which @pwoolcoc will be following I'm sure. Usually the content of such RFC drafts is guided either my one's own ideas and musings or by pre-RFC discussion (over at internals.rust-lang.org). Sometimes they're a collaborative effort. Eventually an RFC PR is opened on the official Rust RFCs repo, when the community and in particular the Rust Lang (Design) team provides feedback on it. Usually the PR author(s) tag people who have been involved in the process, or expressed an interest. In this case we'll also post a link to the draft RFC here, so you can subscribe to that and follow the discussion and process.

alexreg avatar Jan 10 '19 23:01 alexreg

@alexreg I am still working on this, just taking longer than I expected

pwoolcoc avatar Jan 22 '19 15:01 pwoolcoc

@pwoolcoc No problem. Here (or on Discord/Zulip) for advice if you need it, otherwise just ping me when you have a draft ready.

alexreg avatar Jan 22 '19 17:01 alexreg

Excited Rust beginner here, also eager to try and help in any way i can. Writing a side project & bumped into this issue. I'm on both of the Discords with same username, feel free to ping! ❤️

Walther avatar Jan 27 '19 07:01 Walther

@Walther We'll post a link to the RFC (draft) PR here when it's ready, which will hopefully be soon. :-)

alexreg avatar Jan 27 '19 18:01 alexreg

Has any progress been made with the RFC? I'd be willing to try writing one if it's been dropped again.

dmarcuse avatar Mar 21 '19 15:03 dmarcuse

@dmarcuse It seems that it has been dropped yes. If you'd be interested in taking it up, then great. I'm happy to provide advice or help review it.

alexreg avatar Mar 21 '19 17:03 alexreg

I'll start working on it this weekend and keep you updated!

dmarcuse avatar Mar 21 '19 17:03 dmarcuse

Super. You can contact me here, or on Discord/Zulip if you like.

alexreg avatar Mar 21 '19 17:03 alexreg

@alexreg I started drafting the RFC and sent you a friend request on Discord, could you accept it so I could ask some questions?

dmarcuse avatar Mar 25 '19 14:03 dmarcuse

@dmarcuse Okay thanks! Accepted now. (I think you can PM me regardless, but yeah, ask away...)

alexreg avatar Mar 25 '19 18:03 alexreg

@dmarcuse @alexreg have you made any progress since your last comments here?

djc avatar May 09 '19 14:05 djc

@djc Yep, @dmarcuse has begun a draft, and I've looked at a bit. Will review some more today.

alexreg avatar May 09 '19 16:05 alexreg

@djc I have been (very slowly) working on a draft available here - feedback or contributions would be appreciated!

dmarcuse avatar May 09 '19 22:05 dmarcuse

@dmarcuse what you have so far looks good! For drawbacks and alternatives, it would be probably be useful to dig into what require_features currently does and how it would interact with your proposed feature. Otherwise this so far seems like a fairly straightforward feature and a mostly natural extension to what we already have, so there's probably not a lot of prior art and future possibilities to talk about.

My advice would be not to try to polish it all to perfection before you submit it, just write up whatever's in your mind and push it, then the community can help you with more feedback.

djc avatar May 12 '19 13:05 djc

Thanks for the tips @djc! I'm going to try to work on it more later today.

dmarcuse avatar May 12 '19 16:05 dmarcuse

Yes, we'll specifically get Alex Crichton's feedback on it once we have it in a decent state. :-)

alexreg avatar May 12 '19 19:05 alexreg

What are the main reasons for not going with using a workspace or dev-dependencies here?

majg0 avatar Oct 29 '19 12:10 majg0

dev-dependencies are for tests and benchmarks really. Workspaces are designed to solve another problem, and are a cannon for the fly in any case.

alexreg avatar Oct 29 '19 16:10 alexreg

Hello @dmarcuse! Any updates on this issue?

jabedude avatar Dec 03 '19 14:12 jabedude

I've been meaning to finish up and submit the RFC, but have been really busy with coursework lately - sorry for the delays. The current draft version is available here if you want to read through it, I'd appreciate feedback.

dmarcuse avatar Dec 03 '19 14:12 dmarcuse

Currently in this exact situation: I have a library, and a binary that uses said library. I'd prefer for them both to be in the same crate, but I also don't want the library to depend on clap, just the binary.

linclelinkpart5 avatar Mar 21 '20 22:03 linclelinkpart5

There's an RFC now: https://github.com/rust-lang/rfcs/pull/2887

est31 avatar Apr 02 '20 16:04 est31

Created a new RFC for this, https://github.com/rust-lang/rfcs/pull/3020 with the discussion from the previous one.

pksunkara avatar Nov 15 '20 05:11 pksunkara

it'd be nice to have this as you can no longer rely on extern crate in edition 2018.

SoniEx2 avatar Jan 24 '21 13:01 SoniEx2

BTW, this affects bindgen - it optionally depends on clap because of binary but libraries using it in build scripts don't need clap and may forget to turn off default features.. This happened in case of rocksdb for instance.

Kixunil avatar Jan 24 '21 21:01 Kixunil

Perhaps we can enable all features specified by required-features in [[bin]] and [[test]]?

NobodyXu avatar Jul 24 '22 04:07 NobodyXu

https://github.com/rust-lang/cargo/issues/4663 is the issue for auto-enabling required-features, at least for examples with cargo run.

epage avatar Jul 26 '22 16:07 epage

Could we just tag dependencies with the application scope when declaring them? So the compiler/cargo would do the rest:

[dependencies]
foo = { scope = ["bin"], version = "*" } 
bar = { scope = ["lib"], version = "*" }
foobar = { scope = ["bin", "lib"], version = "*" }

charlesrocket avatar Jul 26 '22 17:07 charlesrocket