Make cargo script ignore workspaces (#15496)

### What does this PR try to resolve?

Cargo script considers itself part of the workspace if the script is
located in a sub-directory of a workspace (presumably since
https://github.com/rust-lang/cargo/pull/15168). This becomes an issue
when using a custom registry that is defined in the `.cargo/config.toml`
within the workspace. Cargo script does not take that file into account
and fails with ``registry index was not found in any configuration:
`test-reg` ``.

### How should we test and review this PR?

This PR adds a regression test and makes cargo script ignore the
surrounding workspace.
The test ~~will fail without the fix in the second commit and~~ can be
used to reproduce the issue.

### Additional information

The issue started occurring with `nightly-2025-02-16`.
Related to https://github.com/rust-lang/cargo/issues/12207.

<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

### How should we test and review this PR?

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
This commit is contained in:
Ed Page 2025-05-07 14:23:29 +00:00 committed by GitHub
commit cf7b053c0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 1 deletions

View File

@ -13,6 +13,7 @@ use cargo_platform::Platform;
use cargo_util::paths;
use cargo_util_schemas::manifest::{
self, PackageName, PathBaseName, TomlDependency, TomlDetailedDependency, TomlManifest,
TomlWorkspace,
};
use cargo_util_schemas::manifest::{RustVersion, StringOrBool};
use itertools::Itertools;
@ -80,7 +81,8 @@ pub fn read_manifest(
let empty = Vec::new();
let cargo_features = original_toml.cargo_features.as_ref().unwrap_or(&empty);
let features = Features::new(cargo_features, gctx, &mut warnings, source_id.is_path())?;
let workspace_config = to_workspace_config(&original_toml, path, gctx, &mut warnings)?;
let workspace_config =
to_workspace_config(&original_toml, path, is_embedded, gctx, &mut warnings)?;
if let WorkspaceConfig::Root(ws_root_config) = &workspace_config {
let package_root = path.parent().unwrap();
gctx.ws_roots
@ -211,9 +213,14 @@ fn stringify(dst: &mut String, path: &serde_ignored::Path<'_>) {
fn to_workspace_config(
original_toml: &manifest::TomlManifest,
manifest_file: &Path,
is_embedded: bool,
gctx: &GlobalContext,
warnings: &mut Vec<String>,
) -> CargoResult<WorkspaceConfig> {
if is_embedded {
let ws_root_config = to_workspace_root_config(&TomlWorkspace::default(), manifest_file);
return Ok(WorkspaceConfig::Root(ws_root_config));
}
let workspace_config = match (
original_toml.workspace.as_ref(),
original_toml.package().and_then(|p| p.workspace.as_ref()),

View File

@ -1886,3 +1886,54 @@ CARGO_MANIFEST_PATH: [ROOT]/foo/script.rs
"#]])
.run();
}
#[cargo_test]
fn ignore_surrounding_workspace() {
let p = cargo_test_support::project()
.file(
std::path::Path::new(".cargo").join("config.toml"),
r#"
[registries.test-reg]
index = "https://github.com/rust-lang/crates.io-index"
"#,
)
.file(
std::path::Path::new("inner").join("Cargo.toml"),
r#"
[package]
name = "inner"
version = "0.1.0"
[dependencies]
serde = { version = "1.0", registry = "test-reg" }
"#,
)
.file(std::path::Path::new("inner").join("src").join("lib.rs"), "")
.file(std::path::Path::new("script").join("echo.rs"), ECHO_SCRIPT)
.file(
"Cargo.toml",
r#"
[workspace]
members = [
"inner",
]
"#,
)
.build();
p.cargo("-Zscript -v script/echo.rs")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
bin: [ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]
args: []
"#]])
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[COMPILING] echo v0.0.0 ([ROOT]/foo/script/echo.rs)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]`
"#]])
.run();
}