rust-lang / rust-lang/rust-analyzer

build scripts fail when using environment variable CARGO_BUILD_TARGET_DIR and setting rust-analyzer.cargo.extraEnv in settings.json

Open
#13,729 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'm trying to redirect check on save and rust analyzer builds to another location, however I've met some problems along the way

This is a two-fold bug report:
1: build scripts fail as the title says
2: cargo check fails without an explicit path (cannot use env var as I have)

Why it matters?
Saving on SSD usage by redirecting all builds to a ramdrive

For example, my project here, which sets the build dir to a unique ramdrive path (the reason why I have to use dynamic variables, so I can keep things in sync between vscode and powershell)
https://github.com/MolotovCherry/Cargo-RamDrive

Build scripts fail when setting rust-analyzer.cargo.extraEnv to env var in settings.json

When setting the following in settings.json

    "rust-analyzer.cargo.extraEnv": {
        "CARGO_BUILD_TARGET_DIR": "${env:CARGO_BUILD_TARGET_DIR}"
    },
    "rust-analyzer.checkOnSave.extraEnv": {
        "CARGO_BUILD_TARGET_DIR": "R:\\some\\path"
    },

Rust analyzer fails on startup with

failed to run build scripts Check the server logs for additional info

Note: VsCode was started in a shell with CARGO_BUILD_TARGET_DIR already set.
The contents of CARGO_BUILD_TARGET_DIR point to a valid directory, for example R:\Temp\rust\project-2140558008

The log error reads

[ERROR rust_analyzer::lsp_utils] failed to run build scripts

error: The directory name is invalid. (os error 267) at path "c:\\Users\\myusername\\Desktop\\rustfolder\\${env:CARGO_BUILD_TARGET_DIR}stX7G8"
Workaround

Disable build scripts in settings
"rust-analyzer.cargo.buildScripts.enable": false,

It's also worth noting that removing these configs and just starting VsCode with CARGO_BUILD_TARGET_DIR already set also works. Even so, I still think this is likely a bug.

Cargo check fails when using env var

Cargo check also fails if you use "${env:CARGO_BUILD_TARGET_DIR}" as the value. Not sure why it does

    "rust-analyzer.checkOnSave.extraEnv": {
        "CARGO_BUILD_TARGET_DIR": "${env:CARGO_BUILD_TARGET_DIR}"
    },
Workaround

Use explicit path;
It's also worth noting that removing these configs and just starting VsCode with CARGO_BUILD_TARGET_DIR already set also works. Even so, I still think this is likely a bug.


rust-analyzer version: rust-analyzer version: 0.3.1309-standalone (a2beeb8db 2022-12-04)

rustc version: rustc 1.67.0-nightly (e75aab045 2022-11-09)

relevant settings: N/A / see above

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

Reproduce the issue with rust-analyzer.cargo.extraEnv and rust-analyzer.checkOnSave.extraEnv in VS Code, using CARGO_BUILD_TARGET_DIR as shown. Start by tracing how these settings are passed to build scripts and cargo check; done means both operations accept the environment-variable value without invalid paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
build-system, devtools
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.