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

Supporting Cargo Features #1822

Closed
sanbox-irl opened this issue Sep 11, 2019 · 15 comments
Closed

Supporting Cargo Features #1822

sanbox-irl opened this issue Sep 11, 2019 · 15 comments
Labels

Comments

@sanbox-irl
Copy link

Hello!

I have been using gfx_hal (a common crate if doing graphics work in Rust), and make great use of Cargo conditional compilation features.

The Rust analyzer is great, but has no support for them unless I set a default. I can't git ignore my cargo.toml, so I'm forced to constantly have to revert this change on each computer I use, or commit meaningless changes.

Thanks so much for all the hard work! any help would be great :)

@matklad matklad added the E-hard label Sep 12, 2019
@sanbox-irl
Copy link
Author

Forgive asking this -- we have the ability to make something a default for a feature in our Cargo.toml and the RA successfully reads and honors that. Is it possible for us to just piggy back off of that? Really, just having a .rust-analyzer.toml file where I can write something like that, which I can safely gitignore, is perfectly acceptable.

@matklad
Copy link
Member

matklad commented Sep 13, 2019

It’s won’t be completely wrong to say that rust-analyzer ignores features completely at the moment. There are only two small places where we honor features:

  1. when calling cargo metadata, we pass —all-features. I don’t think we need anything more here, ideally, as features should be additive
  2. default features are picked up by cargo watch command. You can configure this command in the settings.

@sanbox-irl
Copy link
Author

@matklad 2 is a good temporary substitute, but since it's done extension wide, it's not very useful. Work and hobby project have different features.

Even worse, we don't get an error. Instead, for example, I get this in cargo-watch trace:

Fail to parse to json : { SyntaxError: Unexpected token R in JSON at position 1 }Error on cargo-watch : {
error: Package `--v0.1.0 (/Users/--/Documents/Rust/--)` does not have these features: `metal`
}
Fail to parse to json : { SyntaxError: Unexpected token F in JSON at position 1 }

So I think a simple .ra-lsp.toml is in order to fix this.

@lnicola
Copy link
Member

lnicola commented Sep 19, 2019

You can configure the extension settings for each (Code) workspace with the "Open Workspace Settings" command.

@sanbox-irl
Copy link
Author

Fair dues for me right now, but I don't think that will work for other

@eminence
Copy link
Contributor

I'm checking in on this old issue. I think this might be solved now:

  • rust-analyzer has controls for how the current crate is built (via rust-analyzer.cargo.features, rust-analyzer.cargo.allFeatures, and rust-analyzer.cargo.noDefaultFeatures settings)
  • rust-analyzer can parse a Cargo.toml to learn about the features activated for a dependent crate and provide completions accordingly.

Is there anything that's still missing today?

@matklad
Copy link
Member

matklad commented May 27, 2020

Yeah, I think it makes sense to close this!

One thing we might want to do, though, is to automatically propagate settings from rust-analyzer.cargo to rust-analyzer.checkOnSave, but that needs a new issue

@matklad matklad closed this as completed May 27, 2020
@sanbox-irl
Copy link
Author

Thank you all! I should have remembered to close, but those new features have made this excellent for me

@Lucretiel
Copy link

Follow-up: is it possible to configure this on a per-crate basis when working in a cargo workspace? We're running into the problem where we only want certain features enabled by default (so allFeatures is no good) but those features only apply to one of the several crates in the workspace. It's causing errors when trying to build just other crates.

@bjorn3
Copy link
Member

bjorn3 commented Sep 10, 2021

Does cratename/featurename work?

@Lucretiel
Copy link

Does cratename/featurename work?

Just tried it, no luck.

@stephanemagnenat
Copy link

stephanemagnenat commented Sep 21, 2021

Same issue here, is there an issue for enabling features per-crate within a workspace?

@Veykril
Copy link
Member

Veykril commented Sep 21, 2021

#10298 has been opened for that

@threema-donat
Copy link

I'm checking in on this old issue. I think this might be solved now:

* rust-analyzer has controls for how the current crate is built (via `rust-analyzer.cargo.features`, `rust-analyzer.cargo.allFeatures`, and `rust-analyzer.cargo.noDefaultFeatures` settings)

* rust-analyzer can parse a Cargo.toml to learn about the features activated for a dependent crate and provide completions accordingly.

Is there anything that's still missing today?

I did not find the setting rust-analyzer.cargo.allFeatures, although it would be very handy. Was this removed recently or was this mistakenly listed?

@lnicola
Copy link
Member

lnicola commented Sep 19, 2023

"rust-analyzer.cargo.features": "all"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants