rust-lang / rust-lang/rust-analyzer

Custom cargo paths not respected in test code-lenses

Open
#14,106 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-cargo C-support
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

  • VS Code extension: v0.3.1394
  • rust-analyzer binary: v1.67.0

The extension currently allows specifying a custom binaries via environment variables in settings.json. Example:

  "rust-analyzer.server.path": "${workspaceFolder}/bin/rust-analyzer",
  "rust-analyzer.server.extraEnv": {
    "CARGO": "${workspaceFolder}/bin/cargo",
    "RUSTC": "${workspaceFolder}/bin/rustc",
    "RUSTFMT": "${workspaceFolder}/bin/rustfmt"
  },

Which I can verify it is resolved correctly when the server is initialized, by looking at the LSP traces:

    "initializationOptions": {
        "cargoRunner": null,
        "runnableEnv": null,
        "server": {
            "path": "/path/to/repo/bin/rust-analyzer",
            "extraEnv": {
                "CARGO": "/path/to/repo/bin/cargo",
                "RUSTC": "/path/to/repo/bin/rustc",
                "RUSTFMT": "/path/to/repo/bin/rustfmt"
            }
        },
  }

And reading the server code, it looks to me like they should be picked up by toolchain::cargo() by reading the environment variables:

https://github.com/rust-lang/rust-analyzer/blob/eaed19c5399064393e846ad5dddd7b0b290d2582/crates/toolchain/src/lib.rs#L28-L36

However, when executing test code lenses like Run Test or Debug, it seems to not reuse this value, and still try to run cargo from $PATH instead:

image

Which results in the following error in the launched task terminal:

 *  Executing task: cargo test --package PACKAGE_NAME --lib --all-features -- TEST_NAME --exact --nocapture 

 *  The terminal process failed to launch: Path to shell executable "cargo" does not exist. 

Is this a bug? is there some other setting that should be configured as well?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked toolchain/src/lib.rs implementation of toolchain::cargo(), then trace how test code lenses launch Run Test and Debug tasks. Reproduce with the custom CARGO setting and confirm that the launched task uses that path rather than cargo from PATH.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
devtools, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.