From c015a0061a579f9d906941c5107b47f7a8772a70 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 6 Sep 2024 09:30:53 -0500 Subject: [PATCH 1/3] test(new): Verify ../path behavior with workspaces --- .../in/workspace/Cargo.toml | 8 ++++ .../in/workspace/src/main.rs | 1 + .../ignore_current_dir_workspace/mod.rs | 25 ++++++++++++ .../out/out-of-workspace/Cargo.toml | 6 +++ .../out/out-of-workspace/src/lib.rs | 14 +++++++ .../out/workspace/Cargo.toml | 9 +++++ .../out/workspace/src/main.rs | 1 + .../stderr.term.svg | 39 +++++++++++++++++++ tests/testsuite/cargo_new/mod.rs | 1 + 9 files changed, 104 insertions(+) create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/Cargo.toml create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/src/main.rs create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/mod.rs create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/Cargo.toml create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/src/lib.rs create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/src/main.rs create mode 100644 tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/Cargo.toml b/tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/Cargo.toml new file mode 100644 index 00000000000..cd7525f4050 --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] + +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/src/main.rs b/tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/src/main.rs new file mode 100644 index 00000000000..f328e4d9d04 --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/in/workspace/src/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/mod.rs b/tests/testsuite/cargo_new/ignore_current_dir_workspace/mod.rs new file mode 100644 index 00000000000..d1316c16bfa --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/mod.rs @@ -0,0 +1,25 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::current_dir; +use cargo_test_support::file; +use cargo_test_support::prelude::*; +use cargo_test_support::str; +use cargo_test_support::CargoCommandExt; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(current_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root.join("workspace"); + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args(["../out-of-workspace", "--lib"]) + .current_dir(cwd) + .assert() + .success() + .stdout_eq(str![""]) + .stderr_eq(file!["stderr.term.svg"]); + + assert_ui().subset_matches(current_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/Cargo.toml b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/Cargo.toml new file mode 100644 index 00000000000..e0fde467294 --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "out-of-workspace" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/src/lib.rs b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/src/lib.rs new file mode 100644 index 00000000000..b93cf3ffd9c --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/out-of-workspace/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml new file mode 100644 index 00000000000..ce2c1898ce1 --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml @@ -0,0 +1,9 @@ +[workspace] +members = ["../out-of-workspace"] + +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/src/main.rs b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/src/main.rs new file mode 100644 index 00000000000..f328e4d9d04 --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/src/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg b/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg new file mode 100644 index 00000000000..a7b5660b5f0 --- /dev/null +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg @@ -0,0 +1,39 @@ + + + + + + + Creating library `out-of-workspace` package + + Adding `out-of-workspace` as member of workspace at `[ROOT]/case/workspace` + + warning: compiling this new package may not work due to invalid workspace configuration + + + + workspace member `[ROOT]/case/out-of-workspace/Cargo.toml` is not hierarchically below the workspace root `[ROOT]/case/workspace/Cargo.toml` + + note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + + + + + + diff --git a/tests/testsuite/cargo_new/mod.rs b/tests/testsuite/cargo_new/mod.rs index ca8ee0f9cd5..b278e95bded 100644 --- a/tests/testsuite/cargo_new/mod.rs +++ b/tests/testsuite/cargo_new/mod.rs @@ -8,6 +8,7 @@ mod add_members_to_workspace_with_members_glob; mod add_members_to_workspace_without_members; mod empty_name; mod help; +mod ignore_current_dir_workspace; mod inherit_workspace_lints; mod inherit_workspace_package_table; mod inherit_workspace_package_table_with_edition; From 6a3255c386b6bc3749a0d099d477bc9d47ef3295 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 6 Sep 2024 09:44:00 -0500 Subject: [PATCH 2/3] refactor(new): Ensure we always talk about the xame manifest --- src/cargo/ops/cargo_new.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 4598db07ed6..df4d814fb11 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -906,7 +906,7 @@ mod tests { } } - if let Err(e) = Workspace::new(&path.join("Cargo.toml"), gctx) { + if let Err(e) = Workspace::new(&manifest_path, gctx) { crate::display_warning_with_error( "compiling this new package may not work due to invalid \ workspace configuration", From f8467c641c99efab994989e0ffd5f1d22b440415 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 6 Sep 2024 09:45:19 -0500 Subject: [PATCH 3/3] fix(new): Add to workspace relative to manifest, not current-dir We were correctly doing this for cases like `cargo new foo` or `cargo new deeper/than/this/directory/foo` but not `cargo new ../foo`. This came up when discussing #14501 --- src/cargo/ops/cargo_new.rs | 2 +- .../out/workspace/Cargo.toml | 1 - .../ignore_current_dir_workspace/stderr.term.svg | 15 +++------------ 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index df4d814fb11..a3d08bb4db0 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -802,7 +802,7 @@ fn mk(gctx: &GlobalContext, opts: &MkOptions<'_>) -> CargoResult<()> { } } - let manifest_path = path.join("Cargo.toml"); + let manifest_path = paths::normalize_path(&path.join("Cargo.toml")); if let Ok(root_manifest_path) = find_root_manifest_for_wd(&manifest_path) { let root_manifest = paths::read(&root_manifest_path)?; // Sometimes the root manifest is not a valid manifest, so we only try to parse it if it is. diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml index ce2c1898ce1..cd7525f4050 100644 --- a/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/out/workspace/Cargo.toml @@ -1,5 +1,4 @@ [workspace] -members = ["../out-of-workspace"] [package] name = "foo" diff --git a/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg b/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg index a7b5660b5f0..7858e42a578 100644 --- a/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg +++ b/tests/testsuite/cargo_new/ignore_current_dir_workspace/stderr.term.svg @@ -1,10 +1,9 @@ - +