-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 new
sometimes generates invalid Cargo.toml when new crate is in a subdirectory of a workspace with Cargo 1.71
#12360
Comments
to hack around rust-lang/cargo#12360 and fix Windows build with Rust 1.71
Skimming through the 1.71 commits, nothing really stands out that would touch these areas. "workspace" appears a lot because this was when we switched to a workspace. We also touched workspace-related code but in the periphery, like |
The error for the reproduction steps in the issue is also strange. It is trying to load |
Perhaps the error is misleading? Maybe a problem occurred in a tangentially related part of the Cargo codebase that doesn't get caught immediately? |
We are using I find this behavior a bit strange, since cargo also outputs the warning |
Well this is interesting. On GitHub Actions, [package]
name = "required_libs"
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] then Corrosion adds an empty Manually editing the locally generated Cargo.toml to
|
[workspace]
or workspace.exclude
fails on Windows with Cargo 1.71cargo new
sometimes generates Cargo.toml with version.workspace = true
and sometimes with version = "0.1.0"
with Cargo 1.71
Pretty strange. I couldn't quite follow the reproducible steps. Is there a minimal reproduction without cmake involvement? Or could you provide a repository that people can clone and build and see the error? BTW, the directory you ran a command is also a important part of reproduction. It would be great if we can have this piece of information. I also wonder if there is any member crate depending on the generated crate? That could turn generated crate into a workspace member automatically. |
In 1.71, we added this feature. The inconsistent behavior could be a difference in CI between platforms or we could have a bug in detecting if there is a workspace present. As was said, independent reproduction steps will be important. While we should root cause and fix this issue, I wonder if "cargo new" is intended for automation, if we intend the level of compatibility needed for automation or only or user interaction. |
Yeah, I think it would be appropriate for Corrosion to supply the whole Cargo.toml file instead of running |
execute_process(
COMMAND "${Rust_CARGO_CACHED}" new --lib required_libs
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/corrosion"
RESULT_VARIABLE cargo_new_result
ERROR_QUIET
) so the workspace's virtual Cargo.toml would be two directories above that. |
It looks like Cargo does detect and warn about this situation, but
Perhaps Cargo should revert to generating a crate that does not try to be in the workspace ( |
cargo new
sometimes generates Cargo.toml with version.workspace = true
and sometimes with version = "0.1.0"
with Cargo 1.71cargo new
sometimes generates invalid Cargo.toml when new crate is in a subdirectory of a workspace with Cargo 1.71
We are considering adding the new package to the workspace members automatically and we didn't want to block one feature on the other. Could you provide minimal reproduction steps? What I mean by that is independent of corrosion / cmake steps any of us could run. |
I made a minimal example here: https://github.com/jschwe/cargo_regression_repro What corrosion wants is:
Previously,
I guess if this is going to happen, we can't use |
That could work. Alternatively, it would be nice if rustc or cargo could print the libraries required to link std without needing a dummy crate. |
Thanks for the repro! I guess I've figured out what's going on. #12069 use For now, I consider this as a regression. We at least need to stop the invalid member from inheriting things. There are two solutions come up to my mind:
Not sure which one is simpler. Probably the first one? @hi-rustin are you willing to have a look at it? |
@weihanglo that was somewhat intentional as, unless the person is using globs, they can't add it to the workspace members yet. I would assume the bigger question is why is this platform-specific. |
My bad. I was talking about the repro from @jschwe, and thought it has the same root cause as the original issue. The intentional behavior seems to be a bit unpleasant. Or did you mean when a user notices the error message “ |
@jschwe my understanding was that the original bug only happened on Windows and not on other platforms but I'm not seeing that mentioned in the reproduction case. Is that still true or is the platform-specific aspect somehow a red herring? |
Whether it is unpleasant depends. I expect in most cases, the very next action is to add the package to the workspace. That said, we can re-visit that approach and what we should do for automated adding to workspaces later (I was expecting us to add it to workspace members implicitly). |
I believe the issue may have happened on all platforms, but was likely hidden on linux / mac since the libraries which should have been printed by rustc were either not needed due to simple test code, or also added on the link line via other libraries from the C side for more complex cases.
I agree, that is also my most common usecase (at least when manually invoking |
To summarize @Be-ing and others are using With #12069, we now check if it looks like the new package could be part of a workspace and automatically inherit, punting on whether it is a workspace member or not to #6378. This broke @Be-ing and others because they could no longer just run Note: currently
Questions
I've nominated this for discussion in the cargo team meeting to hash some of these details out |
We discussed this in the cargo team meeting today. While we didn't formally nail it down, our primary concern for programmatic usage is in having processes (like a button in an IDE) that run For programmatic usage that expects content of the files to be generated in a specific way we felt was out of scope. For a case like in this issue, the files could be generated directly or with tools like cargo-generate or cargo-hatch. That still leaves the question of what we consider the right UX. Our primary concern was not regressing in what works. Since Since the use case (programmatic use) is out of scope and the proposed fix (don't inherit) was decided against, I'm closing this and discussion on workspace interactions will be carried forward in #6378. |
Problem
A crate in a subdirectory of a workspace opting out of being in the workspace via an empty
[workspace]
table or specifying its path inworkspace.exclude
in the workspace root's Cargo.toml fails on Windows with Cargo 1.71 whereas it worked with previous versions of Cargo.Steps
Create a crate in a subdirectory of a workspace with the following Cargo.toml:
Running cargo commands fail:
Removing the empty
[workspace]
table from Cargo.toml fails with a different error. This error is also shown when specifyingworkspace.exclude = [ "path/to/subdir" ]
in the workspace root's Cargo.toml:Possible Solution(s)
I encounter this when using Corrosion to invoke Cargo via CMake as part of building a C++ application. At CMake configure time, Corrosion autogenerates a dummy staticlib crate with the above Cargo.toml to run
cargo rustc -- --print=native-static-libs
, which is used to tell CMake what system libraries need to be linked whenever linking a staticlib into a C/C++ build. The generated dummy crate is put in the CMake build directory, which conventionally is inside the code repository, which also contains the workspace's virtual Cargo.toml manifest.A workaround for this particular scenario is to create the CMake build directory outside of the code repository.
If rustc had some way to get the list of native-static-libs needed for every staticlib crate without needing to generate a dummy crate, this situation could be avoided.
Notes
Downstream Corrosion bug: corrosion-rs/corrosion#418
Somehow this bug is only occurring on Windows. Linux and macOS builds still work fine with Rust 1.71.
The root Cargo.toml for the workspace is:
Version
The text was updated successfully, but these errors were encountered: