Skip to content

Commit bd5f32b

Browse files
committed
Auto merge of #14505 - epage:new, r=weihanglo
fix(new): Add to workspace relative to manifest, not current-dir ### What does this PR try to resolve? 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 ### How should we test and review this PR? ### Additional information
2 parents 0b390cd + f8467c6 commit bd5f32b

File tree

10 files changed

+96
-2
lines changed

10 files changed

+96
-2
lines changed

src/cargo/ops/cargo_new.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ fn mk(gctx: &GlobalContext, opts: &MkOptions<'_>) -> CargoResult<()> {
802802
}
803803
}
804804

805-
let manifest_path = path.join("Cargo.toml");
805+
let manifest_path = paths::normalize_path(&path.join("Cargo.toml"));
806806
if let Ok(root_manifest_path) = find_root_manifest_for_wd(&manifest_path) {
807807
let root_manifest = paths::read(&root_manifest_path)?;
808808
// Sometimes the root manifest is not a valid manifest, so we only try to parse it if it is.
@@ -906,7 +906,7 @@ mod tests {
906906
}
907907
}
908908

909-
if let Err(e) = Workspace::new(&path.join("Cargo.toml"), gctx) {
909+
if let Err(e) = Workspace::new(&manifest_path, gctx) {
910910
crate::display_warning_with_error(
911911
"compiling this new package may not work due to invalid \
912912
workspace configuration",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[workspace]
2+
3+
[package]
4+
name = "foo"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::current_dir;
3+
use cargo_test_support::file;
4+
use cargo_test_support::prelude::*;
5+
use cargo_test_support::str;
6+
use cargo_test_support::CargoCommandExt;
7+
use cargo_test_support::Project;
8+
9+
#[cargo_test]
10+
fn case() {
11+
let project = Project::from_template(current_dir!().join("in"));
12+
let project_root = project.root();
13+
let cwd = &project_root.join("workspace");
14+
15+
snapbox::cmd::Command::cargo_ui()
16+
.arg("new")
17+
.args(["../out-of-workspace", "--lib"])
18+
.current_dir(cwd)
19+
.assert()
20+
.success()
21+
.stdout_eq(str![""])
22+
.stderr_eq(file!["stderr.term.svg"]);
23+
24+
assert_ui().subset_matches(current_dir!().join("out"), &project_root);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "out-of-workspace"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: u64, right: u64) -> u64 {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[workspace]
2+
3+
[package]
4+
name = "foo"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Loading

tests/testsuite/cargo_new/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod add_members_to_workspace_with_members_glob;
88
mod add_members_to_workspace_without_members;
99
mod empty_name;
1010
mod help;
11+
mod ignore_current_dir_workspace;
1112
mod inherit_workspace_lints;
1213
mod inherit_workspace_package_table;
1314
mod inherit_workspace_package_table_with_edition;

0 commit comments

Comments
 (0)