Cargo using wrong directory

Hello,

I have an issue with cargo commands. When I do "cargo new example" it is created but i cannot run it since it's looking for .toml file in a wrong directory and i dont know the reason why. Also can't run "cargo clean"

As you can see I am cd in a different folder and it's looking for something in files that don't even exist anymore. I have tried a lot of things even reinstalling rust.

There are two problems here.

First, Cargo thinks you have some already-existing workspace Cargo.toml file, perhaps located in rust_projects or in delete. The error you got from cargo new is because that workspace manifest refers to rust/ledBlink. You should probably delete that file (after examining its contents) because it is not usually a good idea to put all of your Rust code in one workspace (workspaces are best used for actually related code that shares dependencies). But if you want to keep it, then modify it to remove ledBlink from the members list — that will fix the error.


Second, note that it is expected that the sequence

cargo new project_name
cargo run

will not succeed, because cargo new creates a package in a new subdirectory, and cargo run and cargo clean need to be run from inside that subdirectory, and will fail if not inside some package or workspace. This should work (once the workspace is removed or fixed):

cargo new project_name
cd project_name
cargo run

You can also use cargo init to create the files in the current directory instead. (Usually you would do that in an empty directory you created.)

2 Likes

The problem is that it thinks I have some already-existing workspace even though that path doesn't even exist. I have deleted all files and only have one project called "hello". Whatever I try to do it leads me to the wrong path (it thinks there is a workspace somewhere where it can't be because I have deleted that path already). Any ideas ?

it's looking into ~\delete\rust\ledBlink... and I dont even have the file rust nor the ledBlink

But you probably have Desktop/Cargo.toml (judging from slashes, it's likely there), which says something about ledBlink.

I have pretty clean dasktop, only 7 folders of which none are a .toml file. Would it be reading it from trash?

EDIT: I have emptied the recycle bin and still same issue. As I said that folder does not exist.

Folder ledBlink does not exist, of course, that's (almost) exactly what Cargo says. Do you have hidden files being hidden in Explorer? What do you get if you cd to Desktop in your command prompt and do dir /a?

Sadly no hidden files.
Is there a way to change the location of workspace or whatever. Why is it forcing it to that location?

Cargo searches for a Cargo.toml in all parent directories, that is, any of these locations:

C:\Users\Peter\Desktop\delete\rust_projects\Cargo.toml
C:\Users\Peter\Desktop\delete\Cargo.toml
C:\Users\Peter\Desktop\Cargo.toml
C:\Users\Peter\Cargo.toml
C:\Users\Cargo.toml
C:\Cargo.toml

Check every one of those places, find where that file is, and delete it (after checking its contents to make sure it's not anything you wanted to keep).

2 Likes

Cargo really should specify where the workspace root is in its messages. Currently, in cases like this, it amounts to saying: "I can't do that because something is wrong. Somewhere. Good luck!"

Cargo can tell you where your workspace's Cargo.toml file is.

For example, I have a repository called wai in my ~/Documents folder which has a workspace with several crates. By default, the cargo locate-project command will tell you where the Cargo.toml for the "current" crate is.

$ cd ~/Documents/wai/crates/language-server
$ cargo locate-project
{"root":"/Users/michael/Documents/wai/crates/language-server/Cargo.toml"}

In your case, if you ran it inside C:\Users\Peter\Desktop\delete\rust\ledBlink\hello, it'd be the hello directory.

However if you add the --workspace flag, it'll tell you which workspace it thinks your current crate belongs to.

$ cargo locate-project --workspace
{"root":"/Users/michael/Documents/wai/Cargo.toml"}

If you run that command, it should tell you which Cargo.toml file you need to edit/remove.

5 Likes

Daaaaaaamn. For some reason when looking this up online I thought locate-project was a crate you had to install separately. I think its because of the dash between the two words, as this is often present in the names of cargo crates, like cargo-add for example.

the thing is that location C:\Users\Peter\Desktop\delete\rust\ledBlink\hello does not exist since directory "rust" has been deleted a while ago. For some reason it's still looking for a worskpace in that place.
I have also deleted rust and everything it belongs to it for a second time now, still nothing changes.

When I did "rustup self uninstall" it didn't delete everything, but when I did it manually like you suggested and deleted all the cargo folders that did the trick. Thank you sir!

Actually I just decided to go forth and made a little PR just for that.

7 Likes