Skip to content

Commit 8c49523

Browse files
committed
Add --ignore-rust-version flag to cargo add
1 parent 4a96edc commit 8c49523

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ The package will be removed from your features.")
7272
Example uses:
7373
- Depending on multiple versions of a crate
7474
- Depend on crates with the same name from different registries"),
75+
flag(
76+
"ignore-rust-version",
77+
"Ignore `rust-version` specification in packages (unstable)"
78+
),
7579
])
7680
.arg_manifest_path()
7781
.arg_package("Package to modify")
@@ -188,12 +192,24 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
188192

189193
let dependencies = parse_dependencies(config, args)?;
190194

195+
let ignore_rust_version = args.flag("ignore-rust-version");
196+
if ignore_rust_version && !config.cli_unstable().msrv_policy {
197+
return Err(CliError::new(
198+
anyhow::format_err!(
199+
"`--ignore-rust-version` is unstable; pass `-Zmsrv-policy` to enable support for it"
200+
),
201+
101,
202+
));
203+
}
204+
let honor_rust_version = !ignore_rust_version;
205+
191206
let options = AddOptions {
192207
config,
193208
spec,
194209
dependencies,
195210
section,
196211
dry_run,
212+
honor_rust_version,
197213
};
198214
add(&ws, &options)?;
199215

src/cargo/ops/cargo_add/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub struct AddOptions<'a> {
5151
pub section: DepTable,
5252
/// Act as if dependencies will be added
5353
pub dry_run: bool,
54+
/// Whether the minimum supported Rust version should be considered during resolution
55+
pub honor_rust_version: bool,
5456
}
5557

5658
/// Add dependencies to a manifest
@@ -88,6 +90,7 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
8890
workspace,
8991
&options.spec,
9092
&options.section,
93+
options.honor_rust_version,
9194
options.config,
9295
&mut registry,
9396
)
@@ -259,6 +262,7 @@ fn resolve_dependency(
259262
ws: &Workspace<'_>,
260263
spec: &Package,
261264
section: &DepTable,
265+
honor_rust_version: bool,
262266
config: &Config,
263267
registry: &mut PackageRegistry<'_>,
264268
) -> CargoResult<DependencyUI> {
@@ -370,7 +374,14 @@ fn resolve_dependency(
370374
}
371375
dependency = dependency.set_source(src);
372376
} else {
373-
let latest = get_latest_dependency(spec, &dependency, false, config, registry)?;
377+
let latest = get_latest_dependency(
378+
spec,
379+
&dependency,
380+
false,
381+
honor_rust_version,
382+
config,
383+
registry,
384+
)?;
374385

375386
if dependency.name != latest.name {
376387
config.shell().warn(format!(
@@ -523,6 +534,7 @@ fn get_latest_dependency(
523534
spec: &Package,
524535
dependency: &Dependency,
525536
_flag_allow_prerelease: bool,
537+
honor_rust_version: bool,
526538
config: &Config,
527539
registry: &mut PackageRegistry<'_>,
528540
) -> CargoResult<Dependency> {
@@ -554,7 +566,7 @@ fn get_latest_dependency(
554566
)
555567
})?;
556568

557-
if config.cli_unstable().msrv_policy {
569+
if config.cli_unstable().msrv_policy && honor_rust_version {
558570
fn parse_msrv(rust_version: impl AsRef<str>) -> (u64, u64, u64) {
559571
// HACK: `rust-version` is a subset of the `VersionReq` syntax that only ever
560572
// has one comparator with a required minor and optional patch, and uses no
@@ -602,7 +614,7 @@ fn get_latest_dependency(
602614
config.shell().warn(format_args!(
603615
"ignoring `{dependency}@{latest_version}` (which has a rust-version of \
604616
{latest_rust_version}) to satisfy this package's rust-version of \
605-
{rust_version}",
617+
{rust_version} (use `--ignore-rust-version` to override)",
606618
latest_version = latest.version(),
607619
latest_rust_version = latest.rust_version().unwrap(),
608620
rust_version = spec.rust_version().unwrap(),
@@ -629,7 +641,8 @@ fn rust_version_incompat_error(
629641
) -> anyhow::Error {
630642
let mut error_msg = format!(
631643
"could not find version of crate `{dep}` that satisfies this package's rust-version of \
632-
{rust_version}"
644+
{rust_version}\n\
645+
help: use `--ignore-rust-version` to override this behavior"
633646
);
634647

635648
if let Some(lowest) = lowest_rust_version {

0 commit comments

Comments
 (0)