Skip to content

cargo config get doesn't seem to pick up RUSTFLAGS or RUSTDOCFLAGS #12087

@LukeMathWalker

Description

@LukeMathWalker

Problem

cargo config get build.rustdocflags and cargo config get build.rustflags do not seem to behave as I would have expected.
If the respective environment variables are set (i.e. RUSTDOCFLAGS and RUSTFLAGS) but no value is set in .cargo/config.toml, I'd expect the environment variable value to be returned.

Instead, the command fails with:

error: config value `build.rustdocflags` is not set

Steps

export RUSTDOCFLAGS="--cfg tokio_unstable"
cargo -Z unstable-options config get build.rustdocflags

and

export RUSTFLAGS="--cfg tokio_unstable"
cargo -Z unstable-options config get build.rustflags

Possible Solution(s)

No response

Notes

Is this intended behaviour?

Version

No response

Activity

added
C-bugCategory: bug
S-triageStatus: This issue is waiting on initial triage.
on May 5, 2023
weihanglo

weihanglo commented on May 5, 2023

@weihanglo
Member

I would say it is kinda intentional. The Cargo configuration system only cares about environment variables starting with CARGO_12. Or more precisely, those that listed in the doc are officially supported; those not may be subject to change.

Try this instead:

CARGO_BUILD_RUSTFLAGS="--cfg tokio_unstable" cargo +nightly -Z unstable-options config get build.rustflags

However, we must admit that the story around rustflags management is a bit messy.

Footnotes

  1. https://github.com/rust-lang/cargo/blob/2d693e20ea2dd3a55af94c56372c960503a6c673/src/cargo/util/config/key.rs#L31

  2. https://doc.rust-lang.org/cargo/reference/config.html#environment-variables

LukeMathWalker

LukeMathWalker commented on May 5, 2023

@LukeMathWalker
Author

I see, but the docs do mention RUSTFLAGS as well:

build.rustflags

    Type: string or array of strings
    Default: none
    Environment: CARGO_BUILD_RUSTFLAGS or CARGO_ENCODED_RUSTFLAGS or RUSTFLAGS

Extra command-line flags to pass to rustc. The value may be an array of strings or a space-separated string.
LukeMathWalker

LukeMathWalker commented on May 5, 2023

@LukeMathWalker
Author

The usecase here is being able to accurately predict what extra flags will be passed when invoking rustc or rustdoc without actually performing the invocation.
If cargo config get shows a value that differs from what cargo actually does, its value is greatly diminished.

added
S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Z-cargo-configNightly: cargo config subcommand
and removed
S-triageStatus: This issue is waiting on initial triage.
S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
on May 6, 2023
weihanglo

weihanglo commented on May 6, 2023

@weihanglo
Member

Since RUSTFLAGS is essentially not a cargo configuration, I guess it won't be included in cargo config get in the near future. #11452 (comment) also pointed out this issue. This comment also listed ways to configure rustflags.

For now, I'll triage this issue as A-documenting-cargo-itself Area: Cargo's documentation . Like you just said in #12087 (comment), we can improve the doc of build.rustflags a bit. We can state that setting CARGO_ENCODED_RUSTFLAGS or RUSTFLAGS won't set the config value. Instead, we can say they affect build.rustflags.

added
S-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review
on May 6, 2023
Bhardwaj-Himanshu

Bhardwaj-Himanshu commented on May 7, 2023

@Bhardwaj-Himanshu
Contributor

After going through the discussion above, a with very little knowledge of what's going on, I think you guys are talking about changing this-

build.rustflags

    Type: string or array of strings
    Default: none
    Environment: CARGO_BUILD_RUSTFLAGS or CARGO_ENCODED_RUSTFLAGS or RUSTFLAGS

Extra command-line flags to pass to rustc. The value may be an array of strings or a space-separated string.

to-

build.rustflags

    Type: string or array of strings
    Default: none
   --> Environment: CARGO_BUILD_RUSTFLAGS or CARGO_ENCODED_RUSTFLAGS or RUSTFLAGS <-- N-stating this line    

Extra command-line flags to pass to rustc. The value may be an array of strings or a space-separated string.

But Idk what N-stating a line means, so if you could shower some more light on the issue,I would be happy to do the doc work for this issue!
Thanks

LukeMathWalker

LukeMathWalker commented on May 7, 2023

@LukeMathWalker
Author

I'd like to understand this better:

Since RUSTFLAGS is essentially not a cargo configuration

Are we saying that cargo ignores entirely RUSTFLAGS and it's rustc that picks it up on its own? If that's the case, I agree, this is a documentation issue.
But if that is not the case (i.e. cargo does look at RUSTFLAGS and performs some behaviour according to its value), I think the documentation is correct and this should be treated as a bug in cargo config.

My impression is that the latter is happening.

6 remaining items

cnpryer

cnpryer commented on Jul 16, 2023

@cnpryer
Contributor

I got an idea for this. Since rustflags is an important config for Cargo, we could have a separate cargo config rustflags unstable subcommand, showing the unmerged and merged flags with the origins.

This is not yet accepted by the Cargo team but is definitely not a bad idea to me.

Is there any interest in this?

LukeMathWalker

LukeMathWalker commented on Jul 16, 2023

@LukeMathWalker
Author

Most definitely—it would be a solution here.

weihanglo

weihanglo commented on Jul 16, 2023

@weihanglo
Member

To avoid any misleading, I'll relabel this issue for some solution like cargo config rustflags. And the solution might involve lots of different place in the codebase, so it might be a bit not easy.

@rustbot label +S-needs-design -S-accepted +E-hard -E-easy

added
E-hardExperience: Hard
S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
and removed
S-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review
E-easyExperience: Easy
on Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documenting-cargo-itselfArea: Cargo's documentationA-rustflagsArea: rustflagsC-bugCategory: bugE-hardExperience: HardS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.Z-cargo-configNightly: cargo config subcommand

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @weihanglo@cnpryer@LukeMathWalker@rustbot@Bhardwaj-Himanshu

        Issue actions

          `cargo config get` doesn't seem to pick up RUSTFLAGS or RUSTDOCFLAGS · Issue #12087 · rust-lang/cargo