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
This commit is contained in:
bors 2024-09-09 22:17:11 +00:00
commit bd5f32bb1c
10 changed files with 96 additions and 2 deletions

View file

@ -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.
@ -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",

View file

@ -0,0 +1,8 @@
[workspace]
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1 @@
fn main() {}

View file

@ -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);
}

View file

@ -0,0 +1,6 @@
[package]
name = "out-of-workspace"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -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);
}
}

View file

@ -0,0 +1,8 @@
[workspace]
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,30 @@
<svg width="970px" height="74px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-cyan { fill: #00AAAA }
.fg-green { fill: #00AA00 }
.container {
padding: 0 10px;
line-height: 18px;
}
.bold { font-weight: bold; }
tspan {
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
white-space: pre;
line-height: 18px;
}
</style>
<rect width="100%" height="100%" y="0" rx="4.5" class="bg" />
<text xml:space="preserve" class="container fg">
<tspan x="10px" y="28px"><tspan class="fg-green bold"> Creating</tspan><tspan> library `out-of-workspace` package</tspan>
</tspan>
<tspan x="10px" y="46px"><tspan class="fg-cyan bold">note</tspan><tspan class="bold">:</tspan><tspan> see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html</tspan>
</tspan>
<tspan x="10px" y="64px">
</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 1,003 B

View file

@ -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;