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

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"

.vscode\settings.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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 47 (8 by maintainers)

Commits related to this issue

Most upvoted comments

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

Looks like these keys keep changing. In the latest rust analyzer, it is this one for me

{
    "rust-analyzer.cargo.target": "x86_64-unknown-none",
    "rust-analyzer.check.allTargets": false,
    "rust-analyzer.check.extraArgs": [
        "--target",
        "x86_64-unknown-none"
    ]
}
{
    "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!

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

.vscode\settings.json file:

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

Weirdly this issue occurred for me when simply copying a directory with an existing sample project, no issue in the original one but can't find crate for 'test' in the new one. Since rust-analyzer changed over the years, and this thread is currently 4 years old, here is a currently working solution (VSCode 1.90, rust-analyzer v0.3.1995) on Linux: File .vscode/settings.json:

{
    // The "target" line is optional, can be skipped if you have this in the .cargo/config.toml file:
    // [build]
    // target = "thumbv6m-none-eabi"
    "rust-analyzer.cargo.target": "thumbv6m-none-eabi",
    "rust-analyzer.cargo.allTargets": false
}

Generally, when you try this or any other solution in settings.json, pay attention that the lines you’ve pasted aren’t grayed out. You can always start typing and follow the auto-completion to check for changed syntax. This error message appears because by default the extension has Pass --all-targets to cargo invocation, and (probably) since we are #![no_std] in this case, we must set it to a specific target. This probably could be resolved with a fix in rust-analyzer.

Please reopen issue

Not working for me either. Config:

{
  "rust-analyzer.cargo.target": "x86_64-unknown-none",
  "rust-analyzer.check.allTargets": false,
  "rust-analyzer.check.extraArgs": ["--target", "x86_64-unknown-none"]
}

I was having the same issue and after a little poking around I don’t think this is an issue with the rust-analyzer extension, but actually the panicbit.cargo extension. It appears to run cargo check on its own, which is running into issues with not properly knowing the target (I think), so it complains about not being able to find the test crate. If you’re having the same problem but the fixes here aren’t working, I’d check if that extension is installed, try disabling it, and see if the error persists.

I am getting this error again. Is anyone else? I didn’t use to get it with

    "rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.overrideCommand": [
        "cargo",
        "clippy",
        "--message-format=json",
        "--bins",
        "--all-features",
        "--target",
        "thumbv7em-none-eabihf",
    ],

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.

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

ok, sounds like the issue is now resolved, at least there’s a documentation trail for this here now 😃

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",
}

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.