Skip to content

Commit

Permalink
fix(285): Added switch ignore-external-rel to workaround issue 285
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofus Mortensen authored and kbknapp committed Mar 2, 2022
1 parent 4d1cf73 commit ffbb249
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
17 changes: 14 additions & 3 deletions src/cargo_ops/elaborate_workspace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::io::{self, Write};
use std::rc::Rc;

Expand Down Expand Up @@ -206,6 +206,7 @@ impl<'ela> ElaborateWorkspace<'ela> {
options: &Options,
_config: &Config,
root: PackageId,
skip: &HashSet<String>
) -> CargoResult<()> {
self.pkg_status.borrow_mut().clear();
let (compat_root, latest_root) = if self.workspace_mode {
Expand Down Expand Up @@ -235,14 +236,17 @@ impl<'ela> ElaborateWorkspace<'ela> {
compat_pkg,
latest_pkg,
status
);
);
self.pkg_status.borrow_mut().insert(path.clone(), status);
// next layer
// this unwrap is safe since we first check if it is None :)
if options.depth.is_none() || depth < options.depth.unwrap() {
self.pkg_deps[pkg]
.keys()
.filter(|dep| !path.contains(dep))
.filter(|&dep| {
!skip.contains(dep.name().as_str())
})
.for_each(|&dep| {
let name = dep.name();
let compat_pkg = compat_pkg
Expand Down Expand Up @@ -271,6 +275,7 @@ impl<'ela> ElaborateWorkspace<'ela> {
options: &Options,
root: PackageId,
preceding_line: bool,
skip: &HashSet<String>,
) -> CargoResult<i32> {
let mut lines = BTreeSet::new();
let mut queue = VecDeque::new();
Expand Down Expand Up @@ -334,6 +339,9 @@ impl<'ela> ElaborateWorkspace<'ela> {
!self.workspace_mode
|| !self.workspace.members().any(|mem| &mem.package_id() == dep)
})
.filter(|&dep| {
!skip.contains(dep.name().as_str())
})
.for_each(|&dep| {
let mut path = path.clone();
path.push(dep);
Expand Down Expand Up @@ -367,7 +375,7 @@ impl<'ela> ElaborateWorkspace<'ela> {
Ok(lines.len() as i32)
}

pub fn print_json(&'ela self, options: &Options, root: PackageId) -> CargoResult<i32> {
pub fn print_json(&'ela self, options: &Options, root: PackageId, skip: &HashSet<String>) -> CargoResult<i32> {
let mut crate_graph = CrateMetadata {
crate_name: root.name().to_string(),
dependencies: BTreeSet::new(),
Expand Down Expand Up @@ -447,6 +455,9 @@ impl<'ela> ElaborateWorkspace<'ela> {
.members()
.any(|mem| mem.package_id() == **dep)
})
.filter(|&dep| {
!skip.contains(dep.name().as_str())
})
.for_each(|dep| {
let mut path = path.clone();
path.push(*dep);
Expand Down
57 changes: 40 additions & 17 deletions src/cargo_ops/temp_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ impl<'tmp> TempProject<'tmp> {
Ok(())
}

fn manipulate_dependencies<F>(manifest: &mut Manifest, f: &F) -> CargoResult<()>
fn manipulate_dependencies<F>(manifest: &mut Manifest, f: &mut F) -> CargoResult<()>
where
F: Fn(&mut Table) -> CargoResult<()>,
F: FnMut(&mut Table) -> CargoResult<()>,
{
if let Some(dep) = manifest.dependencies.as_mut() {
f(dep)?;
Expand Down Expand Up @@ -265,6 +265,7 @@ impl<'tmp> TempProject<'tmp> {
orig_root: P,
tmp_root: P,
workspace: &ElaborateWorkspace<'_>,
skipped: &mut HashSet<String>,
) -> CargoResult<()> {
let bin = {
let mut bin = Table::new();
Expand All @@ -286,19 +287,20 @@ impl<'tmp> TempProject<'tmp> {
if let Some(lib) = manifest.lib.as_mut() {
lib.insert("path".to_owned(), Value::String("test_lib.rs".to_owned()));
}
Self::manipulate_dependencies(&mut manifest, &|deps| {
Self::manipulate_dependencies(&mut manifest, &mut |deps| {
Self::replace_path_with_absolute(
self,
deps,
orig_root.as_ref(),
tmp_root.as_ref(),
manifest_path,
skipped,
)
})?;

let package_name = manifest.name();
let features = manifest.features.clone();
Self::manipulate_dependencies(&mut manifest, &|deps| {
Self::manipulate_dependencies(&mut manifest, &mut |deps| {
self.update_version_and_feature(deps, &features, workspace, &package_name, false)
})?;

Expand All @@ -317,6 +319,7 @@ impl<'tmp> TempProject<'tmp> {
orig_root: P,
tmp_root: P,
workspace: &ElaborateWorkspace<'_>,
skipped: &mut HashSet<String>,
) -> CargoResult<()> {
let bin = {
let mut bin = Table::new();
Expand All @@ -337,20 +340,23 @@ impl<'tmp> TempProject<'tmp> {
if let Some(lib) = manifest.lib.as_mut() {
lib.insert("path".to_owned(), Value::String("test_lib.rs".to_owned()));
}
Self::manipulate_dependencies(&mut manifest, &|deps| {
Self::manipulate_dependencies(&mut manifest, &mut |deps| {
Self::replace_path_with_absolute(
self,
deps,
orig_root.as_ref(),
tmp_root.as_ref(),
manifest_path,
skipped,
)
})?;

let package_name = manifest.name();
let features = manifest.features.clone();
Self::manipulate_dependencies(&mut manifest, &|deps| {
Self::manipulate_dependencies(&mut manifest, &mut |deps| {
self.update_version_and_feature(deps, &features, workspace, &package_name, true)
})?;

Self::write_manifest(&manifest, manifest_path)?;
}

Expand Down Expand Up @@ -633,6 +639,7 @@ impl<'tmp> TempProject<'tmp> {
orig_root: &Path,
tmp_root: &Path,
tmp_manifest: &Path,
skipped: &mut HashSet<String>,
) -> CargoResult<()> {
let dep_names: Vec<_> = dependencies.keys().cloned().collect();
for name in dep_names {
Expand All @@ -642,7 +649,7 @@ impl<'tmp> TempProject<'tmp> {
.ok_or(OutdatedError::NoMatchingDependency)?;
match original {
Value::Table(ref t) if t.contains_key("path") => {
if let Value::String(ref orig_path) = t["path"] {
if let Value::String(ref orig_path) = t["path"] {
let orig_path = Path::new(orig_path);
if orig_path.is_relative() {
let relative = {
Expand All @@ -655,16 +662,32 @@ impl<'tmp> TempProject<'tmp> {
relative.join(orig_path)
};
if !tmp_root.join(&relative).join("Cargo.toml").exists() {
let mut replaced = t.clone();
replaced.insert(
"path".to_owned(),
Value::String(
fs::canonicalize(orig_root.join(relative))?
.to_string_lossy()
.to_string(),
),
);
dependencies.insert(name, Value::Table(replaced));
if self.options.root_deps_only {
dependencies.remove(&name);

if t.contains_key("package") {
if let Value::String(ref package_name) = t["package"] {
skipped.insert(package_name.to_string());
} else {
skipped.insert(name);
}
}
else {
skipped.insert(name);
}

} else {
let mut replaced = t.clone();
replaced.insert(
"path".to_owned(),
Value::String(
fs::canonicalize(orig_root.join(relative))?
.to_string_lossy()
.to_string(),
),
);
dependencies.insert(name, Value::Table(replaced));
}
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Options {
pub root_deps_only: bool,
pub workspace: bool,
pub aggressive: bool,
pub workspace_only: bool,
pub offline: bool,
}

Expand Down Expand Up @@ -105,6 +106,7 @@ impl<'a> From<&ArgMatches<'a>> for Options {
root: m.value_of("root").map(ToOwned::to_owned),
depth: value_t!(m, "depth", i32).ok(),
root_deps_only: m.is_present("root-deps-only"),
workspace_only: m.is_present("ignore-external-rel"),
workspace: m.is_present("workspace"),
aggressive: m.is_present("aggressive"),
offline: m.is_present("offline"),
Expand All @@ -114,6 +116,11 @@ impl<'a> From<&ArgMatches<'a>> for Options {
opts.depth = Some(1);
}

if m.is_present("ignore-external-rel") {
opts.depth = Some(1);
opts.root_deps_only = true;
}

opts
}
}
Expand Down Expand Up @@ -145,6 +152,12 @@ fn build() -> App<'static, 'static> {
.long("root-deps-only")
.help("Only check root dependencies (Equivalent to --depth=1)"),
)
.arg(
Arg::with_name("ignore-external-rel")
.short("e")
.long("ignore-external-rel")
.help("Ignore relative dependencies external to workspace and check root dependencies only."),
)
.arg(
Arg::with_name("workspace")
.short("w")
Expand Down Expand Up @@ -289,6 +302,20 @@ mod test {
)
}

#[test]
fn workspace_only() {
let opts = options(&["--ignore-external-rel"]);
assert_eq!(
Options {
workspace_only: true,
depth: Some(1),
root_deps_only: true,
..Options::default()
},
opts
)
}

#[test]
fn features() {
let opts1 = options(&["--features=one,two,three"]);
Expand Down
16 changes: 11 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod cargo_ops;
mod cli;
mod error;

use std::collections::HashSet;

use cargo::core::shell::Verbosity;
use cargo::core::Workspace;
use cargo::ops::needs_custom_http_transport;
Expand Down Expand Up @@ -116,12 +118,14 @@ pub fn execute(options: Options, config: &mut Config) -> CargoResult<i32> {
}

verbose!(config, "Parsing...", "compat workspace");
let mut skipped = HashSet::new();
let compat_proj =
TempProject::from_workspace(&ela_curr, &curr_manifest.to_string_lossy(), &options)?;
compat_proj.write_manifest_semver(
curr_workspace.root(),
compat_proj.temp_dir.path(),
&ela_curr,
&mut skipped,
)?;
verbose!(config, "Updating...", "compat workspace");
compat_proj.cargo_update()?;
Expand All @@ -141,6 +145,7 @@ pub fn execute(options: Options, config: &mut Config) -> CargoResult<i32> {
curr_workspace.root(),
compat_proj.temp_dir.path(),
&ela_curr,
&mut skipped,
)?;
verbose!(config, "Updating...", "latest workspace");
latest_proj.cargo_update()?;
Expand All @@ -167,13 +172,14 @@ pub fn execute(options: Options, config: &mut Config) -> CargoResult<i32> {
&options,
config,
member.package_id(),
&skipped,
)?;
match options.format {
Format::List => {
sum += ela_curr.print_list(&options, member.package_id(), sum > 0)?;
sum += ela_curr.print_list(&options, member.package_id(), sum > 0, &skipped)?;
}
Format::Json => {
sum += ela_curr.print_json(&options, member.package_id())?;
sum += ela_curr.print_json(&options, member.package_id(), &skipped)?;
}
}
}
Expand All @@ -184,16 +190,16 @@ pub fn execute(options: Options, config: &mut Config) -> CargoResult<i32> {
} else {
verbose!(config, "Resolving...", "package status");
let root = ela_curr.determine_root(&options)?;
ela_curr.resolve_status(&ela_compat, &ela_latest, &options, config, root)?;
ela_curr.resolve_status(&ela_compat, &ela_latest, &options, config, root, &skipped)?;
verbose!(config, "Printing...", "list format");
let mut count = 0;

match options.format {
Format::List => {
count = ela_curr.print_list(&options, root, false)?;
count = ela_curr.print_list(&options, root, false, &skipped)?;
}
Format::Json => {
ela_curr.print_json(&options, root)?;
ela_curr.print_json(&options, root, &skipped)?;
}
}

Expand Down

0 comments on commit ffbb249

Please sign in to comment.