Skip to content

Commit

Permalink
Merge pull request #856 from epage/msrv
Browse files Browse the repository at this point in the history
feat(upgrade): Respect package.rust-version
  • Loading branch information
epage committed May 25, 2023
2 parents ee75d84 + 389b977 commit 7848df6
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 57 deletions.
18 changes: 10 additions & 8 deletions README.md
Expand Up @@ -121,14 +121,16 @@ Upgrade dependency version requirements in Cargo.toml manifest files
Usage: cargo upgrade [OPTIONS]

Options:
--dry-run Print changes to be made without making them
--manifest-path <PATH> Path to the manifest to upgrade
--offline Run without accessing the network
--locked Require `Cargo.toml` to be up to date
-v, --verbose... Use verbose output
-Z <FLAG> Unstable (nightly-only) flags
-h, --help Print help
-V, --version Print version
--dry-run Print changes to be made without making them
--manifest-path <PATH> Path to the manifest to upgrade
--offline Run without accessing the network
--locked Require `Cargo.toml` to be up to date
-v, --verbose... Use verbose output
--ignore-rust-version Ignore `rust-version` specification in packages
--rust-version <RUST_VERSION> Override `rust-version`
-Z <FLAG> Unstable (nightly-only) flags
-h, --help Print help
-V, --version Print version

Version:
--compatible [<allow|ignore>] Upgrade to latest compatible version [default: allow]
Expand Down
53 changes: 43 additions & 10 deletions src/bin/upgrade/upgrade.rs
Expand Up @@ -7,7 +7,7 @@ use anyhow::Context as _;
use cargo_edit::{
find, get_compatible_dependency, get_latest_dependency, registry_url, set_dep_version,
shell_note, shell_status, shell_warn, shell_write_stdout, update_registry_index, CargoResult,
CrateSpec, Dependency, LocalManifest, Source,
CrateSpec, Dependency, LocalManifest, RustVersion, Source,
};
use clap::Args;
use indexmap::IndexMap;
Expand Down Expand Up @@ -38,6 +38,14 @@ pub struct UpgradeArgs {
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,

/// Ignore `rust-version` specification in packages
#[arg(long)]
ignore_rust_version: bool,

/// Override `rust-version`
#[arg(long, conflicts_with = "ignore_rust_version")]
rust_version: Option<RustVersion>,

/// Unstable (nightly-only) flags
#[arg(short = 'Z', value_name = "FLAG", global = true, value_enum)]
unstable_features: Vec<UnstableOptions>,
Expand Down Expand Up @@ -162,12 +170,35 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
let manifests = find_ws_members(&metadata);
let mut manifests = manifests
.into_iter()
.map(|p| (p.name, p.manifest_path.as_std_path().to_owned()))
.map(|p| {
let rust_version = if args.rust_version.is_some() {
args.rust_version
} else if args.ignore_rust_version {
None
} else {
p.rust_version.as_ref().map(RustVersion::from)
};

(
p.name,
p.manifest_path.as_std_path().to_owned(),
rust_version,
)
})
.collect::<Vec<_>>();
if !manifests.iter().any(|(_, p)| *p == root_manifest_path) {
if !manifests.iter().any(|(_, p, _)| *p == root_manifest_path) {
let workspace_rust_version = manifests
.iter()
.map(|(_, _, msrv)| *msrv)
.min_by_key(|msrv| msrv.unwrap_or(RustVersion::MAX))
.flatten();
manifests.insert(
0,
("virtual workspace".to_owned(), root_manifest_path.clone()),
(
"virtual workspace".to_owned(),
root_manifest_path.clone(),
workspace_rust_version,
),
);
}

Expand All @@ -187,8 +218,8 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
let mut pinned_present = false;
let mut incompatible_present = false;
let mut uninteresting_crates = BTreeSet::new();
for (pkg_name, manifest_path) in &manifests {
let mut manifest = LocalManifest::try_new(manifest_path)?;
for (pkg_name, manifest_path, rust_version) in manifests {
let mut manifest = LocalManifest::try_new(&manifest_path)?;
let mut crate_modified = false;
let mut table = Vec::new();
shell_status("Checking", &format!("{pkg_name}'s dependencies"))?;
Expand All @@ -197,7 +228,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
let mut reason = None;

let dep_key = dep_key.get();
let dependency = match Dependency::from_toml(manifest_path, dep_key, dep_item) {
let dependency = match Dependency::from_toml(&manifest_path, dep_key, dep_item) {
Ok(dependency) => dependency,
Err(err) => {
shell_warn(&format!("ignoring {dep_key}, unsupported entry: {err}"))?;
Expand Down Expand Up @@ -266,7 +297,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
// we're offline.
let registry_url = dependency
.registry()
.map(|registry| registry_url(manifest_path, Some(registry)))
.map(|registry| registry_url(&manifest_path, Some(registry)))
.transpose()?;
if !args.offline {
if let Some(registry_url) = &registry_url {
Expand All @@ -281,7 +312,8 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
get_compatible_dependency(
&dependency.name,
&old_version_req,
manifest_path,
rust_version,
&manifest_path,
registry_url.as_ref(),
)
.ok()
Expand All @@ -295,7 +327,8 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
let latest_version = get_latest_dependency(
&dependency.name,
is_prerelease,
manifest_path,
rust_version,
&manifest_path,
registry_url.as_ref(),
)
.map(|d| {
Expand Down

0 comments on commit 7848df6

Please sign in to comment.