Skip to content

Commit

Permalink
Add --ignore-rust-version flag to cargo add
Browse files Browse the repository at this point in the history
  • Loading branch information
cassaundra committed May 4, 2023
1 parent aca793c commit 9ddcf9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ The package will be removed from your features.")
Example uses:
- Depending on multiple versions of a crate
- Depend on crates with the same name from different registries"),
flag(
"ignore-rust-version",
"Ignore `rust-version` specification in packages (unstable)"
),
])
.arg_manifest_path()
.arg_package("Package to modify")
Expand Down Expand Up @@ -188,12 +192,23 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {

let dependencies = parse_dependencies(config, args)?;

let honor_rust_version = !args.flag("ignore-rust-version");
if !config.cli_unstable().msrv_policy && !honor_rust_version {
return Err(CliError::new(
anyhow::format_err!(
"the `ignore-rust-version` flag is unstable, pass -Z msrv-policy to enable it"
),
101,
));
}

let options = AddOptions {
config,
spec,
dependencies,
section,
dry_run,
honor_rust_version,
};
add(&ws, &options)?;

Expand Down
21 changes: 17 additions & 4 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct AddOptions<'a> {
pub section: DepTable,
/// Act as if dependencies will be added
pub dry_run: bool,
/// Whether the minimum supported Rust version should be considered during resolution
pub honor_rust_version: bool,
}

/// Add dependencies to a manifest
Expand Down Expand Up @@ -88,6 +90,7 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
workspace,
&options.spec,
&options.section,
options.honor_rust_version,
options.config,
&mut registry,
)
Expand Down Expand Up @@ -259,6 +262,7 @@ fn resolve_dependency(
ws: &Workspace<'_>,
spec: &Package,
section: &DepTable,
honor_rust_version: bool,
config: &Config,
registry: &mut PackageRegistry<'_>,
) -> CargoResult<DependencyUI> {
Expand Down Expand Up @@ -370,7 +374,14 @@ fn resolve_dependency(
}
dependency = dependency.set_source(src);
} else {
let latest = get_latest_dependency(spec, &dependency, false, config, registry)?;
let latest = get_latest_dependency(
spec,
&dependency,
false,
honor_rust_version,
config,
registry,
)?;

if dependency.name != latest.name {
config.shell().warn(format!(
Expand Down Expand Up @@ -523,6 +534,7 @@ fn get_latest_dependency(
spec: &Package,
dependency: &Dependency,
_flag_allow_prerelease: bool,
honor_rust_version: bool,
config: &Config,
registry: &mut PackageRegistry<'_>,
) -> CargoResult<Dependency> {
Expand Down Expand Up @@ -554,7 +566,7 @@ fn get_latest_dependency(
)
})?;

if config.cli_unstable().msrv_policy {
if config.cli_unstable().msrv_policy && honor_rust_version {
if let Some((req_major, req_minor, req_patch)) =
spec.rust_version().map(parse_rust_version)
{
Expand Down Expand Up @@ -584,7 +596,8 @@ fn get_latest_dependency(
.ok_or_else(|| {
let mut error_msg = format!(
"could not find version of crate `{dependency}` that satisfies \
this package's rust-version"
this package's rust-version\n\
help: use `--ignore-rust-version` to override this behavior"
);

if let Some((lowest, _)) = rust_versions
Expand All @@ -605,7 +618,7 @@ fn get_latest_dependency(
if latest_msrv.version() < latest.version() {
config.shell().warn(format_args!(
"selecting older version of `{dependency}` to satisfy this package's \
rust-version"
rust-version (use `--ignore-rust-version` to override)"
))?;
latest = latest_msrv;
}
Expand Down

0 comments on commit 9ddcf9f

Please sign in to comment.