Skip to content

Commit f651497

Browse files
authored
feat: add cargo pkgid support for cargo-script (#14961)
### What does this PR try to resolve? close #14831 In this PR, we added the `cargo pkgid` support for the cargo-script. For the package itself: ```console cargo pkgid --manifest-path foo.rs path+file:///my-project/foo.rs/foo#0.86.0 ``` For its dependence: ```console cargo pkgid --manifest-path foo.rs -p sqlx registry+https://github.com/rust-lang/crates.io-index#sqlx@0.7.4 ``` ### How should we test and review this PR? I have updated the unit tests and also added more test cases for it. ### Additional information None
2 parents 83c11ee + 9fe45b2 commit f651497

File tree

9 files changed

+170
-65
lines changed

9 files changed

+170
-65
lines changed

src/bin/cargo/commands/pkgid.rs

-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ pub fn cli() -> Command {
1818

1919
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
2020
let ws = args.workspace(gctx)?;
21-
if ws.root_maybe().is_embedded() {
22-
return Err(anyhow::format_err!(
23-
"{} is unsupported by `cargo pkgid`",
24-
ws.root_manifest().display()
25-
)
26-
.into());
27-
}
2821
if args.is_present_with_zero_values("package") {
2922
print_available_packages(&ws)?
3023
}

src/cargo/core/source_id.rs

+3
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ impl SourceId {
393393
.url
394394
.to_file_path()
395395
.expect("path sources cannot be remote");
396+
if crate::util::toml::is_embedded(&path) {
397+
anyhow::bail!("Single file packages cannot be used as dependencies")
398+
}
396399
Ok(Box::new(PathSource::new(&path, self, gctx)))
397400
}
398401
SourceKind::Registry | SourceKind::SparseRegistry => Ok(Box::new(

src/cargo/core/workspace.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,11 @@ impl<'gctx> Workspace<'gctx> {
269269
let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
270270
ws.is_ephemeral = true;
271271
ws.require_optional_deps = require_optional_deps;
272-
let key = ws.current_manifest.parent().unwrap();
273272
let id = package.package_id();
274273
let package = MaybePackage::Package(package);
275-
ws.packages.packages.insert(key.to_path_buf(), package);
274+
ws.packages
275+
.packages
276+
.insert(ws.current_manifest.clone(), package);
276277
ws.target_dir = if let Some(dir) = target_dir {
277278
Some(dir)
278279
} else {
@@ -538,11 +539,7 @@ impl<'gctx> Workspace<'gctx> {
538539
/// Returns a mutable iterator over all packages in this workspace
539540
pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
540541
let packages = &mut self.packages.packages;
541-
let members: HashSet<_> = self
542-
.members
543-
.iter()
544-
.map(|path| path.parent().unwrap().to_owned())
545-
.collect();
542+
let members: HashSet<_> = self.members.iter().map(|path| path).collect();
546543

547544
packages.iter_mut().filter_map(move |(path, package)| {
548545
if members.contains(path) {
@@ -1163,7 +1160,6 @@ impl<'gctx> Workspace<'gctx> {
11631160

11641161
pub fn emit_warnings(&self) -> CargoResult<()> {
11651162
for (path, maybe_pkg) in &self.packages.packages {
1166-
let path = path.join("Cargo.toml");
11671163
if let MaybePackage::Package(pkg) = maybe_pkg {
11681164
if self.gctx.cli_unstable().cargo_lints {
11691165
self.emit_lints(pkg, &path)?
@@ -1792,19 +1788,22 @@ impl<'gctx> Packages<'gctx> {
17921788
}
17931789

17941790
fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
1795-
self.packages.get(manifest_path.parent().unwrap())
1791+
self.packages.get(manifest_path)
17961792
}
17971793

17981794
fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
1799-
self.packages.get_mut(manifest_path.parent().unwrap())
1795+
self.packages.get_mut(manifest_path)
18001796
}
18011797

18021798
fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
1803-
let key = manifest_path.parent().unwrap();
1804-
match self.packages.entry(key.to_path_buf()) {
1799+
match self.packages.entry(manifest_path.to_path_buf()) {
18051800
Entry::Occupied(e) => Ok(e.into_mut()),
18061801
Entry::Vacant(v) => {
1807-
let source_id = SourceId::for_path(key)?;
1802+
let source_id = if crate::util::toml::is_embedded(manifest_path) {
1803+
SourceId::for_path(manifest_path)?
1804+
} else {
1805+
SourceId::for_path(manifest_path.parent().unwrap())?
1806+
};
18081807
let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
18091808
Ok(v.insert(match manifest {
18101809
EitherManifest::Real(manifest) => {

tests/testsuite/alt_registry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ fn depend_on_alt_registry_depends_on_crates_io() {
182182
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
183183
[CHECKING] baz v0.0.1
184184
[CHECKING] bar v0.0.1 (registry `alternative`)
185-
[CHECKING] foo v0.0.1 ([ROOT]/foo)
186185
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
186+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
187187
188188
"#]]
189189
.unordered(),
@@ -441,8 +441,8 @@ fn alt_registry_and_crates_io_deps() {
441441
[DOWNLOADED] alt_reg_dep v0.1.0 (registry `alternative`)
442442
[CHECKING] crates_io_dep v0.0.1
443443
[CHECKING] alt_reg_dep v0.1.0 (registry `alternative`)
444-
[CHECKING] foo v0.0.1 ([ROOT]/foo)
445444
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
445+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
446446
447447
"#]]
448448
.unordered(),

tests/testsuite/artifact_dep.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ fn build_script_with_bin_artifacts() {
602602
.with_stderr_data(
603603
str![[r#"
604604
[LOCKING] 1 package to latest compatible version
605-
[COMPILING] foo v0.0.0 ([ROOT]/foo)
606605
[COMPILING] bar v0.5.0 ([ROOT]/foo/bar)
607606
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
607+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
608608
609609
"#]]
610610
.unordered(),
@@ -1433,7 +1433,6 @@ fn profile_override_basic() {
14331433
str![[r#"
14341434
[LOCKING] 1 package to latest compatible version
14351435
[COMPILING] bar v0.5.0 ([ROOT]/foo/bar)
1436-
[COMPILING] foo v0.0.1 ([ROOT]/foo)
14371436
[RUNNING] `rustc --crate-name build_script_build [..] -C opt-level=1 [..]`
14381437
[RUNNING] `rustc --crate-name bar --edition=2015 bar/src/main.rs [..] -C opt-level=3 [..]`
14391438
[RUNNING] `rustc --crate-name bar --edition=2015 bar/src/main.rs [..] -C opt-level=1 [..]`
@@ -1442,6 +1441,7 @@ fn profile_override_basic() {
14421441
[RUNNING] `rustc --crate-name foo [..] -C opt-level=3 [..]`
14431442
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
14441443
[FINISHED] `dev` profile [optimized + debuginfo] target(s) in [ELAPSED]s
1444+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
14451445
14461446
"#]]
14471447
.unordered(),
@@ -1870,8 +1870,8 @@ fn allow_dep_renames_with_multiple_versions() {
18701870
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
18711871
[COMPILING] bar v1.0.0
18721872
[COMPILING] bar v0.5.0 ([ROOT]/foo/bar)
1873-
[COMPILING] foo v0.0.0 ([ROOT]/foo)
18741873
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1874+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
18751875
18761876
"#]]
18771877
.unordered(),
@@ -2835,13 +2835,13 @@ fn with_assumed_host_target_and_optional_build_dep() {
28352835
.with_stderr_data(
28362836
str![[r#"
28372837
[LOCKING] 1 package to latest compatible version
2838-
[COMPILING] foo v0.0.1 ([ROOT]/foo)
28392838
[COMPILING] d1 v0.0.1 ([ROOT]/foo/d1)
28402839
[RUNNING] `rustc --crate-name build_script_build --edition=2021 [..]--crate-type bin[..]
28412840
[RUNNING] `rustc --crate-name d1 --edition=2021 [..]--crate-type bin[..]
28422841
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
28432842
[RUNNING] `rustc --crate-name foo --edition=2021 [..]--cfg[..]d1[..]
28442843
[FINISHED] `dev` profile [..]
2844+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
28452845
28462846
"#]]
28472847
.unordered(),
@@ -3199,8 +3199,8 @@ fn decouple_same_target_transitive_dep_from_artifact_dep_and_proc_macro() {
31993199
[COMPILING] b v0.1.0 ([ROOT]/foo/b)
32003200
[COMPILING] c v0.1.0 ([ROOT]/foo/c)
32013201
[COMPILING] bar v0.1.0 ([ROOT]/foo/bar)
3202-
[COMPILING] foo v0.1.0 ([ROOT]/foo)
32033202
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
3203+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
32043204
32053205
"#]]
32063206
.unordered(),

tests/testsuite/bad_config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1814,13 +1814,13 @@ fn workspace_default_features2() {
18141814
p.cargo("check")
18151815
.with_stderr_data(
18161816
str![[r#"
1817-
[WARNING] [ROOT]/foo/workspace_only/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
18181817
(in the `dep_workspace_only` dependency)
18191818
[CHECKING] dep_package_only v0.1.0 ([ROOT]/foo/dep_package_only)
18201819
[CHECKING] dep_workspace_only v0.1.0 ([ROOT]/foo/dep_workspace_only)
18211820
[CHECKING] package_only v0.1.0 ([ROOT]/foo/package_only)
18221821
[CHECKING] workspace_only v0.1.0 ([ROOT]/foo/workspace_only)
18231822
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1823+
[WARNING] [ROOT]/foo/workspace_only/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
18241824
18251825
"#]]
18261826
.unordered(),

tests/testsuite/fix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,7 @@ fn main() {
24792479
.with_stderr_data(str![[r#"
24802480
[MIGRATING] foo.rs from 2021 edition to 2024
24812481
[FIXED] foo.rs (1 fix)
2482-
[CHECKING] foo v0.0.0 ([ROOT]/foo)
2482+
[CHECKING] foo v0.0.0 ([ROOT]/foo/foo.rs)
24832483
[MIGRATING] [ROOT]/home/.cargo/target/[HASH]/foo.rs from 2021 edition to 2024
24842484
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
24852485

tests/testsuite/open_namespaces.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ fn main() {}
289289
"edition": "2021",
290290
"features": {},
291291
"homepage": null,
292-
"id": "path+[ROOTURL]/foo#foo::bar@0.0.0",
292+
"id": "path+[ROOTURL]/foo/foo::bar.rs#foo::bar@0.0.0",
293293
"keywords": [],
294294
"license": null,
295295
"license_file": null,

0 commit comments

Comments
 (0)