Rust analyzer workspace specific settings with linked projects

I'm working on an embedded application where I'm following the project layout described by ferrous systems.
In short:

  • I have a workspace with multiple projects that are libs, containing most of my application code. These libs contain unittests and can be built with the normal host compiler.
  • In a subfolder I have another workspace which mainly contains binaries (that depend on the libs). These binaries are cross-compiled to the target only.
+-- root
    +-- lib1
        +-- src
        +-- tests
        +-- ...
        +-- Cargo.toml <- normal lib, contained in workspace
    +-- lib2
        +-- src
        +-- tests
        +-- ...
        +-- Cargo.toml <- normal lib, contained in workspace
    +-- cross-bin
        +-- .cargo/config.toml <- cross compiler setup
        +-- src
            +-- ..
        +-- Cargo.toml <- cross build, depends on lib1 & lib2, not in workspace
    +-- Cargo.toml <- workspace config for lib1 & lib2

The cross-bin can't be part of the workspace since it's .cargo/config.toml would be ignored in that case.
To get rust analyzer to still provide info for the embedded build I set the two as linked projects (in VSCode):

    "rust-analyzer.linkedProjects": [
        "Cargo.toml",
        "cross-bin/Cargo.toml",
    ],

This creates one problem: By default VSCode will run clippy on all targets - which lets clippy (and therefore rust-analyzer) output an error for the cross-compiled binary:

error[E0463]: can't find crate for `test`

The linked article therefore recommends to disable --all-targets in the config.
But this creates the "issue" that e.g. clippy is not run on the test cases.

Is there a way to have different configs for the two workspaces, given the project structure that we want to make testing easier?

I don't think clippy is run by default, did you replace check with clippy for the check on save
command?

I don't know how (or if) you can set different configs in this case, although you have two cargo workspaces, it's in the same vscode workspace. I don't think vscode can open multiple workspaces at the same time (BTW, a multi-root workspace is still a single workspace)

I didn't test this, but does it help if you set test and/or harness to false for the cross-compiled binary target?

[[bin]]
name = "..."
path = "..."
test = false
harness = false

Yes we do, but the same issue would occur with cargo check alone.

That was one reason why I was looking for a solution for this on the rust-analyzer side, also because some of us use neovim (or other editors)

I thought I had tried this, but it seems I only thought about it, sry.
This works, though I have to specify every binary manually and can't rely on autobins (since there doesn't seem to be a way to set test = false for all binaries.

Small price to pay though - Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.