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

How to activate unstable feature restricted_std in rust-analyzer? #16232

Closed
lattice0 opened this issue Jan 3, 2024 · 21 comments
Closed

How to activate unstable feature restricted_std in rust-analyzer? #16232

lattice0 opened this issue Jan 3, 2024 · 21 comments
Labels
A-diagnostics diagnostics / error reporting A-UX C-bug Category: bug

Comments

@lattice0
Copy link

lattice0 commented Jan 3, 2024

rust-analyzer version:
0.3.1791-standalone

rustc version: rustc 1.77.0-nightly (fb5ed726f 2023-12-28)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTC, RUSTUP_HOME or CARGO_HOME)

I'm trying to compile for esp32c6 (riscv32imac-unknown-none-elf). Actually, cargo build runs, but when I save the file rust-analyzer checks it and complains:

error[E0658]: use of unstable library feature 'restricted_std'
   --> /home/lz/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/formatters/json.rs:225:55
    |
225 |     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> ::std::fmt::Result {
    |                                                       ^^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(restricted_std)]` to the crate attributes to enable

As you see, it's complaining about a feature in the std lib, which is strange. I added #![feature(restricted_std)] to main.rs but the error is in the standard library where I cannot add this feature.

I also tried

{
    "rust-analyzer.cargo.cfgs": {
        "build-std": ["core", "std", "alloc", "panic_abort"],
        "restricted_std": true
    },
    "rust-analyzer.check.allTargets": false,
    "rust-analyzer.cargo.target": "riscv32imac-unknown-none-elf"
}

and other variations without success. Also tried

[unstable]
features = ["restricted_std"]

in rust-analyzer.toml.

Also

~/project1$ rustup toolchain install riscv32imac-unknown-none-elf
error: invalid toolchain name: 'riscv32imac-unknown-none-elf'
@lattice0 lattice0 added the C-bug Category: bug label Jan 3, 2024
@Veykril
Copy link
Member

Veykril commented Jan 3, 2024

In what editor is this? It looks like rust-analyzer is trying to compile the standard library for you here which might be an issue with how the editor reports the workspaces.

@lattice0
Copy link
Author

lattice0 commented Jan 3, 2024 via email

@lnicola
Copy link
Member

lnicola commented Jan 3, 2024

rustup toolchain install riscv32imac-unknown-none-elf

rustup +nightly target add riscv32imac-unknown-none-elf

@lnicola
Copy link
Member

lnicola commented Jan 3, 2024

riscv32imac-unknown-none-elf is a no-std target, does "build-std": ["core", "std", "alloc", "panic_abort"] even work there?

@mohe2015
Copy link
Contributor

mohe2015 commented Jan 3, 2024

You could try the equivalent cargo build including the correct target and using -Zbuild-std to check whether this is a rust analyzer issue.

@mohe2015
Copy link
Contributor

mohe2015 commented Jan 3, 2024

Maybe this is rust-lang/wg-cargo-std-aware#87 so you forgot #![no_std] ?

@lattice0
Copy link
Author

@mohe2015 it builds and runs with ˋcargo buildˋ and no ˋ#![no_std]ˋ so I guess no? I actually want to use the std lib. But yeah I added ˋ#![feature(restricted_std)]
#![no_main]ˋ so I could do ˋcargo buildˋ or ˋcargo runˋ etc. How can it work on the command line and not rust-analyzer?

Also @lnicola your suggestion:

rustup +nightly target add riscv32imac-unknown-none-elf
info: component 'rust-std' for target 'riscv32imac-unknown-none-elf' is up to date

@mohe2015
Copy link
Contributor

Is the rust analyzer config to enable build std correct? I'm not sure whether it accepts the build std at that location because I couldn't find any docs and can't test right now

@mohe2015
Copy link
Contributor

mohe2015 commented Jan 19, 2024

@lattice0
Copy link
Author

Ok, if I understood correctly, I'm trying to build, in rust-analyzer, with std, but I actually can only build with restricted_std, for my target. Is this correct? So rust-analyzer does not know what is my target and therefore tries to use std.

@mohe2015
Copy link
Contributor

Ok, if I understood correctly, I'm trying to build, in rust-analyzer, with std, but I actually can only build with restricted_std, for my target. Is this correct? So rust-analyzer does not know what is my target and therefore tries to use std.

Your target does not support std so I personally would add #![no_std] and disable all std features of dependencies. rust-lang/wg-cargo-std-aware#87 is the issue that wants to improve the error message.
See https://github.com/mohe2015/rust-analyzer-issue-16232/tree/main especially the .cargo/config.toml, .vscode/settings.json and src/main.rs.

@lattice0
Copy link
Author

lattice0 commented Jan 19, 2024 via email

@mohe2015
Copy link
Contributor

Yeah with cargo it works just not in rust-analyzer. I think I found the issue. I think rust-analyzer sets the build-std "test" because the commit mohe2015/rust-analyzer-issue-16232@143d64a makes it work for cargo but not rust-analyzer and before it's broken for both.

@lattice0
Copy link
Author

@mohe2015 are you going to merge this?

@mohe2015
Copy link
Contributor

@mohe2015 are you going to merge this?

It's not a commit in rust-analyzer but only in my test project (https://github.com/mohe2015/rust-analyzer-issue-16232/commits/restricted-std). It's also just an assumption what may happen. Somebody would need to find out whether that's actually the case and where in rust analyzer this is set for the project and fix it there. I currently don't have the time to do that.

@lattice0
Copy link
Author

@mohe2015 yes. Unfortunately I'm on vscode.dev so it's a bit hard to compile my own. Any ideas on how to mitigate this with configurations?

@jakzale
Copy link

jakzale commented Mar 25, 2024

I am having a similar issue, while also targeting a variant of riscv32, which doesn't have std available.

I noticed that setting rust-analyzer.check.allTargets to false in .vscode/settings.json gives me diagnostic for my code, whereas setting it to true, doesn't give me any diagnostics.

I know that running cargo check --workspace --all-targets will fail on my codebase with

  • similar errors to error reported by the OP, and
  • those errors are not related to my codebase or my dependencies, because
  • those errors happen in .rustup/toolchain/<toolchain>/lib/rustlib/src/rust/library/test/src/lib.rs.

which seems to cause rust-analyzer to fail silently, as there is no diagnostic message produced on saving source files.

Regarding the errors that are reported, as of current prerelease of rust-analyzer (rust-analyzer 1.79.0-nightly (0ad927c0 2024-03-21)), they occur once, when the server is reloaded, and the are prefixed with

2024-03-25T15:16:50.782873Z ERROR rust_analyzer::main_loop: FetchBuildDataError:
error[E0658]: use of unstable library feature 'restricted_std'
  |
  = help: add `#![feature(restricted_std)]` to the crate attributes to enable
  = note: this compiler was built on 2024-03-09; consider upgrading it if it is out of date

It looks like the reported error is related to a different step within rust-analyzer, that is happening before cargo check.

I suspect that there might be two separate bugs here

  • rust-analyzer using incorrect configuration immediately after server is reloaded, and
  • rust-analyzer failing silently when default cargo check fails due to compilation errors unrelated to the project.

@Veykril
Copy link
Member

Veykril commented Mar 25, 2024

Yes for htese projects you should set .check/build.allTargets to false as otherwise our cargo invocations try to build test targets which require the standard library. We are doing a bad job at telling the user about that as is evident here.

Note that for the FetchBuildDataError we surface that in the status icon (which is not necessarily supported by all clients).

@Veykril Veykril added A-diagnostics diagnostics / error reporting A-UX labels Mar 25, 2024
@jakzale
Copy link

jakzale commented Mar 25, 2024

Yes for htese projects you should set .check/build.allTargets to false as otherwise our cargo invocations try to build test targets which require the standard library. We are doing a bad job at telling the user about that as is evident here.

I don't think you expose .build.allTargets configuration key in VSCode. Neither the Manual mentions it.

Note that for the FetchBuildDataError we surface that in the status icon (which is not necessarily supported by all clients).

Yeah, noticed that in VSCode.

@Veykril
Copy link
Member

Veykril commented Mar 25, 2024

Ah right, #16924 hasn't been merged yet. So we'll expose that soon

@jakzale
Copy link

jakzale commented Apr 2, 2024

I confirm that #16924 solves the issue for me. Just need to put

{
  "rust-analyzer.cargo.allTargets": false,
}

in .vscode/settings.json.

@Veykril Veykril closed this as completed Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics diagnostics / error reporting A-UX C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

5 participants