Skip to content
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

cargo clean and cargo build fails to handle renamed sub-crate in a workspace #9319

Closed
keepsimple1 opened this issue Apr 1, 2021 · 6 comments · Fixed by #9368
Closed

cargo clean and cargo build fails to handle renamed sub-crate in a workspace #9319

keepsimple1 opened this issue Apr 1, 2021 · 6 comments · Fixed by #9368
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug

Comments

@keepsimple1
Copy link

Problem
In a workspace, after doing git mv foo bar to rename a sub-crate, cargo build and cargo clean fails to find renamed sub-crate.

what currently happens:

$ cargo build
error: failed to read `/Users/me/work/foo/Cargo.toml`

Caused by:
  No such file or directory (os error 2)

$ cargo clean
error: failed to read `/Users/me/work/foo/Cargo.toml`

Caused by:
  No such file or directory (os error 2)

what you expected to happen:

cargo build, especially cargo clean can find foo was renamed to bar, and continue to work.

Steps

  1. create a Cargo workspace, including at least 2 crates, say app and foo. The root Cargo.toml looks like:
[workspace]
members = [
    "app",
    "foo"
]

app is an application (executable), while foo is a library. The structure looks like this:

├── Cargo.toml
├── app
│   ├── Cargo.toml
│   ├── src
│   │   ├── main.rs
├── foo
│   ├── Cargo.toml
│   ├── src
│   │   ├── lib.rs
  1. In the sub-crate foo, its Cargo.toml looks like this:
[package]
name = "foo"
version = "0.1.0"
authors = ["me@gmail.com"]
edition = "2018"
  1. build the project using cargo build. It should succeed.

  2. rename foo to bar: git mv foo bar .

  3. modify root Cargo.toml to replace foo with bar.

  4. modify sub-crate Cargo.toml under bar/, to replace foo with bar.

  5. Now run cargo build again, or cargo clean. Will see the errors.

Possible Solution(s)
Not a real fix: remove the project and use git to re-pull the project.

Notes
Tried to remove target/ and remove Cargo.lock, did not fix the issue.

Output of cargo version:

cargo 1.51.0 (43b129a 2021-03-16)

@keepsimple1 keepsimple1 added the C-bug Category: bug label Apr 1, 2021
@ehuss
Copy link
Contributor

ehuss commented Apr 1, 2021

Hm, sorry you're running into that problem. I'm unable to reproduce with the steps given, though. I tried:

mkdir ws9319
cd ws9319
cargo new app
cargo new --lib foo
cat <<EOF > Cargo.toml
[workspace]
members = [
    "app",
    "foo"
]
EOF
cargo build
mv foo bar
sed -i -e 's/foo/bar/g' Cargo.toml
sed -i -e 's/foo/bar/g' bar/Cargo.toml
cargo build

The last step successfully built bar.

Do you maybe have a path dependency on foo somewhere in the workspace?

The error message definitely could be better here.

@keepsimple1
Copy link
Author

Thank you for your quick response. Could you please try to add the following and see if the error happens? i.e. use foo from app :

$ cat <<EOF >> app/Cargo.toml
> foo = { version = "0.1", path = "../foo" }
> EOF

$ cat <<EOF > foo/src/lib.rs
> pub fn lib_hello() {
>     println!("hello from lib");
> }
> EOF

$ cat <<EOF > app/src/main.rs
> fn main() {
>     println!("Hello, world!");
>     foo::lib_hello();
> }
> EOF

Thanks!

@ehuss
Copy link
Contributor

ehuss commented Apr 1, 2021

Yea, if there is a dependency foo = { version = "0.1", path = "../foo" }, then when the directory is moved, the path in the dependency will also need to be updated to bar = { version = "0.1", path = "../bar" }.

It would be good if the error message pointed to where that dependency is defined.

@keepsimple1
Copy link
Author

you are right, my bad, I missed that. Will close this. Thanks!

@ehuss
Copy link
Contributor

ehuss commented Apr 2, 2021

If you don't mind, I'm going to reopen this to track improving the error message.

@ehuss ehuss reopened this Apr 2, 2021
@ehuss ehuss added the A-diagnostics Area: Error and warning messages generated by Cargo itself. label Apr 2, 2021
@keepsimple1
Copy link
Author

Of course I don't mind. That will be great to improve the error message. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants