Rust-analyzer: `#![no_std]` can't find crate for `test` in vs code

Created on 1 Apr 2020  ·  33Comments  ·  Source: rust-analyzer/rust-analyzer

Hi,

Same issue as #3297 but the solution there (for vim) does not seem to work for vs code on windows unfortunately.

Capture

Here is my setup:

.cargo\config file:

[build]
target = "thumbv7m-none-eabi"

.vscodesettings.json file:

{
    "rust.target": "thumbv7m-none-eabi",
    "rust.all_targets": false
}

src\main.rs file:

#![no_std]
#![no_main]

#[entry]
fn main() -> ! {
  loop {}
}

rust-analyzer version: 0.2.108
vs code version: 1.43.2
operating system: Windows 10
rustc 1.42.0 (b8cedc004 2020-03-09) stable

Most helpful comment

{
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.extraArgs": [
        "--target",
        "thumbv7m-none-eabi"
    ]
}

Thanks for the update @lnicola. Yes it broke but your new settings work, cheers!

All 33 comments

I don't know why they're needed, but those settings are spelled "rust-analyzer.cargo-watch.allTargets": true and "rust-analyzer.cargo-watch.arguments": ["--target", "thumbv7m-none-eabi"].

Thank you @lnicola, that worked!
Here is the required settings file if anyone else comes across this post:

.vscodesettings.json file:

{
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "thumbv7m-none-eabi"
    ]
}

Thank you @lnicola, that worked!
Here is the required settings file if anyone else comes across this post:

.vscodesettings.json file:

{
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "thumbv7m-none-eabi"
    ]
}

The current configuration seems to have changed, can you still work?

{
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.extraArgs": [
        "--target",
        "thumbv7m-none-eabi"
    ]
}

Hello, I'm encountering this issue at https://github.com/Vrixyz/button/commit/b1707a9d09d0fe97f75ba050113c5268816e3377

I tried all your options for the .vscode/settings with no success for a full working solution.

Auto-completion works if I restart VSCode.

Source from https://github.com/rust-embedded/discovery was working correctly, so I did setup my source to use a workspace to match its structure as much as possible, It worked for a moment 🤷 but then the error came back.

PS: I'm on macOS Catalina

{
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.extraArgs": [
        "--target",
        "thumbv7m-none-eabi"
    ]
}

Thanks for the update @lnicola. Yes it broke but your new settings work, cheers!

Hello, I'm encountering this issue at Vrixyz/button@b1707a9

I tried all your options for the .vscode/settings with no success for a full working solution.

Auto-completion works if I restart VSCode.

Source from https://github.com/rust-embedded/discovery was working correctly, so I did setup my source to use a workspace to match its structure as much as possible, It worked for a moment 🤷 but then the error came back.

PS: I'm on macOS Catalina

Unfortunately I'm on Windows so I can't say why it's not working for you on a Mac. Make sure you use the new settings @Inicola posted and not my old settings. The settings can be scoped to your Workspace or User. Works for both. Also make sure you have run "rustup update" recently as there has been a new compiler release a few days ago.

Thanks ❤️ , rustup update fixed it !
It sure feels like a hack though...

I appear to be running in this bug...

I'm working in a workspace. I'm using a custom target. Here's my .vscode/settings.json:

{
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.overrideCommand": ["cross"],
    "rust-analyzer.checkOnSave.extraArgs": [
        "--target",
        "x86_64-unknown-custom"
    ],
}

I also tried without the overrideCommand flag. Any suggestions?

I haven't had to use overrideCommand but for one project I had to change "check" to "xcheck" like so:
"rust-analyzer.checkOnSave.command": "xcheck",

perhaps you could try something like
"rust-analyzer.checkOnSave.command": "cross",
or
"rust-analyzer.checkOnSave.command": "check cross",

All of the above didn't work out for me.
I can still see the warning.

Any ideas, guys?

Thank you @lnicola, that worked!
Here is the required settings file if anyone else comes across this post:

.vscodesettings.json file:

{
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "thumbv7m-none-eabi"
    ]
}

This is either a regression or something else but still seems to be a problem, could anyone reopen it?:

Skärmavbild 2020-08-27 kl  22 31 33

@brainstorm you're using the wrong preferences, see the comments above like https://github.com/rust-analyzer/rust-analyzer/issues/3801#issuecomment-609003603.

Aah, sweet, thanks @lnicola, works now! :)

Another gotcha I have come across is opening vs code in the wrong folder. For example, if you open vs code in a folder without a cargo.toml file (maybe your toml file is in a sub folder of this one) then rust-analyzer will not read your settings.json file. A quick sanity check would be to run "cargo check" on the folder you open vs code in and see if it runs ok.

It seems to not be working for me.

{
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.extraArgs": [
        "--target",
        "thumbv7em-none-eabihf"
    ]
}

still results in the test error in vscode.

@dialtone, Do you have the right target installed?

rustup target add thumbv7m-none-eabi

Another thing to check is your general cargo setup (Cargo.toml, .cargo/config file, workspaces etc). Try another [no_std] project and, if it works, compare it to yours.

Yeah, code compiles and runs on the embedded device when I cargo run in it. And yeah I can run cargo check as well. In any case the setup is here: https://github.com/dialtone/airy

I cloned your repo and got the error message to go away by adding a .vscode/settings.json file in your project root with

{
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.extraArgs": [
        "--target",
        "thumbv7em-none-eabihf"
    ]
}

VS Code needed a restart for rust-analyzer to pick it up.

So the file structure is (I've included the important files):

.vscode/settings.json
.cargo/config
src/lib.rs
Cargo.toml

You were missing the settings.json file.

Well, that file was simply not checked in, but it just doesn't make the errors go away for me for some reason. Happy to do whatever you want to debug this.

I think it would be appropriate to check that file into your repo especially since it would be nice to have a known working repo linked in this thread.

done

Just to be clear though, the repository still doesn't work for me, even with the file checked in and having opened the project from the right directory.

Thanks. Your repo works (without showing the error message) for me on both linux and windows with a fresh checkout. Other than a strangely named .cargo/config file (never seen it with a .toml extension but it doesn't seem to make a difference) I can't spot anything wrong. Do a cargo clean and maybe reinstall your rust-analyzer plugin and restart vs code. Perhaps this is a caching issue. Check your global vs code settings for conflicting settings. Check your repo out to an alternative folder on your machine (maybe rust-analyzer is looking at config files in your parent folder). Try your repo on another machine. Other than that I'm all out of ideas!

Well done all of the above and checked the global settings config and can't spot anything obviously wrong so I'll just keep it broken.

below is my global vscode settings file.

{
    "debug.allowBreakpointsEverywhere": true,
    "editor.minimap.enabled": true,
    "editor.formatOnSave": true,
    "editor.wordWrap": "bounded",
    "editor.wordWrapColumn": 120,
    "editor.multiCursorModifier": "alt",
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
    "editor.cursorBlinking": "smooth",
    "editor.fontFamily": "FiraCode-Retina",
    "editor.fontLigatures": true,
    "editor.fontSize": 12,
    "editor.formatOnPaste": true,
    "terminal.integrated.shell.osx": "/usr/local/bin/fish",
    "terminal.integrated.fontSize": 12,
    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true,
    "workbench.iconTheme": "vscode-icons",
    "workbench.editor.enablePreview": false,
    "diffEditor.renderSideBySide": false,
    "vsicons.dontShowNewVersionMessage": true,
    "explorer.confirmDelete": false,
    "window.zoomLevel": 0,
    // markdown
    "markdown-preview-enhanced.mermaidTheme": "forest",
    // git
    "git.inputValidationLength": 120,
    "git.inputValidationSubjectLength": 80,
    // plain text
    "[plaintext]": {
        "editor.wordWrapColumn": 20000,
    },
    // Python
    "python.formatting.provider": "autopep8",
    "python.formatting.autopep8Args": [
        "--ignore",
        "E402"
    ],
    "[python]": {
        "editor.tabSize": 4,
        "editor.insertSpaces": true,
        "editor.formatOnSave": false
    },
    "python.sortImports.path": "nosorting",
    // rust
    "editor.find.addExtraSpaceOnTop": false,
    "[rust]": {
        "editor.defaultFormatter": "rust-lang.rust"
    },
    "rust-client.channel": "nightly", // Use Rust nightly channel
    "rust.clippy_preference": "on", // Always run Clippy lints
    "rust.unstable_features": true, // Conditionally enable range formatting, not enabled by default
    "rust.all_targets": false,
    // go
    "go.useLanguageServer": true,
    "go.autocompleteUnimportedPackages": true,
    "go.goroot": "/usr/local/opt/go/libexec",
    "go.gopath": "/Users/dialtone/dev/go",
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.fixAll": true,
        }
    },
    "[go.mod]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
    },
    "gopls": {
        "analyses": {
            "fillreturns": true,
            "undeclarename": true,
            "unusedparams": true,
            "nonewvars": true,
        }
    },
    // other
    "C_Cpp.default.cStandard": "c11",
    "C_Cpp.default.intelliSenseMode": "clang-x64",
    "lldb.showDisassembly": "never",
}

@dialtone are you using vscode-rust or rust-analyzer? You seem to have settings for the former, but this is the issue tracker for the latter. You should uninstall vscode-rust if you have it, and install rust-analyzer.

I don't have vscode-rust installed. Some of the settings up there, I can remove them all except the defaultFormatter without which rust-analyzer does no formatting of the code. The end result doesn't change.

Do the settings work? If they don't apply, they should have a gray-ish color. You shouldn't need to set the fornatter like that. And I guess vscode-rust is called differently now?

Looks like that may have done it then. Basically the "official" rust extension on VSCode called "rust-lang.rust" should be removed in favor of "matklad.rust-analyzer"?

If you want to use rust-analyzer, yes, don't install both. Some of us prefer it to the official one (and it might replace it in the future), but YMMV.

ok, sounds like the issue is now resolved, at least there's a documentation trail for this here now :)

We really need to put this in users' faces. For some reason there is a lot
of confusion around this topic.

On Thu, Oct 15, 2020, 12:18 PM Laurențiu Nicola notifications@github.com
wrote:

If you want to use rust-analyzer, yes, don't install both. Some of us
prefer it to the official one (and it might replace it in the future), but
YMMV.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/rust-analyzer/rust-analyzer/issues/3801#issuecomment-709436122,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABBACRBRGODMH2GLIKTPGVLSK4OGPANCNFSM4LYTRETQ
.

Was this page helpful?
0 / 5 - 0 ratings