Attempting to clean up the feature activation syntax?

Currently, a feature activation in the manifest can include:

  • "foo": activate the feature foo in the current package
  • "dep:foo": activate the optional dependency foo
  • "foo?/bar": if the optional dependency foo is activated, activate the feature bar within it
  • "foo/bar":
    • If foo is non-optional dependency, activate the feature "bar" within it
    • If foo is an optional dependency, activate it, activate a feature named foo if present, and activate the feature bar

On the command line with resolver = "2" or later,

  • -F foo: activate foo within any selected packages that have it
  • -F foo/bar
    • If foo is a dependency of a selected package, treat foo/bar like it was in a manifest
    • Else if foo is a workspace member, activate bar within it

Problems with this

  • It is hard to know what all these different forms are and their subtle meaning (e.g. I'm not sure that my summary is 100% accurate)
  • Feels weird that -F foo/bar might not just activate bar but could also be equivalent of -F foo,foo/bar
  • It is too easy to accidentally use "foo/bar" and pull in a dep and never realize it (Lint for suspicious optional dependency activation · Issue #17639 · rust-lang/cargo · GitHub)
  • Processing "foo/bar" requires knowledge of the full manifest to know what it translates to (which caused us problems with the path we were taking on RFC #3491 which cause it to be pulled from the 2024 edition at the last minute)
  • I've occasionally been confused that "foo?/bar" isn't "dep:foo?/bar" though from other perspectives, foo?/bar makes sense to me

Additional context

  • We are phasing out being able to refer directly to optional deps (RFC 3491)
  • I have some other ideas for activating more non-feature items, like dep: does for dependencies

So how should we clean this up?

11 Likes

The idea I have been leaning towards:

Phase 1

  • Make "foo?/bar" work with required dependencies and deprecate "foo/bar" in manifests, removing it in a new edition
  • Make --features foo/bar only mean "activate bar in package foo", in a new workspace.resolver version, switching to the new resolver in the next edition

This would make things consistent with where they are used.

Phase 2 (maybe)

  • Reintroduce "foo/bar" in manifests to mean "foo?/bar"

This would reduce the number of different syntaxes users deal with. Yes, this also gives them slightly different meanings, CLI errors if foo isn't active but manifests won't. I expect people will view it relatively naturally, particularly since a lot of people already accidentally write "foo/bar" for "foo?/bar". This would be similar to foo@ver in cargo where sometimes ver is a version (cargo install), sometimes it is a version requirement (cargo add), and sometimes a version prefix (--package).

Downsides

  • By pushing people away from the current "foo/bar", Cargo.lock may end up with extra content because the dependency resolver was never taught how to deal with "foo?/bar"
  • The resolver bumps feels weird for just a CLI change and we tend to view the CLI outside of editions but it follows precedence with resolver = "2"
  • The Index is not subject to editions and we might end up needing to rewrite things into an older form of syntax, especially if foo/bar ever comes to mean foo?/bar
3 Likes

This has always been the weirdest part to me, having a flag that can activate an unlocked transitive dependency (IIRC it caused some issues on docs.rs since people tried using it in their flags but at the point it applies only the locked dependencies had been fetched).

Only supporting the foo?/bar form in manifests sounds good to me, (it would be really nice if the index and lockfiles supported it properly too to make auditing easier).

It's odd that dep:foo/bar and dep:foo?/bar aren't a thing. I mentally parse it as dep:foo referring to the dependency namespace, and therefore foo referring to feature namespace, so foo?/bar reads as feature?/feature, which is weird! Logically if dep:foo names a dependency, then dep:foo/bar should be required to refer to a feature of a dependency. But at this point it would be too much churn to migrate to this logic.


I've used -F foo/bar maybe a couple of times ever. Usually it just doesn't work as intended, because top-level crates remain unaware the feature is enabled, since they can't detect what features their dependencies have. Enabling a nested feature flag usually bypasses top-level crate's own mirror of the feature flag and its cfg()s.

If you have something like:

[package]
name = "foo"

[features]
turbo = ["foo-core/inner-turbo"]

then the crate is likely to have #[cfg(feature = "turbo")] itself, and -F foo-core/inner-turbo enables something that the crate won't use anyway (BTW, it'd be cool if #[cfg(feature = "dep:foo-core/inner-turbo")] worked!)

I find -F foo/bar useful to enable debugging features of dependencies. In this case, there are no cfgs to go wrong within the workspace, and it is very much desirable that the feature can be enabled without it being mentioned anywhere in the workspace. (It's not good that this can bypass the lock file, though.)

2 Likes

clap has a debug feature and I mirrored it in winnow.

Same, I think this would be the most consistent variation of today's syntax:

# Feature (not a dependency)
shiny-feature

# Optional dependency
dep:library

# Feature of the dependency (optional dependency is enabled implicitly)
dep:library/shiny-feature

# Feature of the dependency (optional dependency is not enabled implicitly)
dep:library?/shiny-feature

As for CLI, --features/-F accepting values that are not defined under [features] of the package itself is also a surprising behavior, though I can see why it might be convenient sometimes and that some got used to it.

More controversial and possibly slightly less directly related thoughts

I personally struggled with features a bit recently, mostly because there are no dev-specific and build-specific features, no features specific to [lib] or [bin], and features can't be named in cfg() in Cargo.toml in contrast to Rust's #[cfg()] syntax. I also only recently discovered that build dependencies can be optional (seems obvious in retrospect) and activated via features as well, though it also activates non-build optional dependency, which isn't always desirable.

Dev and build dependencies could potentially be addressed with dev-dep: and build-dep: prefix, by analogy to dev-dependencies and build-dependencies.

Another alternative might be to get rid of dep: completely and write things like this instead:

# Exactly the same syntax as in Rust code
[target.'cfg(feature = "interesting")'.dependencies]
# Some extra dependencies

# Exactly the same syntax as in Rust code
[target.'cfg(all(target_arch = "x86_64", feature = "interesting"))'.dependencies]
# Some extra platform-specific dependencies

[features]
# Can't refer to dependencies, only defines the hierarchy of features
less-interesting = []
interesting = ["less-interesting"]

Then explicitly defined features are the only ones available from the outside (including CLI), everything else is an implementation detail. It does tend to be a lot more verbose though.


The least intuitive aspect of feature activation though is doing it from build.rs, which today doesn't even require defining it in Cargo.toml.


Since features and dependencies are closely related, these (and probably more) seem relevant too:

The challenge with features in target.*.dependencies is we can't support not(feature = "foo"). There is a This Development Cycle in Cargo post entry talking about this.

The least intuitive aspect of feature activation though is doing it from build.rs, which today doesn't even require defining it in Cargo.toml.

You can set a cfg but there be dragons (like most things in build.rs) if you set it on a feature, like not getting the associated dependencies, feature activations it causes, etc.

The closest match I could find was this: This Development-cycle in Cargo: 1.77 | Inside Rust Blog

Does it help if dependencies stop creating implicit features? At the first glance it seems like all features can be resolved first, and then whatever feature set ends up at the end dictates which dependencies get enabled (including both dep: and cfg()).

I imagine a more complex version of the first step is what Cargo does today already. So maybe if it is simplified, that could in turn enable new capabilities elsewhere?

Yes, that is it.

Sadly, implicit features don't make a difference for this. Still looking forward to removing them on an Edition.

1 Like

One thing that bothers me a bit is that you can list needed features for an example or test, but you can't make those features turn on automatically when you run it independently.

2 Likes

I have cheated at this by having the package depend on itself as a dev-dependency with more features enabled, but that has other drawbacks.

2 Likes

I kind of want something a lot more verbose so that I can slot other things in there, like activating features based on what target I'm running for, and other cfg flags.

I do like the idea of moving foo/bar to mean foo?/bar by default, though "foo", "foo/bar" does get a little verbose if you actually want the dependency activation.

1 Like