Skip to content

#![no_std] can't find crate for test in vs code #3801

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

Closed
ninjasource opened this issue Apr 1, 2020 · 47 comments
Closed

#![no_std] can't find crate for test in vs code #3801

ninjasource opened this issue Apr 1, 2020 · 47 comments

Comments

@ninjasource
Copy link

ninjasource commented Apr 1, 2020

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

@lnicola
Copy link
Member

lnicola commented Apr 1, 2020

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"].

@ninjasource
Copy link
Author

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

@liangyongrui
Copy link

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

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

@lnicola
Copy link
Member

lnicola commented Apr 4, 2020

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

@Vrixyz
Copy link

Vrixyz commented Apr 24, 2020

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

@ninjasource
Copy link
Author

ninjasource commented Apr 24, 2020

{
    "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!

@ninjasource
Copy link
Author

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.

@Vrixyz
Copy link

Vrixyz commented Apr 25, 2020

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

@cdmistman
Copy link

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?

@ninjasource
Copy link
Author

ninjasource commented Jul 9, 2020

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

@muttering-oldman
Copy link

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

Any ideas, guys?

@brainstorm
Copy link

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

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

@lnicola
Copy link
Member

lnicola commented Aug 27, 2020

@brainstorm you're using the wrong preferences, see the comments above like #3801 (comment).

@brainstorm
Copy link

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

@ninjasource
Copy link
Author

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.

@dialtone
Copy link

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.

@ninjasource
Copy link
Author

ninjasource commented Oct 14, 2020

@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.

@dialtone
Copy link

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

@ninjasource
Copy link
Author

ninjasource commented Oct 14, 2020

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.

@dialtone
Copy link

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.

@ninjasource
Copy link
Author

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.

@dialtone
Copy link

done

@dialtone
Copy link

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.

@ninjasource
Copy link
Author

ninjasource commented Oct 15, 2020

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!

@dialtone
Copy link

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

@lnicola
Copy link
Member

lnicola commented Oct 15, 2020

@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.

@dialtone
Copy link

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.

@lnicola
Copy link
Member

lnicola commented Oct 15, 2020

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?

@dialtone
Copy link

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"?

@lnicola
Copy link
Member

lnicola commented Oct 15, 2020

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.

@dialtone
Copy link

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

@kjeremy
Copy link
Contributor

kjeremy commented Oct 15, 2020 via email

@lnicola
Copy link
Member

lnicola commented Oct 15, 2020

Well, it's in the manual: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc#vs-code.

@HTGAzureX1212
Copy link

I just ran into this today. The only problem is that I have multiple crates in my project, and that one of them targets x86_64-unknown-uefi and the others all target a custom target. Is there a way to... configure the checkOnSave arguments for both targets?

@andrewgazelka
Copy link

andrewgazelka commented Jan 21, 2022

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

@eqkessel
Copy link

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.

@iFreilicht
Copy link

iFreilicht commented Jun 25, 2022

@andrewgazelka The problem in your config is that you override the command arguments and add --all-targets again, which makes "rust-analyzer.checkOnSave.allTargets": false, have no effect.

I tried this today on a new project, and if you specify the target in .cargo/config.toml like so:

[build]
target = "avr-specs/avr-atmega328p.json"

Then all you need in your .vscode/settings.json is this;

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

bcr added a commit to dcaponi/raw-dog that referenced this issue Sep 1, 2022
You need to specify the target platform or you get some errors.

rust-lang/rust-analyzer#3801
@MolotovCherry
Copy link

MolotovCherry commented Mar 27, 2023

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

@zoeleu
Copy link

zoeleu commented May 19, 2023

None of these worked for me. I still have the same errors.

@XGY4n
Copy link

XGY4n commented Jun 5, 2023

None of these worked for me. I still have the same errors.

same to me, sad .

@zoeleu
Copy link

zoeleu commented Jun 5, 2023

Please reopen issue

@XGY4n
Copy link

XGY4n commented Jun 8, 2023

I create dir and file
xxx
├─.cargo
├─.vscode <- this and add them jscode in setting.json and it works for me
├─src

@zoeleu
Copy link

zoeleu commented Jun 8, 2023

Yes, I also did this. It did not work.

@ibnz36
Copy link

ibnz36 commented Nov 26, 2023

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

@taunusflieger
Copy link

Issue is still there. I got the above error with the following workspace settings:

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

@Hecatron
Copy link

Hecatron commented Mar 5, 2024

I'm noticing while developing for the stm32
this does work in so far as suppressing the missing test crate error

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

But it also seems to have a side effect of preventing errors or warnings showing up of code under examples
I'm only seeing analysis of code under src.

If I set check.allTargets to true then it does check code under examples, but flags that missing test crate error

@l-0-l
Copy link

l-0-l commented Jun 12, 2024

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.

lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Sep 25, 2024
jacsjs added a commit to jacsjs/embedded-rs-lora that referenced this issue Feb 28, 2025
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

No branches or pull requests