Skip to content

Commit 341d416

Browse files
committed
Extract a fn load_workspace_config()
Reword the documentation for the two `metadata` tables, and suggest consistent usage.
1 parent 866d431 commit 341d416

File tree

3 files changed

+59
-48
lines changed

3 files changed

+59
-48
lines changed

src/cargo/core/workspace.rs

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,26 @@ impl<'cfg> Workspace<'cfg> {
405405
self.custom_metadata.as_ref()
406406
}
407407

408+
pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
409+
// If we didn't find a root, it must mean there is no [workspace] section, and thus no
410+
// metadata.
411+
if let Some(root_path) = &self.root_manifest {
412+
let root_package = self.packages.load(root_path)?;
413+
match root_package.workspace_config() {
414+
WorkspaceConfig::Root(ref root_config) => {
415+
return Ok(Some(root_config.clone()));
416+
}
417+
418+
_ => anyhow::bail!(
419+
"root of a workspace inferred but wasn't a root: {}",
420+
root_path.display()
421+
),
422+
}
423+
}
424+
425+
Ok(None)
426+
}
427+
408428
/// Finds the root of a workspace for the crate whose manifest is located
409429
/// at `manifest_path`.
410430
///
@@ -478,25 +498,10 @@ impl<'cfg> Workspace<'cfg> {
478498
Ok(None)
479499
}
480500

481-
/// After the root of a workspace has been located, loads the `workspace.metadata` table from
482-
/// the workspace root file.
483-
///
484-
/// If no workspace was defined, then no changes occur.
485-
fn load_workspace_metadata(&mut self) -> CargoResult<()> {
486-
// If we didn't find a root, it must mean there is no [workspace] section, and thus no
487-
// metadata.
488-
if let Some(root_path) = &self.root_manifest {
489-
let root_package = self.packages.load(root_path)?;
490-
match root_package.workspace_config() {
491-
WorkspaceConfig::Root(ref root_config) => {
492-
self.custom_metadata = root_config.custom_metadata.clone();
493-
}
494-
495-
_ => anyhow::bail!(
496-
"root of a workspace inferred but wasn't a root: {}",
497-
root_path.display()
498-
),
499-
}
501+
/// After the root of a workspace has been located, sets the custom_metadata, if it exists.
502+
pub fn load_workspace_metadata(&mut self) -> CargoResult<()> {
503+
if let Some(workspace_config) = self.load_workspace_config()? {
504+
self.custom_metadata = workspace_config.custom_metadata;
500505
}
501506

502507
Ok(())
@@ -510,8 +515,8 @@ impl<'cfg> Workspace<'cfg> {
510515
/// will transitively follow all `path` dependencies looking for members of
511516
/// the workspace.
512517
fn find_members(&mut self) -> CargoResult<()> {
513-
let root_manifest_path = match self.root_manifest {
514-
Some(ref path) => path.clone(),
518+
let workspace_config = match self.load_workspace_config()? {
519+
Some(workspace_config) => workspace_config,
515520
None => {
516521
debug!("find_members - only me as a member");
517522
self.members.push(self.current_manifest.clone());
@@ -524,30 +529,20 @@ impl<'cfg> Workspace<'cfg> {
524529
}
525530
};
526531

527-
let members_paths;
528-
let default_members_paths;
529-
{
530-
let root_package = self.packages.load(&root_manifest_path)?;
531-
match *root_package.workspace_config() {
532-
WorkspaceConfig::Root(ref root_config) => {
533-
members_paths = root_config
534-
.members_paths(root_config.members.as_ref().unwrap_or(&vec![]))?;
535-
default_members_paths = if root_manifest_path == self.current_manifest {
536-
if let Some(ref default) = root_config.default_members {
537-
Some(root_config.members_paths(default)?)
538-
} else {
539-
None
540-
}
541-
} else {
542-
None
543-
};
544-
}
545-
_ => anyhow::bail!(
546-
"root of a workspace inferred but wasn't a root: {}",
547-
root_manifest_path.display()
548-
),
532+
// self.root_manifest must be Some to have retrieved workspace_config
533+
let root_manifest_path = self.root_manifest.clone().unwrap();
534+
535+
let members_paths =
536+
workspace_config.members_paths(workspace_config.members.as_ref().unwrap_or(&vec![]))?;
537+
let default_members_paths = if root_manifest_path == self.current_manifest {
538+
if let Some(ref default) = workspace_config.default_members {
539+
Some(workspace_config.members_paths(default)?)
540+
} else {
541+
None
549542
}
550-
}
543+
} else {
544+
None
545+
};
551546

552547
for path in members_paths {
553548
self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)?;

src/doc/src/reference/manifest.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ package-name = "my-awesome-android-app"
420420
assets = "path/to/static"
421421
```
422422

423+
There is a similar table at the workspace level at
424+
[`workspace.metadata`][workspace-metadata]. While cargo does not specify a
425+
format for the content of either of these tables, it is suggested that
426+
external tools may wish to use them in a consistent fashion, such as referring
427+
to the data in `workspace.metadata` if data is missing from `package.metadata`,
428+
if that makes sense for the tool in question.
429+
430+
[workspace-metadata](workspaces.md#the-metadata-table)
431+
423432
#### The `default-run` field
424433

425434
The `default-run` field in the `[package]` section of the manifest can be used

src/doc/src/reference/workspaces.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ default-members = ["path/to/member2", "path/to/member3/foo"]
8282

8383
When specified, `default-members` must expand to a subset of `members`.
8484

85+
<a id="the-metadata-table"></a>
8586
### The `workspace.metadata` table
8687

87-
Like the [`package.metadata`][package-metadata] table, the `workspace.metadata`
88-
table is ignored by Cargo and will not be warned about. This section can be
89-
used for tools that would like to store workspace configuration in
90-
`Cargo.toml`. For example:
88+
The `workspace.metadata` table is ignored by Cargo and will not be warned
89+
about. This section can be used for tools that would like to store workspace
90+
configuration in `Cargo.toml`. For example:
9191

9292
```toml
9393
[workspace]
@@ -99,6 +99,13 @@ tool = ["npm", "run", "build"]
9999
# ...
100100
```
101101

102+
There is a similar set of tables at the package level at
103+
[`package.metadata`][package-metadata]. While cargo does not specify a
104+
format for the content of either of these tables, it is suggested that
105+
external tools may wish to use them in a consistent fashion, such as referring
106+
to the data in `workspace.metadata` if data is missing from `package.metadata`,
107+
if that makes sense for the tool in question.
108+
102109
[package]: manifest.md#the-package-section
103110
[package-metadata]: manifest.md#the-metadata-table
104111
[output directory]: ../guide/build-cache.md

0 commit comments

Comments
 (0)