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

Support specifying #[cfg(panic = ...)] #11794

Closed
andylizi opened this issue Mar 22, 2022 · 5 comments · Fixed by #19243
Closed

Support specifying #[cfg(panic = ...)] #11794

andylizi opened this issue Mar 22, 2022 · 5 comments · Fixed by #19243

Comments

@andylizi
Copy link
Contributor

#[cfg(panic = "...")] was recently stablized in nightly. At the moment, rust-analyzer always uses panic = "unwind", making it difficult to write code for "abort", similar to #7225.

My first thought was to set [profile.dev] panic = "abort" in Cargo.toml, but that didn't work. As it turns out, RA gets the built-in cfgs from running cargo -Z unstable-options rustc --print cfg, which specifically ignores profile settings (rust-lang/cargo#9357):

  • Profiles are ignored. This has historically caused confusion for people (for things like debug_assertions). However, all of cargo ignores profiles for the intended use case (--print=cfg). It's not clear what this should do.

But if we pass --print to rustc directly with cargo rustc --lib -- --print cfg, it will use the settings from profile.dev then. This kind of problem had never came up before, since panic is the first and only cfg that can be affected by cargo profiles (aside from debug_assertions which RA hardcodes anyway).

https://github.com/rust-analyzer/rust-analyzer/blob/b594f9c441cf12319d10c14ba6a511d5c9db1b87/crates/project_model/src/rustc_cfg.rs#L40-L44

@bjorn3
Copy link
Member

bjorn3 commented Mar 22, 2022

cargo rustc --lib -- --print cfg will build all dependencies. It also doesn't work if your package doesn't have a lib target, but only a bin one.

@andylizi
Copy link
Contributor Author

andylizi commented Mar 22, 2022

cargo rustc --lib -- --print cfg will build all dependencies

Ah I didn't notice that. Yeah that won't work then… 😂

@mickvangelderen
Copy link

How does one best work around this?

@Veykril
Copy link
Member

Veykril commented Feb 8, 2025

You can set

    "rust-analyzer.cargo.cfgs": [
        "panic=abort",
    ]

in your settings as a workaround for now

@mickvangelderen
Copy link

mickvangelderen commented Feb 8, 2025

Now both panic="unwind" and panic="abort" are true. That is a problem for me. Can I also unset unwind? The docs only specify "enabling".

Setting RUSTFLAGS to -C panic=abort doesn't help either. Full config:

{
    "rust-analyzer.rustfmt.extraArgs": [
        "+nightly"
    ],
    "rust-analyzer.check.command": "clippy",
    "rust-analyzer.cargo.features": "all",
    "rust-analyzer.cargo.cfgs": [
        "debug_assertions",
        "miri",
        "panic=abort"
    ],
    "rust-analyzer.cargo.extraEnv": {
        "RUSTFLAGS": "-C panic=abort"
    },
}

EDIT: I can work around that use #[cfg(not(panic = "abort"))] for now because there are only two stable panic strategies right now. There may be new ones though.

EDIT 2: It seems that cargo ignores [profile.test] with panic=abort and uses panic=unwind (it prints: `panic` setting is ignored for `test` profile), so I thought maybe I have to set "rust-analyzer.cfg.setTest": false and "rust-analyzer.cargo.features": [] but that doesn't seem to help.

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

Successfully merging a pull request may close this issue.

4 participants