Skip to content

Commit 3a80b98

Browse files
authored
Merge pull request #90 from secona/fix/read-manifest-wrong-root
fix(read-manifest): Wrong root getting outputted
2 parents 936c0ba + c8fa8e3 commit 3a80b98

File tree

2 files changed

+113
-174
lines changed

2 files changed

+113
-174
lines changed
Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::PathBuf;
22

3+
use anyhow::Context as _;
34
use cargo::core::{
45
find_workspace_root, EitherManifest, MaybePackage, SourceId, Workspace, WorkspaceConfig,
56
};
@@ -19,10 +20,10 @@ pub(crate) struct Args {
1920
}
2021

2122
pub(crate) fn exec(gctx: &mut GlobalContext, args: Args) -> CargoResult<()> {
22-
let manifest_path = gctx.cwd().join(args.manifest_path);
23+
let requested_manifest_path = gctx.cwd().join(args.manifest_path);
2324

2425
if args.workspace {
25-
let workspace = Workspace::new(&manifest_path, gctx)?;
26+
let workspace = Workspace::new(&requested_manifest_path, gctx)?;
2627

2728
// Here, we print the root Package or the Virtual Manifest of the workspace.
2829
let msg = match workspace.root_maybe() {
@@ -55,73 +56,72 @@ pub(crate) fn exec(gctx: &mut GlobalContext, args: Args) -> CargoResult<()> {
5556
}
5657
} else {
5758
// As this is the branch without `--workspace`, we want to only read one manifest.
58-
let source_id = SourceId::for_manifest_path(&manifest_path)?;
59-
let (pkg_id, ws_config, manifest) = match read_manifest(&manifest_path, source_id, gctx)? {
60-
EitherManifest::Real(r) => (
61-
Some(r.package_id().to_spec()),
62-
r.workspace_config().clone(),
63-
r.normalized_toml().clone(),
64-
),
65-
EitherManifest::Virtual(v) => (
66-
None,
67-
v.workspace_config().clone(),
68-
v.normalized_toml().clone(),
69-
),
70-
};
59+
let source_id = SourceId::for_manifest_path(&requested_manifest_path)?;
60+
let (pkg_id, ws_config, manifest) =
61+
match read_manifest(&requested_manifest_path, source_id, gctx)? {
62+
EitherManifest::Real(r) => (
63+
Some(r.package_id().to_spec()),
64+
r.workspace_config().clone(),
65+
r.normalized_toml().clone(),
66+
),
67+
EitherManifest::Virtual(v) => (
68+
None,
69+
v.workspace_config().clone(),
70+
v.normalized_toml().clone(),
71+
),
72+
};
7173

7274
let msg = ReadManifestOut::Manifest {
73-
path: manifest_path.clone(),
75+
path: requested_manifest_path.clone(),
7476
pkg_id,
7577
manifest,
7678
};
7779
gctx.shell().print_json(&msg)?;
7880

79-
// We also want to print the root workspace even if no `--workspace` flag is provided.
80-
match ws_config {
81+
// If the current manifest is a workspace member, find its workspace manifest path.
82+
let ws_manifest_path = match ws_config {
8183
WorkspaceConfig::Root(..) => {
82-
// Skip if the current manifest *is* the workspace manifest.
84+
// The current manifest is already the workspace root, so there is no other
85+
// workspace root to find.
86+
None
8387
}
8488
WorkspaceConfig::Member {
8589
root: Some(path_to_root),
8690
} => {
87-
// This case is when the workspace members is defined through the package workspace
88-
// key. Hence, we make it into a `PathBuf` first.
89-
let path_to_root = PathBuf::from(path_to_root);
90-
print_workspace_root(gctx, manifest_path, path_to_root)?;
91+
// The member's `[package.workspace]` key points to the root directory
92+
Some(
93+
requested_manifest_path
94+
.parent()
95+
.context("manifest-path can't be root")?
96+
.join(path_to_root)
97+
.join("Cargo.toml"),
98+
)
9199
}
92100
WorkspaceConfig::Member { root: None } => {
93-
// This case is the common case for workspace members where the members are defined
94-
// from the workspace manifest
95-
if let Some(path_to_root) = find_workspace_root(&manifest_path, gctx)? {
96-
print_workspace_root(gctx, manifest_path, path_to_root)?;
97-
}
101+
// Find the root directory by searching upwards the filesystem.
102+
find_workspace_root(&requested_manifest_path, gctx)?
98103
}
99104
};
100-
}
101105

102-
Ok(())
103-
}
104-
105-
/// Given a manifest path the the path to workspace root, we print the manifest there.
106-
fn print_workspace_root(
107-
gctx: &GlobalContext,
108-
manifest_path: PathBuf,
109-
path_to_root: PathBuf,
110-
) -> CargoResult<()> {
111-
let workspace_path = paths::normalize_path(&gctx.cwd().join(path_to_root));
112-
let source_id = SourceId::for_manifest_path(&workspace_path)?;
106+
if let Some(ws_manifest_path) = ws_manifest_path {
107+
let ws_manifest_path = paths::normalize_path(&gctx.cwd().join(ws_manifest_path));
108+
let source_id = SourceId::for_manifest_path(&ws_manifest_path)?;
113109

114-
let (pkg_id, manifest) = match read_manifest(&manifest_path, source_id, gctx)? {
115-
EitherManifest::Real(r) => (Some(r.package_id().to_spec()), r.normalized_toml().clone()),
116-
EitherManifest::Virtual(v) => (None, v.normalized_toml().clone()),
117-
};
110+
let (pkg_id, manifest) = match read_manifest(&ws_manifest_path, source_id, gctx)? {
111+
EitherManifest::Real(r) => {
112+
(Some(r.package_id().to_spec()), r.normalized_toml().clone())
113+
}
114+
EitherManifest::Virtual(v) => (None, v.normalized_toml().clone()),
115+
};
118116

119-
let msg = ReadManifestOut::Manifest {
120-
path: manifest_path.clone(),
121-
pkg_id,
122-
manifest,
123-
};
124-
gctx.shell().print_json(&msg)?;
117+
let msg = ReadManifestOut::Manifest {
118+
path: ws_manifest_path.clone(),
119+
pkg_id,
120+
manifest,
121+
};
122+
gctx.shell().print_json(&msg)?;
123+
}
124+
}
125125

126126
Ok(())
127127
}

tests/testsuite/read_manifest.rs

Lines changed: 63 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,54 +1382,35 @@ fn workspace_member_with_inherited_deps() {
13821382
"manifest": {
13831383
"badges": null,
13841384
"bench": [],
1385-
"bin": [],
1385+
"bin": [
1386+
{
1387+
"bench": null,
1388+
"crate-type": null,
1389+
"crate_type": null,
1390+
"doc": null,
1391+
"doc-scrape-examples": null,
1392+
"doctest": null,
1393+
"edition": null,
1394+
"filename": null,
1395+
"harness": null,
1396+
"name": "read-manifest-test",
1397+
"path": "src/main.rs",
1398+
"proc-macro": null,
1399+
"proc_macro": null,
1400+
"required-features": null,
1401+
"test": null
1402+
}
1403+
],
13861404
"build-dependencies": null,
13871405
"build_dependencies": null,
13881406
"cargo-features": null,
1389-
"dependencies": {
1390-
"a": {
1391-
"artifact": null,
1392-
"base": null,
1393-
"branch": null,
1394-
"default-features": null,
1395-
"default_features": null,
1396-
"features": null,
1397-
"git": null,
1398-
"lib": null,
1399-
"optional": null,
1400-
"package": null,
1401-
"path": null,
1402-
"public": null,
1403-
"registry": null,
1404-
"registry-index": null,
1405-
"rev": null,
1406-
"tag": null,
1407-
"target": null,
1408-
"version": "1.0.0"
1409-
}
1410-
},
1407+
"dependencies": null,
14111408
"dev-dependencies": null,
14121409
"dev_dependencies": null,
14131410
"example": [],
14141411
"features": null,
14151412
"hints": null,
1416-
"lib": {
1417-
"bench": null,
1418-
"crate-type": null,
1419-
"crate_type": null,
1420-
"doc": null,
1421-
"doc-scrape-examples": null,
1422-
"doctest": null,
1423-
"edition": null,
1424-
"filename": null,
1425-
"harness": null,
1426-
"name": "crate1",
1427-
"path": "src/lib.rs",
1428-
"proc-macro": null,
1429-
"proc_macro": null,
1430-
"required-features": null,
1431-
"test": null
1432-
},
1413+
"lib": null,
14331414
"lints": null,
14341415
"package": {
14351416
"authors": [],
@@ -1456,7 +1437,7 @@ fn workspace_member_with_inherited_deps() {
14561437
"links": null,
14571438
"metabuild": null,
14581439
"metadata": null,
1459-
"name": "crate1",
1440+
"name": "read-manifest-test",
14601441
"publish": null,
14611442
"readme": false,
14621443
"repository": null,
@@ -1471,10 +1452,24 @@ fn workspace_member_with_inherited_deps() {
14711452
"replace": null,
14721453
"target": null,
14731454
"test": [],
1474-
"workspace": null
1455+
"workspace": {
1456+
"default-members": null,
1457+
"dependencies": {
1458+
"a": "1.0.0"
1459+
},
1460+
"exclude": null,
1461+
"lints": null,
1462+
"members": [
1463+
"crate1",
1464+
"crate2"
1465+
],
1466+
"metadata": null,
1467+
"package": null,
1468+
"resolver": "3"
1469+
}
14751470
},
1476-
"path": "[ROOT]/foo/crate1/Cargo.toml",
1477-
"pkg_id": "path+[ROOTURL]/foo#crate1@0.1.0",
1471+
"path": "[ROOT]/foo/Cargo.toml",
1472+
"pkg_id": "path+[ROOTURL]/foo#read-manifest-test@0.1.0",
14781473
"reason": "manifest"
14791474
}
14801475
]
@@ -1930,100 +1925,44 @@ fn workspace_member_via_package_workspace_key() {
19301925
{
19311926
"manifest": {
19321927
"badges": null,
1933-
"bench": [],
1934-
"bin": [],
1928+
"bench": null,
1929+
"bin": null,
19351930
"build-dependencies": null,
19361931
"build_dependencies": null,
19371932
"cargo-features": null,
1938-
"dependencies": {
1939-
"a": {
1940-
"artifact": null,
1941-
"base": null,
1942-
"branch": null,
1943-
"default-features": null,
1944-
"default_features": null,
1945-
"features": null,
1946-
"git": null,
1947-
"lib": null,
1948-
"optional": null,
1949-
"package": null,
1950-
"path": null,
1951-
"public": null,
1952-
"registry": null,
1953-
"registry-index": null,
1954-
"rev": null,
1955-
"tag": null,
1956-
"target": null,
1957-
"version": "1.0.0"
1958-
}
1959-
},
1933+
"dependencies": null,
19601934
"dev-dependencies": null,
19611935
"dev_dependencies": null,
1962-
"example": [],
1936+
"example": null,
19631937
"features": null,
19641938
"hints": null,
1965-
"lib": {
1966-
"bench": null,
1967-
"crate-type": null,
1968-
"crate_type": null,
1969-
"doc": null,
1970-
"doc-scrape-examples": null,
1971-
"doctest": null,
1972-
"edition": null,
1973-
"filename": null,
1974-
"harness": null,
1975-
"name": "crate1",
1976-
"path": "src/lib.rs",
1977-
"proc-macro": null,
1978-
"proc_macro": null,
1979-
"required-features": null,
1980-
"test": null
1981-
},
1939+
"lib": null,
19821940
"lints": null,
1983-
"package": {
1984-
"authors": [],
1985-
"autobenches": false,
1986-
"autobins": false,
1987-
"autoexamples": false,
1988-
"autolib": false,
1989-
"autotests": false,
1990-
"build": false,
1991-
"categories": null,
1992-
"default-run": null,
1993-
"default-target": null,
1994-
"description": null,
1995-
"documentation": null,
1996-
"edition": "2024",
1997-
"exclude": null,
1998-
"forced-target": null,
1999-
"homepage": null,
2000-
"im-a-teapot": null,
2001-
"include": null,
2002-
"keywords": null,
2003-
"license": null,
2004-
"license-file": null,
2005-
"links": null,
2006-
"metabuild": null,
2007-
"metadata": null,
2008-
"name": "crate1",
2009-
"publish": null,
2010-
"readme": false,
2011-
"repository": null,
2012-
"resolver": null,
2013-
"rust-version": null,
2014-
"version": "0.1.0",
2015-
"workspace": "../workspace-root"
2016-
},
1941+
"package": null,
20171942
"patch": null,
20181943
"profile": null,
20191944
"project": null,
20201945
"replace": null,
20211946
"target": null,
2022-
"test": [],
2023-
"workspace": null
1947+
"test": null,
1948+
"workspace": {
1949+
"default-members": null,
1950+
"dependencies": {
1951+
"a": "1.0.0"
1952+
},
1953+
"exclude": null,
1954+
"lints": null,
1955+
"members": [
1956+
"../crate1",
1957+
"../crate2"
1958+
],
1959+
"metadata": null,
1960+
"package": null,
1961+
"resolver": "3"
1962+
}
20241963
},
2025-
"path": "[ROOT]/foo/crate1/Cargo.toml",
2026-
"pkg_id": "path+[ROOTURL]#[email protected]",
1964+
"path": "[ROOT]/foo/workspace-root/Cargo.toml",
1965+
"pkg_id": null,
20271966
"reason": "manifest"
20281967
}
20291968
]

0 commit comments

Comments
 (0)