rust-lang / rust-lang/rust-analyzer

RA doesn't properly resolve _relative_ env vars from `.cargo/config.toml`

Open
#18,143 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rust-analyzer version: rust-analyzer version: 0.3.2112-standalone (94b526fc8 2024-09-15) [/Users/greg/.vscode/extensions/rust-lang.rust-analyzer-0.3.2112-darwin-arm64/server/rust-analyzer]

rustc version: rustc 1.81.0 (eeb90cda1 2024-09-04)

editor or extension: vscode v0.3.2112

relevant settings:

Any .cargo/config.toml setting a relative env var. For example: https://github.com/rust-lang/rust-analyzer/blob/990c48cb0df6cd6aed2c584dfaa6940406abc28b/.cargo/config.toml#L12-L13

repository link (if public, optional):

code snippet to reproduce:

The [env] table in .cargo/config.toml allows for values to specified as relative paths, in which case they should be resolved to absolute paths that are relative to the .cargo dir. See https://doc.rust-lang.org/cargo/reference/config.html#env. An example is:

[env]
FOO = { value = ".", relative = true }

Rust-Analyzer does not convert this to a proper absolute path.

The relevant code is here:

https://github.com/rust-lang/rust-analyzer/blob/990c48cb0df6cd6aed2c584dfaa6940406abc28b/crates/project-model/src/env.rs#L72-L113

We an see that RA runs a command like: cargo -Zunstable-options config get env, which will output something like the following, assuming the example toml snippet above:

env.FOO.relative = true
env.FOO.value = "."

We can see that RA strips the .value suffix

https://github.com/rust-lang/rust-analyzer/blob/990c48cb0df6cd6aed2c584dfaa6940406abc28b/crates/project-model/src/env.rs#L106

and ignores the .relative field.

A Fix

One way to fix this would be to compute the absolute path by joining the relative path with the config file's path.

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 in crates/project-model/src/env.rs, especially the environment parsing around the cargo config output described in the issue. Reproduce the cargo -Zunstable-options config get env output with an [env] value marked relative = true, then trace how the .value and .relative fields are handled. Done means relative values resolve against the .cargo directory as absolute paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.