Skip to content

"cargo metadata failed: No such file or directory (os error 2)" #3118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
samuela opened this issue Feb 12, 2020 · 69 comments · Fixed by #4329
Closed

"cargo metadata failed: No such file or directory (os error 2)" #3118

samuela opened this issue Feb 12, 2020 · 69 comments · Fixed by #4329

Comments

@samuela
Copy link

samuela commented Feb 12, 2020

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 759100f.

@Veetaha
Copy link
Contributor

Veetaha commented Feb 12, 2020

@samuela, thank you for the report! Can you please describe the directory structure of the project you open in VSCode that fails?

@samuela
Copy link
Author

samuela commented Feb 12, 2020

It's https://github.com/samuela/rustybox. The Cargo.toml is in the root and then the entrypoint (rustybox.rs) is also in the root. I know that it's conventional to put things in src/ but this is how c2rust spit things out for better or worse. It still works fine with the rest of the rust toolchain.

@Veetaha
Copy link
Contributor

Veetaha commented Feb 12, 2020

May I ask you to dump VSCode > Help > About window output here?

@Veetaha
Copy link
Contributor

Veetaha commented Feb 12, 2020

Cloned the repo, installed the specified version of rust-analyzer, freshly installed the following code-insiders:

1.43.0-insider
f4c95d5b9cfb2265f1730d564df1709533eff55e
x64

But couldn't reproduce the error...
I guess we will have to wait until better logging (#3119) is not landed...

@samuela
Copy link
Author

samuela commented Feb 12, 2020

Here's the About info

Version: 1.43.0-insider
Commit: f4c95d5b9cfb2265f1730d564df1709533eff55e
Date: 2020-02-12T05:45:29.569Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 19.2.0

FWIW there's also an info message "workspace loaded, 0 rust packages".

@samuela
Copy link
Author

samuela commented Feb 12, 2020

Also getting this on 5e464d0.

@samuela
Copy link
Author

samuela commented Feb 12, 2020

Here's what I'm seeing in the Output pane:

[ERROR ra_lsp_server::main_loop] loading workspace failed: cargo metadata failed: No such file or directory (os error 2)
[ERROR ra_project_model] failed to get rustc cfgs: No such file or directory (os error 2)
[ERROR ra_lsp_server::main_loop] overly long loop turn: 749.734893ms
[Error - 11:55:50 PM] Request textDocument/formatting failed.
  Message: No such file or directory (os error 2)
  Code: -32603 
[Error - 11:55:52 PM] Request textDocument/formatting failed.
  Message: No such file or directory (os error 2)
  Code: -32603 

@marcogroppo
Copy link
Contributor

@samuela can you run cargo and rustc from the Docker container? Maybe the files that rust-analyzer can't find are these binaries

@matklad
Copy link
Member

matklad commented Feb 13, 2020

It might make sense to retry after #3119 is merged: it should give a better error message.

@samuela
Copy link
Author

samuela commented Feb 14, 2020

@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...

@lnicola
Copy link
Member

lnicola commented Feb 14, 2020

Does it work if you start Code from a terminal?

Depending on how you set PATH, it might not be used by the extension. I was thinking of a common issue that occurs when starting Code from a DE, but the same thing might happen under Docker. The remote Code instance needs to be able to run cargo, but I'm not sure how to check for that.

@samuela
Copy link
Author

samuela commented Feb 14, 2020

@lnicola Yeah I'm guessing it's a PATH issue. I'm starting VSCode on my laptop and then going through the whole "remote connect -> attach to running Docker container" song-and-dance. It prob doesn't help that the Docker container is on a remote machine, connected via SSH and DOCKER_HOST. I'm not sure what that means for the env variables that VSCode sees. For example, I also had to explicitly set the full path to ra_lsp_server:

{
    "rust-analyzer.raLspServerPath": "/root/.cargo/bin/ra_lsp_server"
}

even though ra_lsp_server is actually in PATH. Not sure what I could be doing differently to get around this...

@marcogroppo
Copy link
Contributor

@samuela ok, it's definitely a PATH issue. Maybe you could try following these instructions to set the PATH inside the container: https://code.visualstudio.com/docs/remote/containers-advanced#_adding-environment-variables

@samuela
Copy link
Author

samuela commented Feb 14, 2020

@marcogroppo I can try that. I wanted to avoid messing with devcontainer.json stuff, but open to it if that resolves the issue. I just can't figure out how to set this up for a container that already exists/is running...

@Veetaha
Copy link
Contributor

Veetaha commented Feb 14, 2020

@samuela We merged #3119, this means me should get a better error message here now. Might I ask you to retry reproducing the bug having installed rust-analyzer from latest master commit now?

@samuela
Copy link
Author

samuela commented Feb 14, 2020

@Veetaha Sure thing, I'm now seeing an error popup:

rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /root/rustybox/Cargo.toml

and the same info popup:

workspace loaded, 0 rust packages

@samuela
Copy link
Author

samuela commented Feb 14, 2020

Here's the "Rust Analyzer Language Server" stuff in the Output panel:

[ERROR ra_lsp_server::main_loop] loading workspace failed: Failed to read Cargo metadata from Cargo.toml file /root/rustybox/Cargo.toml
[ERROR ra_project_model] failed to get rustc cfgs: Failed to get output from rustc --print cfg -O

@Veetaha
Copy link
Contributor

Veetaha commented Feb 14, 2020

@samuela May I ask you to try running the following command from /root/rustybox (CWD) and send the output?

cargo metadata --format-version 1 --all-features --manifest-path /root/rustybox/Cargo.toml

@samuela
Copy link
Author

samuela commented Feb 14, 2020

So this is interesting. When I run it in the VSCode Terminal pane I get a whole gob of json output as expected but when I SSH in and attach to the container I get

root@196c35e56dcf:~/rustybox# cargo metadata --format-version 1 --all-features --manifest-path /root/rustybox/Cargo.toml
bash: cargo: command not found

So for some reason the container's default shell is still bash even though VSCode Terminal opens up zsh. So then the question is which one is rust-analyzer using and why? Bash presumably?

@samuela
Copy link
Author

samuela commented Feb 14, 2020

I tried adding export PATH="$HOME/.cargo/bin:$PATH" to my .bashrc in the container. I can now run cargo metadata ... in bash but the primary issue remains.

@marcogroppo
Copy link
Contributor

So then the question is which one is rust-analyzer using and why? Bash presumably?

/bin/sh, probably?

@samuela
Copy link
Author

samuela commented Feb 14, 2020

If I run

bash$ sh
sh$ cd /root/rustybox
sh$ cargo metadata ...
...

it works, but perhaps that's because it's inheriting env vars for the parent bash session.

@samuela
Copy link
Author

samuela commented Feb 14, 2020

I also tried putting export PATH="$HOME/.cargo/bin:$PATH" in my ~/.profile in the hopes it would be read by /bin/sh, but rust-analyzer/VSCode isn't picking up on that either.

@marcogroppo
Copy link
Contributor

@samuela I'm just guessing, but IMHO VSCode by default will try to run something as docker run -ti IMAGE /bin/sh. You can probably override this somehow...

@lnicola
Copy link
Member

lnicola commented Feb 15, 2020

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

@GuzTech
Copy link

GuzTech commented Mar 4, 2020

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.

@lnicola
Copy link
Member

lnicola commented Mar 4, 2020

Can you try running Code from a terminal?

@matklad
Copy link
Member

matklad commented Mar 4, 2020 via email

@GuzTech
Copy link

GuzTech commented Mar 4, 2020

Can you try running Code from a terminal?

Yup

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

How do I check that?

echo $PATH
/home/oguz286/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin

@waylandc
Copy link

waylandc commented Apr 2, 2020

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
`

@lnicola
Copy link
Member

lnicola commented Apr 3, 2020

On Unix, we could sniff /etc/passwd for the user's shell, spawn it, get the configured PATH and look there for the binaries. But it seems a bit overkill.

@cedeerwe
Copy link

cedeerwe commented May 5, 2020

I am failing to find a solution in this thread, is there currently some clean workaround for this? I have installed the rust-analyzer extension on my VSCode, both rustc & cargo can be run from the terminal and rust-analyzer fails due to all the same reasons from this thread.

What should I do? Running Ubuntu 18.04.

@cdisselkoen
Copy link
Contributor

For me (on macOS) the workaround I've been using is to start VSCode via code <dirname> or code . from the terminal, rather than launching it from the Dock or Alfred (ie the taskbar or search thingy on Ubuntu).

@bjorn3
Copy link
Member

bjorn3 commented May 5, 2020

You can use launchctl setenv PATH $PATH to set PATH for all programs, including those not launched from the terminal.

@cdisselkoen
Copy link
Contributor

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.

@cedeerwe
Copy link

cedeerwe commented May 5, 2020

For me (on macOS) the workaround I've been using is to start VSCode via code or code . from the terminal, rather than launching it from the Dock or Alfred (ie the taskbar or search thingy on Ubuntu).

I have tried this and it did not work for me. Now it seems that the issue was that I had other VSCode windows open at the same time and that somehow interfered. I have closed all the windows and then the terminal approach works. Thanks!

@Veetaha
Copy link
Contributor

Veetaha commented May 5, 2020

I think setting CARGO=${HOME}/.cargo/bin/cargo in you .profile will work, rust-analyzer should respect the value of env this variable.
https://github.com/rust-analyzer/rust-analyzer/blob/a4778ddb7a00f552a8e653bbf56ae9fd69cfe1d3/crates/ra_project_model/src/cargo_workspace.rs#L341

@cdisselkoen
Copy link
Contributor

@Veetaha the problem is that $CARGO, like $PATH, is not inherited correctly (i.e. .profile is not read) when VSCode is launched in certain ways on macOS.

@Veetaha
Copy link
Contributor

Veetaha commented May 5, 2020

Okay guys, let me tackle this

@cdisselkoen
Copy link
Contributor

Actually I'm already working on a fix

@Veetaha
Copy link
Contributor

Veetaha commented May 5, 2020

Cool, thanks!

@Veetaha
Copy link
Contributor

Veetaha commented May 5, 2020

@cdisselkoen
There is also minor code duplication of this function in flycheck.
https://github.com/rust-analyzer/rust-analyzer/blob/a4778ddb7a00f552a8e653bbf56ae9fd69cfe1d3/crates/ra_flycheck/src/lib.rs#L338-L340
I am not sure where is the right place for this function to land. My gut feeling is that we should have a shared simple ra_cargo module for this stuff, let's see what @matklad can advise if any.

@cdisselkoen
Copy link
Contributor

I'm also not sure if https://github.com/rust-analyzer/rust-analyzer/blob/a4778ddb7a00f552a8e653bbf56ae9fd69cfe1d3/editors/code/src/cargo.ts#L79 in JS-land will need to be fixed as well. I'm not too familiar with VSCode extensions, or frankly, NodeJS at all.

@Veetaha
Copy link
Contributor

Veetaha commented May 5, 2020

Let's not bother with this one, for now. You can ask for help in the PR regarding TypeScript part, I am sure I or any other person will respond

cdisselkoen added a commit to cdisselkoen/rust-analyzer that referenced this issue May 5, 2020
@cdisselkoen
Copy link
Contributor

^ the above commit partially fixes the problem, but I now get an error about finding rustc at https://github.com/rust-analyzer/rust-analyzer/blob/a4778ddb7a00f552a8e653bbf56ae9fd69cfe1d3/crates/ra_project_model/src/sysroot.rs#L117
We may need a similar solution for finding a path to rustc.

@Veetaha
Copy link
Contributor

Veetaha commented May 5, 2020

Right, I think we can generalize this for a bit passing the env variable, executable name and standard location name so we can also cover rustc and rustup.

cdisselkoen added a commit to cdisselkoen/rust-analyzer that referenced this issue May 5, 2020
cdisselkoen added a commit to cdisselkoen/rust-analyzer that referenced this issue May 5, 2020
@bors bors bot closed this as completed in 8295a93 May 8, 2020
@hzbd
Copy link

hzbd commented Jan 12, 2021

// "rust-analyzer.cargo.features": [
//     "rust-analyzer"
// ],

it works.

@JenuelDev
Copy link

. if your in windows.. I recommend setting up your wsl subsystem...
check if cargo is installed or not..

if your still using wsl subsystem but still does not work check if your have installed rust and cargo..

@BennoCrafter
Copy link

@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...

thank u so much dude

@zeshantech
Copy link

I was facing the same issue, and this command worked for me.

curl https://sh.rustup.rs -sSf | sh
export PATH="$HOME/.cargo/bin:$PATH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.