From 5465e6a451938dcf4d1e032909cfef7da1b7814c Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Tue, 17 Dec 2024 21:39:18 +0800 Subject: [PATCH 1/8] fix: remove unsupported embedded workspace check from `cargo pkgid` command Signed-off-by: Rustin170506 --- src/bin/cargo/commands/pkgid.rs | 7 ------- tests/testsuite/script.rs | 13 ++++++++++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bin/cargo/commands/pkgid.rs b/src/bin/cargo/commands/pkgid.rs index 5fcf85b8f..aa98dc5c3 100644 --- a/src/bin/cargo/commands/pkgid.rs +++ b/src/bin/cargo/commands/pkgid.rs @@ -18,13 +18,6 @@ pub fn cli() -> Command { pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { let ws = args.workspace(gctx)?; - if ws.root_maybe().is_embedded() { - return Err(anyhow::format_err!( - "{} is unsupported by `cargo pkgid`", - ws.root_manifest().display() - ) - .into()); - } if args.is_present_with_zero_values("package") { print_available_packages(&ws)? } diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 3c31c77f3..8db5fcc12 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1297,18 +1297,25 @@ fn cmd_verify_project_with_embedded() { .run(); } -#[cargo_test] +#[cargo_test(nightly, reason = "edition2024 hasn't hit stable yet")] fn cmd_pkgid_with_embedded() { let p = cargo_test_support::project() .file("script.rs", ECHO_SCRIPT) .build(); + p.cargo("-Zscript script.rs") + .masquerade_as_nightly_cargo(&["script"]) + .run(); + + // FIXME: It should be `path+[ROOTURL]/foo/script.rs#script@0.0.0`. p.cargo("-Zscript pkgid --manifest-path script.rs") .masquerade_as_nightly_cargo(&["script"]) - .with_status(101) + .with_stdout_data(str![[r#" +path+[ROOTURL]/foo#script@0.0.0 + +"#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[ERROR] [ROOT]/foo/script.rs is unsupported by `cargo pkgid` "#]]) .run(); From feda97d9f27f3b70e7de266ffec05fc48cd60b23 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Thu, 19 Dec 2024 22:33:42 +0800 Subject: [PATCH 2/8] test: add the no lock file case Signed-off-by: Rustin170506 --- tests/testsuite/script.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 8db5fcc12..bae2c2837 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1321,6 +1321,23 @@ path+[ROOTURL]/foo#script@0.0.0 .run(); } +#[cargo_test] +fn cmd_pkgid_with_embedded_no_lock_file() { + let p = cargo_test_support::project() + .file("script.rs", ECHO_SCRIPT) + .build(); + + p.cargo("-Zscript pkgid --manifest-path script.rs") + .masquerade_as_nightly_cargo(&["script"]) + .with_status(101) + .with_stderr_data(str![[r#" +[WARNING] `package.edition` is unspecified, defaulting to `2024` +[ERROR] a Cargo.lock must exist for this command + +"#]]) + .run(); +} + #[cargo_test] fn cmd_package_with_embedded() { let p = cargo_test_support::project() From 0c2bb4e5a8afbeba257dbe10123823d1e0f5541c Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Thu, 19 Dec 2024 22:43:48 +0800 Subject: [PATCH 3/8] test: add the dep case Signed-off-by: Rustin170506 --- tests/testsuite/script.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index bae2c2837..520ebbe55 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1338,6 +1338,39 @@ fn cmd_pkgid_with_embedded_no_lock_file() { .run(); } +#[cargo_test(nightly, reason = "edition2024 hasn't hit stable yet")] +fn cmd_pkgid_with_embedded_dep() { + Package::new("dep", "1.0.0").publish(); + let script = r#"#!/usr/bin/env cargo +--- +[dependencies] +dep = "1.0.0" +--- + +fn main() { + println!("Hello world!"); +}"#; + let p = cargo_test_support::project() + .file("script.rs", script) + .build(); + + p.cargo("-Zscript script.rs") + .masquerade_as_nightly_cargo(&["script"]) + .run(); + + p.cargo("-Zscript pkgid --manifest-path script.rs -p dep") + .masquerade_as_nightly_cargo(&["script"]) + .with_stdout_data(str![[r#" +registry+https://github.com/rust-lang/crates.io-index#dep@1.0.0 + +"#]]) + .with_stderr_data(str![[r#" +[WARNING] `package.edition` is unspecified, defaulting to `2024` + +"#]]) + .run(); +} + #[cargo_test] fn cmd_package_with_embedded() { let p = cargo_test_support::project() From d4c1eeeaa070f9d8cc7764938656ad89948c14d3 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Mon, 20 Jan 2025 21:51:47 +0800 Subject: [PATCH 4/8] refactor: use manifest path as the key Signed-off-by: Rustin170506 fix --- src/cargo/core/workspace.rs | 21 ++++++++------------- tests/testsuite/alt_registry.rs | 4 ++-- tests/testsuite/artifact_dep.rs | 10 +++++----- tests/testsuite/bad_config.rs | 2 +- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index a81c6b930..2f8da34b4 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -269,10 +269,11 @@ impl<'gctx> Workspace<'gctx> { let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx); ws.is_ephemeral = true; ws.require_optional_deps = require_optional_deps; - let key = ws.current_manifest.parent().unwrap(); let id = package.package_id(); let package = MaybePackage::Package(package); - ws.packages.packages.insert(key.to_path_buf(), package); + ws.packages + .packages + .insert(ws.current_manifest.clone(), package); ws.target_dir = if let Some(dir) = target_dir { Some(dir) } else { @@ -538,11 +539,7 @@ impl<'gctx> Workspace<'gctx> { /// Returns a mutable iterator over all packages in this workspace pub fn members_mut(&mut self) -> impl Iterator { let packages = &mut self.packages.packages; - let members: HashSet<_> = self - .members - .iter() - .map(|path| path.parent().unwrap().to_owned()) - .collect(); + let members: HashSet<_> = self.members.iter().map(|path| path).collect(); packages.iter_mut().filter_map(move |(path, package)| { if members.contains(path) { @@ -1159,7 +1156,6 @@ impl<'gctx> Workspace<'gctx> { pub fn emit_warnings(&self) -> CargoResult<()> { for (path, maybe_pkg) in &self.packages.packages { - let path = path.join("Cargo.toml"); if let MaybePackage::Package(pkg) = maybe_pkg { if self.gctx.cli_unstable().cargo_lints { self.emit_lints(pkg, &path)? @@ -1788,19 +1784,18 @@ impl<'gctx> Packages<'gctx> { } fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> { - self.packages.get(manifest_path.parent().unwrap()) + self.packages.get(manifest_path) } fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> { - self.packages.get_mut(manifest_path.parent().unwrap()) + self.packages.get_mut(manifest_path) } fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> { - let key = manifest_path.parent().unwrap(); - match self.packages.entry(key.to_path_buf()) { + match self.packages.entry(manifest_path.to_path_buf()) { Entry::Occupied(e) => Ok(e.into_mut()), Entry::Vacant(v) => { - let source_id = SourceId::for_path(key)?; + let source_id = SourceId::for_path(manifest_path.parent().unwrap())?; let manifest = read_manifest(manifest_path, source_id, self.gctx)?; Ok(v.insert(match manifest { EitherManifest::Real(manifest) => { diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index b001da4f8..c61c90205 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -182,8 +182,8 @@ fn depend_on_alt_registry_depends_on_crates_io() { [DOWNLOADED] bar v0.0.1 (registry `alternative`) [CHECKING] baz v0.0.1 [CHECKING] bar v0.0.1 (registry `alternative`) -[CHECKING] foo v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[CHECKING] foo v0.0.1 ([ROOT]/foo) "#]] .unordered(), @@ -441,8 +441,8 @@ fn alt_registry_and_crates_io_deps() { [DOWNLOADED] alt_reg_dep v0.1.0 (registry `alternative`) [CHECKING] crates_io_dep v0.0.1 [CHECKING] alt_reg_dep v0.1.0 (registry `alternative`) -[CHECKING] foo v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[CHECKING] foo v0.0.1 ([ROOT]/foo) "#]] .unordered(), diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index 1912d61e8..fc4b8f63e 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -602,9 +602,9 @@ fn build_script_with_bin_artifacts() { .with_stderr_data( str![[r#" [LOCKING] 1 package to latest compatible version -[COMPILING] foo v0.0.0 ([ROOT]/foo) [COMPILING] bar v0.5.0 ([ROOT]/foo/bar) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[COMPILING] foo v0.0.0 ([ROOT]/foo) "#]] .unordered(), @@ -1433,7 +1433,6 @@ fn profile_override_basic() { str![[r#" [LOCKING] 1 package to latest compatible version [COMPILING] bar v0.5.0 ([ROOT]/foo/bar) -[COMPILING] foo v0.0.1 ([ROOT]/foo) [RUNNING] `rustc --crate-name build_script_build [..] -C opt-level=1 [..]` [RUNNING] `rustc --crate-name bar --edition=2015 bar/src/main.rs [..] -C opt-level=3 [..]` [RUNNING] `rustc --crate-name bar --edition=2015 bar/src/main.rs [..] -C opt-level=1 [..]` @@ -1442,6 +1441,7 @@ fn profile_override_basic() { [RUNNING] `rustc --crate-name foo [..] -C opt-level=3 [..]` [RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` [FINISHED] `dev` profile [optimized + debuginfo] target(s) in [ELAPSED]s +[COMPILING] foo v0.0.1 ([ROOT]/foo) "#]] .unordered(), @@ -1870,8 +1870,8 @@ fn allow_dep_renames_with_multiple_versions() { [DOWNLOADED] bar v1.0.0 (registry `dummy-registry`) [COMPILING] bar v1.0.0 [COMPILING] bar v0.5.0 ([ROOT]/foo/bar) -[COMPILING] foo v0.0.0 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[COMPILING] foo v0.0.0 ([ROOT]/foo) "#]] .unordered(), @@ -2835,13 +2835,13 @@ fn with_assumed_host_target_and_optional_build_dep() { .with_stderr_data( str![[r#" [LOCKING] 1 package to latest compatible version -[COMPILING] foo v0.0.1 ([ROOT]/foo) [COMPILING] d1 v0.0.1 ([ROOT]/foo/d1) [RUNNING] `rustc --crate-name build_script_build --edition=2021 [..]--crate-type bin[..] [RUNNING] `rustc --crate-name d1 --edition=2021 [..]--crate-type bin[..] [RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` [RUNNING] `rustc --crate-name foo --edition=2021 [..]--cfg[..]d1[..] [FINISHED] `dev` profile [..] +[COMPILING] foo v0.0.1 ([ROOT]/foo) "#]] .unordered(), @@ -3199,8 +3199,8 @@ fn decouple_same_target_transitive_dep_from_artifact_dep_and_proc_macro() { [COMPILING] b v0.1.0 ([ROOT]/foo/b) [COMPILING] c v0.1.0 ([ROOT]/foo/c) [COMPILING] bar v0.1.0 ([ROOT]/foo/bar) -[COMPILING] foo v0.1.0 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[COMPILING] foo v0.1.0 ([ROOT]/foo) "#]] .unordered(), diff --git a/tests/testsuite/bad_config.rs b/tests/testsuite/bad_config.rs index bf1b0da2a..13b3a492d 100644 --- a/tests/testsuite/bad_config.rs +++ b/tests/testsuite/bad_config.rs @@ -1814,13 +1814,13 @@ fn workspace_default_features2() { p.cargo("check") .with_stderr_data( str![[r#" -[WARNING] [ROOT]/foo/workspace_only/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition (in the `dep_workspace_only` dependency) [CHECKING] dep_package_only v0.1.0 ([ROOT]/foo/dep_package_only) [CHECKING] dep_workspace_only v0.1.0 ([ROOT]/foo/dep_workspace_only) [CHECKING] package_only v0.1.0 ([ROOT]/foo/package_only) [CHECKING] workspace_only v0.1.0 ([ROOT]/foo/workspace_only) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[WARNING] [ROOT]/foo/workspace_only/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition "#]] .unordered(), From 95f6d0b2da4be8d511cf9e1902f5458ee4180ca0 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Thu, 9 Jan 2025 23:20:22 +0800 Subject: [PATCH 5/8] feat: use full URL for embedded script paths Signed-off-by: Rustin170506 --- src/cargo/core/workspace.rs | 6 ++- tests/testsuite/fix.rs | 2 +- tests/testsuite/open_namespaces.rs | 2 +- tests/testsuite/script.rs | 69 +++++++++++++++--------------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 2f8da34b4..f5e956043 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1795,7 +1795,11 @@ impl<'gctx> Packages<'gctx> { match self.packages.entry(manifest_path.to_path_buf()) { Entry::Occupied(e) => Ok(e.into_mut()), Entry::Vacant(v) => { - let source_id = SourceId::for_path(manifest_path.parent().unwrap())?; + let source_id = if crate::util::toml::is_embedded(manifest_path) { + SourceId::for_path(manifest_path)? + } else { + SourceId::for_path(manifest_path.parent().unwrap())? + }; let manifest = read_manifest(manifest_path, source_id, self.gctx)?; Ok(v.insert(match manifest { EitherManifest::Real(manifest) => { diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index f475fe5ba..f5635528a 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -2479,7 +2479,7 @@ fn main() { .with_stderr_data(str![[r#" [MIGRATING] foo.rs from 2021 edition to 2024 [FIXED] foo.rs (1 fix) -[CHECKING] foo v0.0.0 ([ROOT]/foo) +[CHECKING] foo v0.0.0 ([ROOT]/foo/foo.rs) [MIGRATING] [ROOT]/home/.cargo/target/[HASH]/foo.rs from 2021 edition to 2024 [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s diff --git a/tests/testsuite/open_namespaces.rs b/tests/testsuite/open_namespaces.rs index 6fc018f11..c7506dfae 100644 --- a/tests/testsuite/open_namespaces.rs +++ b/tests/testsuite/open_namespaces.rs @@ -289,7 +289,7 @@ fn main() {} "edition": "2021", "features": {}, "homepage": null, - "id": "path+[ROOTURL]/foo#foo::bar@0.0.0", + "id": "path+[ROOTURL]/foo/foo::bar.rs#foo::bar@0.0.0", "keywords": [], "license": null, "license_file": null, diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 520ebbe55..b6de58e27 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -40,7 +40,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] echo v0.0.0 ([ROOT]/foo) +[COMPILING] echo v0.0.0 ([ROOT]/foo/echo.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]` @@ -63,7 +63,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] echo v0.0.0 ([ROOT]/foo) +[COMPILING] echo v0.0.0 ([ROOT]/foo/echo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]` @@ -117,7 +117,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] echo v0.0.0 ([ROOT]/foo) +[COMPILING] echo v0.0.0 ([ROOT]/foo/echo.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/echo[EXE]` @@ -204,7 +204,7 @@ Hello world! "#]]) .with_stderr_data(str![[r#" -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -234,7 +234,7 @@ Hello world! "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -262,7 +262,7 @@ msg = undefined "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -294,7 +294,7 @@ msg = hello "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -422,7 +422,7 @@ line: 4 "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -446,7 +446,7 @@ args: ["-NotAnArg"] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] -NotAnArg` @@ -470,7 +470,7 @@ args: ["-NotAnArg"] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] -NotAnArg` @@ -494,7 +494,7 @@ args: ["--help"] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] --help` @@ -518,7 +518,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] s-h-w-c- v0.0.0 ([ROOT]/foo) +[COMPILING] s-h-w-c- v0.0.0 ([ROOT]/foo/s-h.w§c!.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/s-h-w-c-[EXE]` @@ -542,7 +542,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] answer v0.0.0 ([ROOT]/foo) +[COMPILING] answer v0.0.0 ([ROOT]/foo/42answer.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/answer[EXE]` @@ -564,7 +564,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] package v0.0.0 ([ROOT]/foo) +[COMPILING] package v0.0.0 ([ROOT]/foo/42.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/package[EXE]` @@ -715,7 +715,7 @@ Hello world! [DOWNLOADING] crates ... [DOWNLOADED] script v1.0.0 (registry `dummy-registry`) [COMPILING] script v1.0.0 -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] --help` @@ -751,7 +751,7 @@ Hello world! [WARNING] `package.edition` is unspecified, defaulting to `2024` [LOCKING] 1 package to latest Rust [..] compatible version [COMPILING] bar v0.0.1 ([ROOT]/foo/bar) -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] --help` @@ -779,7 +779,7 @@ Hello world! "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] --help` @@ -807,7 +807,7 @@ Hello world! "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] --help` @@ -835,7 +835,7 @@ Hello world! "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] --help` @@ -859,7 +859,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -886,7 +886,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -943,7 +943,7 @@ fn cmd_check_with_embedded() { .with_stdout_data("") .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[CHECKING] script v0.0.0 ([ROOT]/foo) +[CHECKING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) @@ -992,7 +992,7 @@ fn cmd_build_with_embedded() { .with_stdout_data("") .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) @@ -1019,7 +1019,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fini "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] unittests script.rs ([ROOT]/home/.cargo/target/[HASH]/debug/deps/script-[HASH][EXE]) @@ -1091,7 +1091,7 @@ fn cmd_metadata_with_embedded() { "edition": "2024", "features": {}, "homepage": null, - "id": "path+[ROOTURL]/foo#script@0.0.0", + "id": "path+[ROOTURL]/foo/script.rs#script@0.0.0", "keywords": [], "license": null, "license_file": null, @@ -1129,18 +1129,18 @@ fn cmd_metadata_with_embedded() { "dependencies": [], "deps": [], "features": [], - "id": "path+[ROOTURL]/foo#script@0.0.0" + "id": "path+[ROOTURL]/foo/script.rs#script@0.0.0" } ], - "root": "path+[ROOTURL]/foo#script@0.0.0" + "root": "path+[ROOTURL]/foo/script.rs#script@0.0.0" }, "target_directory": "[ROOT]/home/.cargo/target/[HASH]", "version": 1, "workspace_default_members": [ - "path+[ROOTURL]/foo#script@0.0.0" + "path+[ROOTURL]/foo/script.rs#script@0.0.0" ], "workspace_members": [ - "path+[ROOTURL]/foo#script@0.0.0" + "path+[ROOTURL]/foo/script.rs#script@0.0.0" ], "workspace_root": "[ROOT]/foo" } @@ -1175,7 +1175,7 @@ fn cmd_read_manifest_with_embedded() { "edition": "2024", "features": {}, "homepage": null, - "id": "path+[ROOTURL]/foo#script@0.0.0", + "id": "path+[ROOTURL]/foo/script.rs#script@0.0.0", "keywords": [], "license": null, "license_file": null, @@ -1231,7 +1231,7 @@ args: [] "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` @@ -1248,7 +1248,7 @@ fn cmd_tree_with_embedded() { p.cargo("-Zscript tree --manifest-path script.rs") .masquerade_as_nightly_cargo(&["script"]) .with_stdout_data(str![[r#" -script v0.0.0 ([ROOT]/foo) +script v0.0.0 ([ROOT]/foo/script.rs) "#]]) .with_stderr_data(str![[r#" @@ -1307,11 +1307,10 @@ fn cmd_pkgid_with_embedded() { .masquerade_as_nightly_cargo(&["script"]) .run(); - // FIXME: It should be `path+[ROOTURL]/foo/script.rs#script@0.0.0`. p.cargo("-Zscript pkgid --manifest-path script.rs") .masquerade_as_nightly_cargo(&["script"]) .with_stdout_data(str![[r#" -path+[ROOTURL]/foo#script@0.0.0 +path+[ROOTURL]/foo/script.rs#script@0.0.0 "#]]) .with_stderr_data(str![[r#" @@ -1427,7 +1426,7 @@ CARGO_MANIFEST_PATH: [ROOT]/foo/script.rs "#]]) .with_stderr_data(str![[r#" [WARNING] `package.edition` is unspecified, defaulting to `2024` -[COMPILING] script v0.0.0 ([ROOT]/foo) +[COMPILING] script v0.0.0 ([ROOT]/foo/script.rs) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]` From 1514c7e63097b77c8e40710257156ea25779aa05 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Fri, 10 Jan 2025 01:04:20 +0800 Subject: [PATCH 6/8] test: add test for script as a dependency Signed-off-by: Rustin170506 --- tests/testsuite/script.rs | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index b6de58e27..53569caa9 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1370,6 +1370,47 @@ registry+https://github.com/rust-lang/crates.io-index#dep@1.0.0 .run(); } +#[cargo_test(nightly, reason = "edition2024 hasn't hit stable yet")] +fn script_as_dep() { + let p = cargo_test_support::project() + .file("script.rs", ECHO_SCRIPT) + .file("src/lib.rs", "pub fn foo() {}") + .file( + "Cargo.toml", + r#" +[package] +name = "foo" +version = "0.1.0" + +[dependencies] +script.path = "script.rs" +"#, + ) + .build(); + + p.cargo("build") + .masquerade_as_nightly_cargo(&["script"]) + .with_status(101) + .with_stderr_data(str![[r#" +[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2024 +[ERROR] failed to get `script` as a dependency of package `foo v0.1.0 ([ROOT]/foo)` + +Caused by: + failed to load source for dependency `script` + +Caused by: + Unable to update [ROOT]/foo/script.rs + +Caused by: + failed to read `[ROOT]/foo/script.rs/Cargo.toml` + +Caused by: + Not a directory (os error 20) + +"#]]) + .run(); +} + #[cargo_test] fn cmd_package_with_embedded() { let p = cargo_test_support::project() From 91da8dba0dd4f1d3bbb8096a519ff0983dc0b2d2 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Mon, 13 Jan 2025 22:53:38 +0800 Subject: [PATCH 7/8] fix: better error message for using the script as a dep Signed-off-by: Rustin170506 --- src/cargo/core/source_id.rs | 3 +++ tests/testsuite/script.rs | 5 +---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index 501c88cba..3dbcc6f37 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -393,6 +393,9 @@ impl SourceId { .url .to_file_path() .expect("path sources cannot be remote"); + if crate::util::toml::is_embedded(&path) { + anyhow::bail!("Single file packages cannot be used as dependencies") + } Ok(Box::new(PathSource::new(&path, self, gctx))) } SourceKind::Registry | SourceKind::SparseRegistry => Ok(Box::new( diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 53569caa9..6ff7114b6 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1402,10 +1402,7 @@ Caused by: Unable to update [ROOT]/foo/script.rs Caused by: - failed to read `[ROOT]/foo/script.rs/Cargo.toml` - -Caused by: - Not a directory (os error 20) + Single file packages cannot be used as dependencies "#]]) .run(); From 9fe45b20100536ba2efb41c57e9866acda4d7682 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Mon, 3 Feb 2025 00:11:55 +0800 Subject: [PATCH 8/8] test: add a test for error handling in script installation Signed-off-by: Rustin170506 --- tests/testsuite/script.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 6ff7114b6..dcc9a0701 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1408,6 +1408,22 @@ Caused by: .run(); } +#[cargo_test] +fn cmd_install_with_embedded() { + let p = cargo_test_support::project() + .file("script.rs", ECHO_SCRIPT) + .build(); + + p.cargo("-Zscript install --path script.rs") + .masquerade_as_nightly_cargo(&["script"]) + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] `[ROOT]/foo/script.rs` is not a directory. --path must point to a directory containing a Cargo.toml file. + +"#]]) + .run(); +} + #[cargo_test] fn cmd_package_with_embedded() { let p = cargo_test_support::project()