forked from rust-lang/cargo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement path-bases (RFC 3529) 2/n:
cargo add
support
RFC: rust-lang/rfcs#3529 Tracking Issue: rust-lang#14355 This PR adds the `--base` option to `cargo add` to allow adding a path dependency with a path base.
- Loading branch information
1 parent
9e152bb
commit ffd37c4
Showing
46 changed files
with
559 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ use std::str::FromStr; | |
use anyhow::Context as _; | ||
use cargo_util::paths; | ||
use cargo_util_schemas::core::PartialVersion; | ||
use cargo_util_schemas::manifest::PathBaseName; | ||
use cargo_util_schemas::manifest::RustVersion; | ||
use indexmap::IndexSet; | ||
use itertools::Itertools; | ||
|
@@ -20,6 +21,7 @@ use toml_edit::Item as TomlItem; | |
use crate::core::dependency::DepKind; | ||
use crate::core::registry::PackageRegistry; | ||
use crate::core::FeatureValue; | ||
use crate::core::Features; | ||
use crate::core::Package; | ||
use crate::core::Registry; | ||
use crate::core::Shell; | ||
|
@@ -28,6 +30,7 @@ use crate::core::Workspace; | |
use crate::sources::source::QueryKind; | ||
use crate::util::cache_lock::CacheLockMode; | ||
use crate::util::style; | ||
use crate::util::toml::lookup_path_base; | ||
use crate::util::toml_mut::dependency::Dependency; | ||
use crate::util::toml_mut::dependency::GitSource; | ||
use crate::util::toml_mut::dependency::MaybeWorkspace; | ||
|
@@ -270,8 +273,11 @@ pub struct DepOp { | |
/// Registry for looking up dependency version | ||
pub registry: Option<String>, | ||
|
||
/// Git repo for dependency | ||
/// File system path for dependency | ||
pub path: Option<String>, | ||
/// Specify a named base for a path dependency | ||
pub base: Option<String>, | ||
|
||
/// Git repo for dependency | ||
pub git: Option<String>, | ||
/// Specify an alternative git branch | ||
|
@@ -331,10 +337,41 @@ fn resolve_dependency( | |
}; | ||
selected | ||
} else if let Some(raw_path) = &arg.path { | ||
let base_name_and_value = if let Some(base_name) = &arg.base { | ||
let workspace_root = ws.root_manifest().parent().unwrap().to_path_buf(); | ||
let workspace_root = || Ok(&workspace_root); | ||
|
||
let features = { | ||
let empty = Vec::new(); | ||
let mut warnings = Vec::new(); | ||
let cargo_features = spec | ||
.manifest() | ||
.original_toml() | ||
.cargo_features | ||
.as_ref() | ||
.unwrap_or(&empty); | ||
let features = Features::new(cargo_features, gctx, &mut warnings, true)?; | ||
if !warnings.is_empty() { | ||
for warning in warnings { | ||
gctx.shell().warn(warning)?; | ||
} | ||
} | ||
features | ||
}; | ||
let base_value = lookup_path_base( | ||
&PathBaseName::new(base_name.clone())?, | ||
gctx, | ||
&workspace_root, | ||
&features, | ||
)?; | ||
Some((base_name.clone(), base_value)) | ||
} else { | ||
None | ||
}; | ||
let path = paths::normalize_path(&std::env::current_dir()?.join(raw_path)); | ||
let src = PathSource::new(&path); | ||
let src = PathSource::new(path); | ||
|
||
let selected = if let Some(crate_spec) = &crate_spec { | ||
let mut selected = if let Some(crate_spec) = &crate_spec { | ||
if let Some(v) = crate_spec.version_req() { | ||
// crate specifier includes a version (e.g. `[email protected]`) | ||
anyhow::bail!("cannot specify a path (`{raw_path}`) with a version (`{v}`)."); | ||
|
@@ -349,10 +386,15 @@ fn resolve_dependency( | |
} | ||
selected | ||
} else { | ||
let mut source = crate::sources::PathSource::new(&path, src.source_id()?, gctx); | ||
let mut source = crate::sources::PathSource::new(&src.path, src.source_id()?, gctx); | ||
let package = source.root_package()?; | ||
Dependency::from(package.summary()) | ||
}; | ||
if let Some(selected_source) = selected.source.as_mut() { | ||
if let Source::Path(selected_source) = selected_source { | ||
selected_source.base_name_and_value = base_name_and_value; | ||
} | ||
} | ||
selected | ||
} else if let Some(crate_spec) = &crate_spec { | ||
crate_spec.to_dependency()? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.