Skip to content

Make cargo script ignore workspaces #15496

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

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()),
Expand Down
51 changes: 51 additions & 0 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}