Skip to content

Commit cf7b053

Browse files
authored
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 #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 #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. -->
2 parents 74c051f + 2b247ff commit cf7b053

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/cargo/util/toml/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use cargo_platform::Platform;
1313
use cargo_util::paths;
1414
use cargo_util_schemas::manifest::{
1515
self, PackageName, PathBaseName, TomlDependency, TomlDetailedDependency, TomlManifest,
16+
TomlWorkspace,
1617
};
1718
use cargo_util_schemas::manifest::{RustVersion, StringOrBool};
1819
use itertools::Itertools;
@@ -80,7 +81,8 @@ pub fn read_manifest(
8081
let empty = Vec::new();
8182
let cargo_features = original_toml.cargo_features.as_ref().unwrap_or(&empty);
8283
let features = Features::new(cargo_features, gctx, &mut warnings, source_id.is_path())?;
83-
let workspace_config = to_workspace_config(&original_toml, path, gctx, &mut warnings)?;
84+
let workspace_config =
85+
to_workspace_config(&original_toml, path, is_embedded, gctx, &mut warnings)?;
8486
if let WorkspaceConfig::Root(ws_root_config) = &workspace_config {
8587
let package_root = path.parent().unwrap();
8688
gctx.ws_roots
@@ -211,9 +213,14 @@ fn stringify(dst: &mut String, path: &serde_ignored::Path<'_>) {
211213
fn to_workspace_config(
212214
original_toml: &manifest::TomlManifest,
213215
manifest_file: &Path,
216+
is_embedded: bool,
214217
gctx: &GlobalContext,
215218
warnings: &mut Vec<String>,
216219
) -> CargoResult<WorkspaceConfig> {
220+
if is_embedded {
221+
let ws_root_config = to_workspace_root_config(&TomlWorkspace::default(), manifest_file);
222+
return Ok(WorkspaceConfig::Root(ws_root_config));
223+
}
217224
let workspace_config = match (
218225
original_toml.workspace.as_ref(),
219226
original_toml.package().and_then(|p| p.workspace.as_ref()),

tests/testsuite/script.rs

+51
Original file line numberDiff line numberDiff line change
@@ -1886,3 +1886,54 @@ CARGO_MANIFEST_PATH: [ROOT]/foo/script.rs
18861886
"#]])
18871887
.run();
18881888
}
1889+
1890+
#[cargo_test]
1891+
fn ignore_surrounding_workspace() {
1892+
let p = cargo_test_support::project()
1893+
.file(
1894+
std::path::Path::new(".cargo").join("config.toml"),
1895+
r#"
1896+
[registries.test-reg]
1897+
index = "https://github.com/rust-lang/crates.io-index"
1898+
"#,
1899+
)
1900+
.file(
1901+
std::path::Path::new("inner").join("Cargo.toml"),
1902+
r#"
1903+
[package]
1904+
name = "inner"
1905+
version = "0.1.0"
1906+
1907+
[dependencies]
1908+
serde = { version = "1.0", registry = "test-reg" }
1909+
"#,
1910+
)
1911+
.file(std::path::Path::new("inner").join("src").join("lib.rs"), "")
1912+
.file(std::path::Path::new("script").join("echo.rs"), ECHO_SCRIPT)
1913+
.file(
1914+
"Cargo.toml",
1915+
r#"
1916+
[workspace]
1917+
members = [
1918+
"inner",
1919+
]
1920+
"#,
1921+
)
1922+
.build();
1923+
1924+
p.cargo("-Zscript -v script/echo.rs")
1925+
.masquerade_as_nightly_cargo(&["script"])
1926+
.with_stdout_data(str![[r#"
1927+
bin: [ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]
1928+
args: []
1929+
1930+
"#]])
1931+
.with_stderr_data(str![[r#"
1932+
[WARNING] `package.edition` is unspecified, defaulting to `2024`
1933+
[COMPILING] echo v0.0.0 ([ROOT]/foo/script/echo.rs)
1934+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1935+
[RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]`
1936+
1937+
"#]])
1938+
.run();
1939+
}

0 commit comments

Comments
 (0)