rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.28k stars 1.61k forks source link

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

Open devjgm opened 1 month ago

devjgm commented 1 month ago

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.

devjgm commented 1 month ago

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

Actually, it looks like RA doesn't know where the .cargo/config.toml file was, so it may not be trivial to compute the correct absolute path.

Veykril commented 1 month ago

Yes we don't know where the source is, we'd need cargo to somehow inform us about this