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

Get structured cargo config info #9342

Closed
Undin opened this issue Apr 10, 2021 · 7 comments
Closed

Get structured cargo config info #9342

Undin opened this issue Apr 10, 2021 · 7 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@Undin
Copy link
Contributor

Undin commented Apr 10, 2021

I want to get target info defined in cargo config to improve IDE support (in my case it's about IntelliJ Rust plugin but I suppose it may be useful for any IDE integration). More precisely, I want to fetch correct info about cfg options from the compiler if a user uses some specific target to compiler the project to provide the corresponding highlighting, name resolution and other code insight features that may depend on cfg options. For example, see intellij-rust/intellij-rust#5183.

According to https://doc.rust-lang.org/cargo/reference/config.html, config file may be placed not only in project dir but also in parent directories. Moreover, some specific options can be overridden by environment variables. And all this logic is already implemented in cargo and will be updated in the future if rules about config files are changed.
So, question: is there a way to get structured output about cargo config from cargo (or at least, info about target) not to re-implement described logic in the plugin?

If it's not possible for now, what's the best place for it? A separate command (something like cargo config), cargo metadata or anything else?

@Undin Undin added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Apr 10, 2021
@Undin
Copy link
Contributor Author

Undin commented Apr 10, 2021

CC @ehuss

@ehuss
Copy link
Contributor

ehuss commented Apr 12, 2021

Sorry, I'm a bit confused about the discussion of config files and how that relates to target cfg values.

On nightly, you can use cargo rustc --print cfg to display cfg values from the compiler.

@Undin
Copy link
Contributor Author

Undin commented Apr 13, 2021

Sorry, I'm a bit confused about the discussion of config files and how that relates to target cfg values.

Sorry if the question was not clear. We want to support cases when a project is supposed to be built with a non-host target.
One subtask is to evaluate cfg attributes according to target option to provide proper name resolution, completion, etc.
To do it, we need to retrieve the corresponding info from some source. Here there are a lot of implementation variants but we want to support the existing one (Cargo config) not to introduce a new entity for users. Hence, we need to get info from Cargo config somehow.

On nightly, you can use cargo rustc --print cfg to display cfg values from the compiler.

Yeah and it actually takes into account cargo config. But unfortunately, this command requires you to pass the concrete target if there are several ones and compiles it. The latest thing makes this option unusable for us because it may require a lot of time and may fail because of different reasons.
So it would be great to get config info without project compilation.

@ehuss
Copy link
Contributor

ehuss commented Apr 13, 2021

this command requires you to pass the concrete target if there are several ones and compiles it.

That command shouldn't trigger a compile. It should print something like the following and exit immediately. Are you not seeing that?

~/Temp/z1> cargo +nightly rustc --print=cfg -Zunstable-options
debug_assertions
panic="unwind"
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_has_atomic_equal_alignment="16"
target_has_atomic_equal_alignment="32"
target_has_atomic_equal_alignment="64"
target_has_atomic_equal_alignment="8"
target_has_atomic_equal_alignment="ptr"
target_has_atomic_load_store="16"
target_has_atomic_load_store="32"
target_has_atomic_load_store="64"
target_has_atomic_load_store="8"
target_has_atomic_load_store="ptr"
target_os="linux"
target_pointer_width="64"
target_thread_local
target_vendor="unknown"
unix

@Undin
Copy link
Contributor Author

Undin commented Apr 13, 2021

Oh, looks awesome!
Actually, I ran cargo rustc -- --print cfg because I knew that it worked and thought that you just missed -- in command. And cargo rustc -- --print cfg tries to compile the target.
But with nightly toolchain cargo rustc --print cfg works as expected and it's an answer to my initial question (and in fact it's a duplicate of #8923). Thanks!

Some questions about the feature itself:

  • Do I understand correctly that the option will be released in 1.52?
  • Are there plans to stabilize it?

@ehuss
Copy link
Contributor

ehuss commented Apr 13, 2021

It is a part of 1.52, but is unstable so should only be used on nightly.

No particular plans right now. I opened #9357 for more discussion.

@Undin
Copy link
Contributor Author

Undin commented Apr 13, 2021

@ehuss Thank you!
I think this issue can be closed then

@Undin Undin closed this as completed Apr 13, 2021
bors bot added a commit to intellij-rust/intellij-rust that referenced this issue Apr 14, 2021
7093: CARGO: fetch cfg options according to Cargo config r=vlad20012 a=Undin

Now the plugin use `cargo rustc --print cfg` instead of raw `rustc --print cfg` that allows taking [Cargo config](https://doc.rust-lang.org/cargo/reference/config.html) into account, i.e. fetch cfg options for custom build target, `RUSTFLAGS`, etc.
As a result, evaluation of `cfg` attributes also takes such custom options into account and users can set up them.

Note, `cargo rustc --print cfg` option is available only since Rust 1.52. With older toolchain, the plugin uses host `cfg` options

Fixes #5183
Part of #6104
Related to rust-lang/cargo#9342

<img width="358" src="https://user-images.githubusercontent.com/2539310/114613416-fba27200-9cab-11eb-9b41-97457858ec54.png"/>

<img width="358" alt="Screen Shot 2021-04-12 at 15 12 51" src="https://user-images.githubusercontent.com/2539310/114392523-95312d00-9ba1-11eb-849a-827bae53f69a.png">



changelog: Now the plugin takes [Cargo config](https://doc.rust-lang.org/cargo/reference/config.html) into account during evaluation of `cfg` conditions. Note, it works since Rust 1.52.0


Co-authored-by: Arseniy Pendryak <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

2 participants