rust-analyzer: "cargo metadata failed: No such file or directory (os error 2)"

I’m trying to work on a project that the RLS extension has no problem handling but I’m getting an odd error from rust-analyzer that is pretty unhelpful:

rust-analyzer failed to load workspace: cargo metadata failed: No such file or directory (os error 2)

What file or directory could not be found? Why couldn’t it be found even though RLS works without a hitch?

I’m running the VSCode extension remotely (Docker) with VSCode Insiders and rust-analyzer at commit 759100fb0dcb41518f2a593dae5de5bbedd07776.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 68 (38 by maintainers)

Commits related to this issue

Most upvoted comments

@marcogroppo Yes, I can cargo and rustc without a problem. They’re included in my PATH via .zshrc:

export PATH="$HOME/.cargo/bin:$PATH"

Not sure if it matters that I’m using zsh…

This is also an issue on macOS, where there’s no trivial way to set environment variables for GUI apps.

This is interesting. RLS depends on cargo as a package. I suppose they don’t use cargo cli then…?

Related stackoverflow question on why this is an issue for Mac users that is not as easy to fix as on Linux or Windows: https://stackoverflow.com/questions/135688/setting-environment-variables-on-os-x

Actually I’m already working on a fix

You can use launchctl setenv PATH $PATH to …

Adding that line to the end of my .profile (which I know is executed by my shell) did not solve the problem for me, but the launching-from-terminal workaround does.

Regardless, if someone wants to actually fix this in rust-analyzer, I’ll remind people that @matklad (primary maintainer here IIUC) commented farther above in this thread that

The 90% solution here is to try to detect rustc/cargo in standard installation paths without any additional configs.

I imagine this wouldn’t be terribly difficult to make a PR for. Maybe I’ll get around to making a PR for this soon.

Archlinux 5.5.13 rustc 1.42.0 cargo 1.42.0 emacs 27.0.9

Also got same error when trying to use emacs daemon. I added a PATH env var to my systemd service file to ensure it found cargo and seems to have fixed it for me.

note - on exact same Rust project, I didn’t get this error if I ran emacs alone (without the daemon).

`[Unit] Description=Emacs: the extensible, self-documenting text editor Documentation=man:emacs(1) info:Emacs

[Service] Type=forking ExecStart=/usr/bin/emacs --daemon ExecStop=/usr/bin/emacsclient --eval “(progn (setq kill-emacs-hook nil) (kill-emacs))” Restart=on-failure Environment=DISPLAY=:%i Environment=HOME=/home/wayc Environment=PATH=/home/wayc/.cargo/bin:/usr/local/bin:/usr/bin:/bin TimeoutStartSec=0

[Install] WantedBy=default.target `

Got it working, thanks! Ended up just creating a wrapper script and launched that from the custom .desktop file saved to ~/.local/share/applications/

Is rustc in PATH for the proceeds which starts the server?

On Wednesday, 4 March 2020, GuzTech notifications@github.com wrote:

I’ve just installed rust-analyzer from the VS Code marketplace, and I have the same problem:

Version: 1.42.1 Commit: c47d83b293181d9be64f27ff093689e8e7aed054 Date: 2020-02-17T09:32:31.598Z Electron: 6.1.9 Chrome: 76.0.3809.146 Node.js: 12.4.0 V8: 7.6.303.31-electron.0 OS: Linux x64 5.5.7-zen1-1-zen

and the output of the rust-analyzer is:

[ERROR rust_analyzer::main_loop] loading workspace failed: Failed to read Cargo metadata from Cargo.toml file /home/oguz286/Projects/Rust/rcw/Cargo.toml

Caused by:
    0: Failed to run `cargo metadata --manifest-path /home/oguz286/Projects/Rust/rcw/Cargo.toml`
    1: No such file or directory (os error 2)
    2: No such file or directory (os error 2)

[ERROR ra_project_model] failed to get rustc cfgs: Failed to get output from rustc --print cfg -O

I just did cargo new rcw so it’s a clean project.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rust-analyzer/rust-analyzer/issues/3118?email_source=notifications&email_token=AANB3M7KZSA4YCKTV4K5FWDRF3I7RA5CNFSM4KTVCK7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEN2WSCA#issuecomment-594897160, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANB3M2JHMDGWMXWL5UDAULRF3I7RANCNFSM4KTVCK7A .

I think you can set environment variables in the Dockerfile? Our at least in devcontainer.json.