-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Pass cfg
s to cargo metadata
to discover correct subset of target.cfg(...)
dependencies
#19846
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
Comments
cargo metadata
, target.cfg(...)
dependencies not discoveredcfg
s to cargo metadata
to discover correct subset of target.cfg(...)
dependencies
This does sound like a hole in the |
Thanks for confirming, created the respective issue here: rust-lang/cargo#15576 |
Thinking about this a bit more and reading @epage's comment over on the cargo issue -- I think this is purely a rust-analyzer issue: In the end, it introduces a "side channel" for cfgs that the rest of the toolchain doesn't know about. I feel the naming of In the work project we've decided to just also set the corresponding RUSTFLAGS via Maybe it's just a documentation issue for |
@seritools I've filed #19926 about this - I agree it's a confusing situation. |
We should definitely document this at the very least yes. I am not sure we can modify RUSTFLAGS though, I can see this causing a lot of (confusing) issues for users |
Uh oh!
There was an error while loading. Please reload this page.
(This is somewhere between a bug report and a feature request)
At work we've got a setup where various
cfg
s are being set depending on outside configuration. For rust-analyzer, we set these via therust-analyzer.cargo.cfgs
setting:This works fine as long as all
cfg
checks remain only in Rust code. We did start, however, to use target dependencies based on thesecfg
s as well:Sadly, rust-analyzer doesn't recognize and load these dependencies, and the relevant imports and code lose all LSP benefits.
A minimal reproduction repository can be found here: https://github.com/seritools/cfg-dependencies-repro
I've looked into what's happening and traced it back to the
cargo metadata
call inCargoWorkspace::fetch_metadata_
: Generally, rust-analyzer passes the--filter-platform
argument tocargo metadata
, to filter out dependencies for other targets. It seems that--filter-platform
causescargo metadata
to evaluate alltarget.'cfg(...)'
configurations, including those using customcfg
s.Finally, the problem is that
cfg
s set via therust-analyzer.cargo.cfgs
setting are not passed tocargo metadata
, meaning that the returned metadata becomes out of sync with the expected config.To confirm that that is the problem (and as a hacky proof-of-concept solution) I patched rust-analyzer to inject the specified
cfg
s viaCARGO_ENCODED_RUSTFLAGS
: master...seritools:rust-analyzer:extra-cfgs-for-cargo-metadataI've confirmed that this allows
cargo metadata
to return the correct dependency tree. This proof of concept solution is of course not sane for general use (because of how it breaksRUSTFLAGS
usage), but I hope it shows the issue and and idea for resolution.Personally, I couldn't think of a way to implement this sanely that doesn't also involve extending
cargo metadata
-- something akin to--filter-cfg
to pass in extra cfgs. What do you think?If that seems reasonable, I'll also create an issue in the cargo repository and link it to this one.Created the respective cargo issue: rust-lang/cargo#15576The text was updated successfully, but these errors were encountered: