Setting cargo command to fetch metadata in rust-analyzer in VSCode?

Hi, I need to dynamically set proxy variables before calling Cargo, so I created a wrapper around cargo to do that. I'm trying to make VSCode use that wrapper. Found and set these relevant settings:

    "rust-analyzer.runnables.command": "cargowrap",
    "rust-analyzer.cargo.buildScripts.overrideCommand": ["cargowrap check --quiet --workspace --message-format=json --all-targets"],
    "rust-analyzer.check.overrideCommand": ["cargowrap check --workspace --message-format=json --all-targets"]

But it's not enough, VSCode still runs regular cargo when it's trying to fetch metadata. Is there a VSCode setting I missed perhaps?

Thanks.

Sorry for not addressing your actual question, but is the Cargo http.proxy setting (and friends) adequate for your use case?

They're not, because proxy vars need to be set dynamically. Corp environment requires hopping between different vpns.

1 Like

Hi @yurigo - Did you find a solution for your issue?? I am running into similar issue, cargo metadata fetch is failing in the Rust-analyzer in VS-code.

I need to replace crates.io to the private registry and to do so I prepared a config.toml. Things are working fine from the Terminal (including cargo fetch metadata) but failing in the vs-code rust-analyzer

rust-analyzer.cargo.extraEnv (default: {})

Extra environment variables that will be set when running cargo, rustc or other commands within the workspace. Useful for setting RUSTFLAGS.

may help, i.e.

"rust-analyzer.cargo.extraEnv": {
  "CARGO_HTTP_PROXY": "xxx", // proxy

  "CARGO_REGISTRY_DEFAULT": "", // registry
  "CARGO_REGISTRIES_<name>_INDEX": "", // registry
  "CARGO_REGISTRIES_<name>_TOKEN": "" // registry
}

@vague - I tried setting config file path as environment variable. But, my issue is fixed now - it was some script in my bash profile that is running for more than 60 secs during init causing vscode bail on fetching env variables.

It's good to know we can directly set default registry like you mentioned instead of me defining the config.toml.

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.