# Pre-RFC: cfg - Aligning feature configuration between Cargo & rustc

**URL:** <https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341>\
**Category:** tools and infrastructure\
**Created:** [March 18, 2022, 7:59am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341 "2022-03-18T07:59:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![taqtiqa-mark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taqtiqa-mark/32/9114_2.png) [@taqtiqa-mark](https://internals.rust-lang.org/u/taqtiqa-mark)\
**Post date:** [March 18, 2022, 7:59am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/1 "2022-03-18T07:59:16Z")

</div>

Hi, The Cargo feature request ticket directs a Pre-RFC here in the first instance. Also a previous discussion "pre-rfc-crate-defined-cfg-attributes"[1](https://internals.rust-lang.org/t/pre-rfc-crate-defined-cfg-attributes/2154) suggests `cfg` related functionality might best be addressed in Cargo.

Feature: Add a constrained/defined subset of `#[cfg(...)]` functionality to Cargo, via `[cfg.features]` in Cargo.tom, that enables the same rust-lang features/behavior available via `rustc --cfg`. The `rustc --cfg` feature is quite prominent, so was a little surprising this language feature (?) is not available in any degree via Cargo:

> **[Custom - Rust By Example](https://doc.rust-lang.org/rust-by-example/attribute/cfg/custom.html)**
>
> Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

Context: Something like the following is elegant and expressive, and is available to you if you build using `rustc` but not if you use `cargo build|run`:

```rust
#[cfg(debug)]
fn a() { }

```

A workaround is this:

```rust
#[cfg(feature = "debug")]
fn a() { }

```

Proposal: Cargo.toml might look like this:

```nohighlight
[cfg.features]
debug = []
trace = ["traceable", "jaeger"]

[features]
traceable = ["opentelemetry", "tracing", "tracing-opentelemetry"]
jaeger = ["tracing-jaeger","opentelemetry-jaeger"]

```

Where Cargo `debug=[]` is equivalent to `rustc --cfg debug` and `trace = ["traceable", "jaeger"]` is equivalent to `rustc --cfg trace --cfg 'feature="jaeger"' --cfg 'feature="traceable"'`

Not sure how enthused people are by that? Whether a RFC is warranted or how best to proceed - i.e. how difficult to implement this might be?

Appreciate any thoughts.

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [March 18, 2022, 8:14am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/2 "2022-03-18T08:14:40Z")

</div>

> [@taqtiqa-mark](#):
>
> so was a little surprising this language feature (?) is not available via Cargo

That's not an omission, but a deliberate design decision. Cargo intentionally doesn't expose the full power of arbitrary cfg attributes. I don't remember off the top of my head the exact arguments for why the system works the way it does (that's probably explained in some issue/pr/rfc), but the overall idea is that unrestricted cfgs would break composition of independent cargo packages.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 18, 2022, 8:39am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/3 "2022-03-18T08:39:40Z")

</div>

It's worth noting that you _can_ set arbitrary `cfg` flags for just your crate with `build.rs`, so while it's more difficult than if it were in the manifest, it's not something you can't do with cargo.

---

<div class="post-metadata">

**Author:** ![wesleywiser](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wesleywiser/32/2742_2.png) [@wesleywiser](https://internals.rust-lang.org/u/wesleywiser)\
**Post date:** [March 18, 2022, 3:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/4 "2022-03-18T15:26:21Z")

</div>

If `cfg`s are going to be exposed in `Cargo.toml`, we should make sure that we can take advantage of the [conditional compilation checking](https://rust-lang.github.io/rfcs/3013-conditional-compilation-checking.html) feature.

---

<div class="post-metadata">

**Author:** ![taqtiqa-mark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taqtiqa-mark/32/9114_2.png) [@taqtiqa-mark](https://internals.rust-lang.org/u/taqtiqa-mark)\
**Post date:** [March 19, 2022, 9:02am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/5 "2022-03-19T09:02:59Z")

</div>

> [@matklad](#):
>
> That's not an omission, but a deliberate design decision. Cargo intentionally doesn't expose the full power of arbitrary cfg attributes.

OK, thanks for the insight. IIUC, Cargo does not support conditional compilation via Cargo.toml, but rather through build.rs?

If that is correct then users can be put on the right path just with a documentation suggestion or two?

---

<div class="post-metadata">

**Author:** ![taqtiqa-mark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taqtiqa-mark/32/9114_2.png) [@taqtiqa-mark](https://internals.rust-lang.org/u/taqtiqa-mark)\
**Post date:** [March 19, 2022, 9:06am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/6 "2022-03-19T09:06:30Z")

</div>

> [@CAD97](#):
>
> It's worth noting that you _can_ set arbitrary `cfg` flags for just your crate with `build.rs` , so while it's more difficult than if it were in the manifest, it's not something you can't do with cargo.

Thanks, I've not had reason to use `build.rs`, so it looks like that time has arrived.

Per @matklad's comment, unless there is a chorus of support I'm going to considered this solved by your suggestion to use build.rs.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [March 19, 2022, 9:52am UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/7 "2022-03-19T09:52:11Z")

</div>

> [@taqtiqa-mark](#):
>
> OK, thanks for the insight. IIUC, Cargo does not support conditional compilation via Cargo.toml, but rather through build.rs?

`#[cfg(feature= "...")]` is supported using Cargo.toml. Why do you want it to not be namespaced? You can rewrite the `#[cfg]` from `#[cfg(foo)]` to `#[cfg(feature = "foo")]`, right?

---

<div class="post-metadata">

**Author:** ![taqtiqa-mark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taqtiqa-mark/32/9114_2.png) [@taqtiqa-mark](https://internals.rust-lang.org/u/taqtiqa-mark)\
**Post date:** [March 19, 2022, 4:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/8 "2022-03-19T16:26:41Z")

</div>

> [@bjorn3](#):
>
> Why do you want it to not be namespaced? You can rewrite the `#[cfg]` from `#[cfg(foo)]` to `#[cfg(feature = "foo")]` , right?

I addressed that in the OP: "Something like the following is elegant and expressive". Yes things can be rewritten, I gave that workaround in the OP. In the example I tried to convey that, in the example, `trace` is a particular configuration of features.

In case it mitigates the objections @matklad aluded to I've amended the proposal to be `[cfg.features]`, because it is not more general than that.

Otherwise, I'll leave this as is - stalled.  
If others have similar needs they'll likely find this thread.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 17, 2022, 4:27pm UTC](https://internals.rust-lang.org/t/pre-rfc-cfg-aligning-feature-configuration-between-cargo-rustc/16341/9 "2022-06-17T16:27:22Z")

</div>

Closed on June 17, 2022, 4:27pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
