From 0925e0f7ccbf0e17305ccb6e2fc524b7dfdf2fe6 Mon Sep 17 00:00:00 2001 From: Tastaturtaste <31124715+Tastaturtaste@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:35:39 +0100 Subject: [PATCH] Fix filtering for workspace crates comparing canonical with non-canonical paths (#94) * Fix filtering for workspace crates comparing canonical with non-canonical paths * Add test * Fix lints --------- Co-authored-by: Jake Shadle --- src/builder.rs | 10 +- src/cm.rs | 23 +- tests/exclude.rs | 10 + .../snapshots/exclude__includes_by_path.snap | 443 ++++++++++++++++++ 4 files changed, 468 insertions(+), 18 deletions(-) create mode 100644 tests/snapshots/exclude__includes_by_path.snap diff --git a/src/builder.rs b/src/builder.rs index be77a75..cfed3f1 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -662,11 +662,11 @@ impl Builder { } else { for wm in &workspace_members { if let Ok(i) = packages.binary_search_by(|(id, _pkg)| id.cmp(wm)) { - if self - .workspace_filters - .iter() - .any(|wf| wf == &packages[i].1.manifest_path) - { + let mp = packages[i].1.manifest_path.as_std_path(); + let manifest_path = mp.canonicalize(); + + let mp = manifest_path.as_deref().unwrap_or(mp); + if self.workspace_filters.iter().any(|wf| wf == mp) { roots.insert(wm); } } diff --git a/src/cm.rs b/src/cm.rs index cb328c4..978547e 100644 --- a/src/cm.rs +++ b/src/cm.rs @@ -60,19 +60,16 @@ pub struct Metadata { impl Metadata { /// Get the workspace's root package of this metadata instance. pub fn root_package(&self) -> Option<&Package> { - match &self.resolve { - Some(resolve) => { - // if dependencies are resolved, use Cargo's answer - let root = resolve.root.as_ref()?; - self.packages.iter().find(|pkg| &pkg.id == root) - } - None => { - // if dependencies aren't resolved, check for a root package manually - let root_manifest_path = self.workspace_root.join("Cargo.toml"); - self.packages - .iter() - .find(|pkg| pkg.manifest_path == root_manifest_path) - } + if let Some(resolve) = &self.resolve { + // if dependencies are resolved, use Cargo's answer + let root = resolve.root.as_ref()?; + self.packages.iter().find(|pkg| &pkg.id == root) + } else { + // if dependencies aren't resolved, check for a root package manually + let root_manifest_path = self.workspace_root.join("Cargo.toml"); + self.packages + .iter() + .find(|pkg| pkg.manifest_path == root_manifest_path) } } diff --git a/tests/exclude.rs b/tests/exclude.rs index a1e35f0..296aea1 100644 --- a/tests/exclude.rs +++ b/tests/exclude.rs @@ -13,6 +13,16 @@ fn excludes_workspace_member() { insta::assert_snapshot!(grafs.dotgraph()); } +#[test] +fn includes_by_path() { + let mut kb = krates::Builder::new(); + kb.include_workspace_crates(["/home/jake/code/krates/tests/ws/c"]); + + let grafs = build("all-features.json", kb).unwrap(); + + insta::assert_snapshot!(grafs.dotgraph()); +} + #[test] fn excludes_dependencies() { let mut kb = krates::Builder::new(); diff --git a/tests/snapshots/exclude__includes_by_path.snap b/tests/snapshots/exclude__includes_by_path.snap new file mode 100644 index 0000000..e65e6b8 --- /dev/null +++ b/tests/snapshots/exclude__includes_by_path.snap @@ -0,0 +1,443 @@ +--- +source: tests/exclude.rs +expression: grafs.dotgraph() +snapshot_kind: text +--- +digraph { + 0 [ label = "crate aho-corasick 0.7.6" ] + 1 [ label = "crate anyhow 1.0.26" ] + 2 [ label = "crate bindgen 0.51.1" ] + 3 [ label = "crate bitflags 1.2.1" ] + 4 [ label = "crate bumpalo 3.1.2" ] + 5 [ label = "crate byteorder 1.3.2" ] + 6 [ label = "crate c 0.1.0 path+file:///krates/tests/ws/c" ] + 7 [ label = "crate cc 1.0.50" ] + 8 [ label = "crate cc 1.0.84 git+https://github.com/alexcrichton/cc-rs?branch=main" ] + 9 [ label = "crate cexpr 0.3.6" ] + 10 [ label = "crate cfg-if 0.1.10" ] + 11 [ label = "crate clang-sys 0.28.1" ] + 12 [ label = "crate coreaudio-rs 0.9.1" ] + 13 [ label = "crate coreaudio-sys 0.2.3" ] + 14 [ label = "crate difference 2.0.0" ] + 15 [ label = "crate glob 0.3.0" ] + 16 [ label = "crate heck 0.3.1" ] + 17 [ label = "crate js-sys 0.3.35" ] + 18 [ label = "crate lazy_static 1.4.0" ] + 19 [ label = "crate leftpad 0.2.0" ] + 20 [ label = "crate libc 0.2.66" ] + 21 [ label = "crate libloading 0.5.2" ] + 22 [ label = "crate log 0.4.8" ] + 23 [ label = "crate memchr 2.2.1" ] + 24 [ label = "crate nix 0.16.1" ] + 25 [ label = "crate nom 4.2.3" ] + 26 [ label = "crate peeking_take_while 0.1.2" ] + 27 [ label = "crate proc-macro2 1.0.7" ] + 28 [ label = "crate quote 1.0.2" ] + 29 [ label = "crate regex 1.3.3" ] + 30 [ label = "crate regex-syntax 0.6.13" ] + 31 [ label = "crate rustc-hash 1.0.1" ] + 32 [ label = "crate shlex 0.1.1" ] + 33 [ label = "crate sourcefile 0.1.4" ] + 34 [ label = "crate spin 0.5.2" ] + 35 [ label = "crate syn 1.0.13" ] + 36 [ label = "crate thread_local 1.0.0" ] + 37 [ label = "crate unicode-segmentation 1.6.0" ] + 38 [ label = "crate unicode-xid 0.2.0" ] + 39 [ label = "crate version_check 0.1.5" ] + 40 [ label = "crate void 1.0.2" ] + 41 [ label = "crate wasm-bindgen 0.2.58" ] + 42 [ label = "crate wasm-bindgen-backend 0.2.58" ] + 43 [ label = "crate wasm-bindgen-macro 0.2.58" ] + 44 [ label = "crate wasm-bindgen-macro-support 0.2.58" ] + 45 [ label = "crate wasm-bindgen-shared 0.2.58" ] + 46 [ label = "crate wasm-bindgen-webidl 0.2.58" ] + 47 [ label = "crate web-sys 0.3.35" ] + 48 [ label = "crate weedle 0.10.0" ] + 49 [ label = "crate winapi 0.2.8" ] + 50 [ label = "crate winapi 0.3.8" ] + 51 [ label = "crate winapi-i686-pc-windows-gnu 0.4.0" ] + 52 [ label = "crate winapi-x86_64-pc-windows-gnu 0.4.0" ] + 53 [ label = "feature use_std" ] + 54 [ label = "feature default" ] + 55 [ label = "feature runtime" ] + 56 [ label = "feature clang_6_0" ] + 57 [ label = "feature default" ] + 58 [ label = "feature default" ] + 59 [ label = "feature default" ] + 60 [ label = "feature Crypto" ] + 61 [ label = "feature Window" ] + 62 [ label = "feature verbose-errors" ] + 63 [ label = "feature default" ] + 64 [ label = "feature audio_toolbox" ] + 65 [ label = "feature audio_unit" ] + 66 [ label = "feature core_audio" ] + 67 [ label = "feature core_midi" ] + 68 [ label = "feature open_al" ] + 69 [ label = "feature default" ] + 70 [ label = "feature winerror" ] + 71 [ label = "feature errhandlingapi" ] + 72 [ label = "feature libloaderapi" ] + 73 [ label = "feature extra_traits" ] + 74 [ label = "feature default" ] + 75 [ label = "feature default" ] + 76 [ label = "feature default" ] + 77 [ label = "feature proc-macro" ] + 78 [ label = "feature default" ] + 79 [ label = "feature default" ] + 80 [ label = "feature unicode-age" ] + 81 [ label = "feature unicode-bool" ] + 82 [ label = "feature unicode-case" ] + 83 [ label = "feature unicode-gencat" ] + 84 [ label = "feature unicode-perl" ] + 85 [ label = "feature unicode-script" ] + 86 [ label = "feature unicode-segment" ] + 87 [ label = "feature default" ] + 88 [ label = "feature proc-macro" ] + 89 [ label = "feature spans" ] + 90 [ label = "feature default" ] + 91 [ label = "feature default" ] + 92 [ label = "feature default" ] + 93 [ label = "feature full" ] + 94 [ label = "feature default" ] + 95 [ label = "feature spans" ] + 96 [ label = "feature visit" ] + 97 [ label = "feature spans" ] + 98 [ label = "feature default" ] + 99 [ label = "feature std" ] + 100 [ label = "feature std" ] + 101 [ label = "feature std" ] + 102 [ label = "feature leftpad" ] + 103 [ label = "feature leftier-strings" ] + 104 [ label = "feature lazy_static" ] + 105 [ label = "feature default" ] + 106 [ label = "feature libloading" ] + 107 [ label = "feature gte_clang_6_0" ] + 108 [ label = "feature gte_clang_5_0" ] + 109 [ label = "feature gte_clang_4_0" ] + 110 [ label = "feature gte_clang_3_9" ] + 111 [ label = "feature gte_clang_3_8" ] + 112 [ label = "feature gte_clang_3_7" ] + 113 [ label = "feature gte_clang_3_6" ] + 114 [ label = "feature open_al" ] + 115 [ label = "feature audio_toolbox" ] + 116 [ label = "feature audio_unit" ] + 117 [ label = "feature core_audio" ] + 118 [ label = "feature core_midi" ] + 119 [ label = "feature std" ] + 120 [ label = "feature alloc" ] + 121 [ label = "feature std" ] + 122 [ label = "feature unicode-segment" ] + 123 [ label = "feature unicode-script" ] + 124 [ label = "feature unicode-perl" ] + 125 [ label = "feature unicode-gencat" ] + 126 [ label = "feature unicode-case" ] + 127 [ label = "feature unicode-bool" ] + 128 [ label = "feature unicode-age" ] + 129 [ label = "feature unicode" ] + 130 [ label = "feature thread_local" ] + 131 [ label = "feature std" ] + 132 [ label = "feature perf-literal" ] + 133 [ label = "feature aho-corasick" ] + 134 [ label = "feature memchr" ] + 135 [ label = "feature perf-inline" ] + 136 [ label = "feature perf-dfa" ] + 137 [ label = "feature perf-cache" ] + 138 [ label = "feature perf" ] + 139 [ label = "feature quote" ] + 140 [ label = "feature proc-macro" ] + 141 [ label = "feature printing" ] + 142 [ label = "feature parsing" ] + 143 [ label = "feature derive" ] + 144 [ label = "feature clone-impls" ] + 145 [ label = "feature std" ] + 146 [ label = "feature std" ] + 147 [ label = "feature spans" ] + 0 -> 23 [ label = "" ] + 0 -> 53 [ label = "" ] + 2 -> 54 [ label = "" ] + 2 -> 9 [ label = "" ] + 2 -> 10 [ label = "" ] + 2 -> 55 [ label = "" ] + 2 -> 56 [ label = "" ] + 2 -> 18 [ label = "" ] + 2 -> 26 [ label = "" ] + 2 -> 27 [ label = "" ] + 2 -> 28 [ label = "" ] + 2 -> 57 [ label = "" ] + 2 -> 31 [ label = "" ] + 2 -> 32 [ label = "" ] + 6 -> 8 [ label = "(build)" ] + 6 -> 58 [ label = " 'x86_64-apple-darwin'" ] + 6 -> 59 [ label = "(dev)" ] + 6 -> 18 [ label = " 'cfg(any(target_os = \"android\", target_os = \"freebsd\", target_os = \"linux\", target_os = \"netbsd\", target_os = \"openbsd\", target_os = \"solaris\"))'" ] + 6 -> 19 [ label = "" ] + 6 -> 20 [ label = " 'cfg(any(target_os = \"android\", target_os = \"linux\"))'" ] + 6 -> 24 [ label = " 'x86_64-unknown-linux-gnu'" ] + 6 -> 34 [ label = " 'cfg(all(any(target_arch = \"aarch64\", target_arch = \"arm\", target_arch = \"x86\", target_arch = \"x86_64\"), not(target_os = \"ios\")))'" ] + 6 -> 60 [ label = " 'cfg(all(target_arch = \"wasm32\", target_vendor = \"unknown\", target_os = \"unknown\", target_env = \"\"))'" ] + 6 -> 61 [ label = " 'cfg(all(target_arch = \"wasm32\", target_vendor = \"unknown\", target_os = \"unknown\", target_env = \"\"))'" ] + 6 -> 49 [ label = " 'cfg(target_os = \"windows\")'" ] + 8 -> 20 [ label = " 'cfg(unix)'" ] + 9 -> 62 [ label = "" ] + 9 -> 63 [ label = "" ] + 11 -> 15 [ label = "" ] + 11 -> 15 [ label = "(build)" ] + 11 -> 20 [ label = "" ] + 11 -> 21 [ label = "" ] + 12 -> 54 [ label = "" ] + 12 -> 13 [ label = "" ] + 12 -> 64 [ label = "" ] + 12 -> 65 [ label = "" ] + 12 -> 66 [ label = "" ] + 12 -> 67 [ label = "" ] + 12 -> 68 [ label = "" ] + 13 -> 2 [ label = "(build)" ] + 16 -> 37 [ label = "" ] + 17 -> 69 [ label = "" ] + 21 -> 7 [ label = "(build)" ] + 21 -> 70 [ label = " 'cfg(windows)'" ] + 21 -> 71 [ label = " 'cfg(windows)'" ] + 21 -> 72 [ label = " 'cfg(windows)'" ] + 22 -> 10 [ label = "" ] + 24 -> 54 [ label = "" ] + 24 -> 7 [ label = "(build) 'cfg(target_os = \"dragonfly\")'" ] + 24 -> 10 [ label = "" ] + 24 -> 73 [ label = "" ] + 24 -> 74 [ label = "" ] + 24 -> 75 [ label = "" ] + 25 -> 23 [ label = "" ] + 25 -> 53 [ label = "" ] + 25 -> 39 [ label = "(build)" ] + 27 -> 76 [ label = "" ] + 28 -> 27 [ label = "" ] + 28 -> 77 [ label = "" ] + 29 -> 78 [ label = "" ] + 29 -> 79 [ label = "" ] + 29 -> 30 [ label = "" ] + 29 -> 80 [ label = "" ] + 29 -> 81 [ label = "" ] + 29 -> 82 [ label = "" ] + 29 -> 83 [ label = "" ] + 29 -> 84 [ label = "" ] + 29 -> 85 [ label = "" ] + 29 -> 86 [ label = "" ] + 29 -> 36 [ label = "" ] + 31 -> 87 [ label = "" ] + 35 -> 27 [ label = "" ] + 35 -> 77 [ label = "" ] + 35 -> 28 [ label = "" ] + 35 -> 88 [ label = "" ] + 35 -> 76 [ label = "" ] + 36 -> 18 [ label = "" ] + 41 -> 10 [ label = "" ] + 41 -> 43 [ label = "" ] + 41 -> 89 [ label = "" ] + 42 -> 90 [ label = "" ] + 42 -> 18 [ label = "" ] + 42 -> 22 [ label = "" ] + 42 -> 91 [ label = "" ] + 42 -> 92 [ label = "" ] + 42 -> 93 [ label = "" ] + 42 -> 94 [ label = "" ] + 42 -> 45 [ label = "" ] + 43 -> 92 [ label = "" ] + 43 -> 44 [ label = "" ] + 43 -> 95 [ label = "" ] + 44 -> 91 [ label = "" ] + 44 -> 92 [ label = "" ] + 44 -> 96 [ label = "" ] + 44 -> 94 [ label = "" ] + 44 -> 42 [ label = "" ] + 44 -> 97 [ label = "" ] + 44 -> 45 [ label = "" ] + 46 -> 98 [ label = "" ] + 46 -> 16 [ label = "" ] + 46 -> 22 [ label = "" ] + 46 -> 91 [ label = "" ] + 46 -> 92 [ label = "" ] + 46 -> 93 [ label = "" ] + 46 -> 94 [ label = "" ] + 46 -> 42 [ label = "" ] + 46 -> 48 [ label = "" ] + 47 -> 98 [ label = "(build)" ] + 47 -> 17 [ label = "" ] + 47 -> 33 [ label = "(build)" ] + 47 -> 69 [ label = "" ] + 47 -> 46 [ label = "(build)" ] + 48 -> 63 [ label = "" ] + 50 -> 51 [ label = " 'i686-pc-windows-gnu'" ] + 50 -> 52 [ label = " 'x86_64-pc-windows-gnu'" ] + 99 -> 0 [ label = "" ] + 99 -> 53 [ label = "" ] + 78 -> 0 [ label = "" ] + 78 -> 99 [ label = "" ] + 100 -> 1 [ label = "" ] + 98 -> 1 [ label = "" ] + 98 -> 100 [ label = "" ] + 54 -> 3 [ label = "" ] + 90 -> 4 [ label = "" ] + 101 -> 5 [ label = "" ] + 87 -> 5 [ label = "" ] + 87 -> 101 [ label = "" ] + 102 -> 6 [ label = "" ] + 102 -> 19 [ label = "" ] + 103 -> 6 [ label = "" ] + 103 -> 102 [ label = "" ] + 104 -> 6 [ label = "" ] + 104 -> 18 [ label = "" ] + 105 -> 6 [ label = "" ] + 105 -> 102 [ label = "" ] + 55 -> 11 [ label = "" ] + 55 -> 106 [ label = "" ] + 106 -> 11 [ label = "" ] + 106 -> 21 [ label = "" ] + 107 -> 11 [ label = "" ] + 108 -> 11 [ label = "" ] + 109 -> 11 [ label = "" ] + 110 -> 11 [ label = "" ] + 111 -> 11 [ label = "" ] + 112 -> 11 [ label = "" ] + 113 -> 11 [ label = "" ] + 56 -> 11 [ label = "" ] + 56 -> 113 [ label = "" ] + 56 -> 112 [ label = "" ] + 56 -> 111 [ label = "" ] + 56 -> 110 [ label = "" ] + 56 -> 109 [ label = "" ] + 56 -> 108 [ label = "" ] + 56 -> 107 [ label = "" ] + 114 -> 12 [ label = "" ] + 114 -> 68 [ label = "" ] + 58 -> 12 [ label = "" ] + 58 -> 115 [ label = "" ] + 58 -> 116 [ label = "" ] + 58 -> 117 [ label = "" ] + 58 -> 114 [ label = "" ] + 58 -> 118 [ label = "" ] + 118 -> 12 [ label = "" ] + 118 -> 67 [ label = "" ] + 117 -> 12 [ label = "" ] + 117 -> 66 [ label = "" ] + 116 -> 12 [ label = "" ] + 116 -> 65 [ label = "" ] + 115 -> 12 [ label = "" ] + 115 -> 64 [ label = "" ] + 68 -> 13 [ label = "" ] + 67 -> 13 [ label = "" ] + 66 -> 13 [ label = "" ] + 65 -> 13 [ label = "" ] + 64 -> 13 [ label = "" ] + 59 -> 14 [ label = "" ] + 119 -> 20 [ label = "" ] + 73 -> 20 [ label = "" ] + 74 -> 20 [ label = "" ] + 74 -> 119 [ label = "" ] + 53 -> 23 [ label = "" ] + 79 -> 23 [ label = "" ] + 79 -> 53 [ label = "" ] + 62 -> 25 [ label = "" ] + 62 -> 120 [ label = "" ] + 121 -> 25 [ label = "" ] + 121 -> 120 [ label = "" ] + 121 -> 53 [ label = "" ] + 63 -> 25 [ label = "" ] + 63 -> 121 [ label = "" ] + 120 -> 25 [ label = "" ] + 77 -> 27 [ label = "" ] + 91 -> 27 [ label = "" ] + 91 -> 77 [ label = "" ] + 88 -> 28 [ label = "" ] + 88 -> 77 [ label = "" ] + 92 -> 28 [ label = "" ] + 92 -> 88 [ label = "" ] + 122 -> 29 [ label = "" ] + 122 -> 86 [ label = "" ] + 123 -> 29 [ label = "" ] + 123 -> 85 [ label = "" ] + 124 -> 29 [ label = "" ] + 124 -> 84 [ label = "" ] + 125 -> 29 [ label = "" ] + 125 -> 83 [ label = "" ] + 126 -> 29 [ label = "" ] + 126 -> 82 [ label = "" ] + 127 -> 29 [ label = "" ] + 127 -> 81 [ label = "" ] + 128 -> 29 [ label = "" ] + 128 -> 80 [ label = "" ] + 129 -> 29 [ label = "" ] + 129 -> 128 [ label = "" ] + 129 -> 127 [ label = "" ] + 129 -> 126 [ label = "" ] + 129 -> 125 [ label = "" ] + 129 -> 124 [ label = "" ] + 129 -> 123 [ label = "" ] + 129 -> 122 [ label = "" ] + 130 -> 29 [ label = "" ] + 130 -> 36 [ label = "" ] + 131 -> 29 [ label = "" ] + 132 -> 29 [ label = "" ] + 132 -> 133 [ label = "" ] + 132 -> 134 [ label = "" ] + 135 -> 29 [ label = "" ] + 136 -> 29 [ label = "" ] + 137 -> 29 [ label = "" ] + 137 -> 130 [ label = "" ] + 138 -> 29 [ label = "" ] + 138 -> 137 [ label = "" ] + 138 -> 136 [ label = "" ] + 138 -> 135 [ label = "" ] + 138 -> 132 [ label = "" ] + 134 -> 29 [ label = "" ] + 134 -> 23 [ label = "" ] + 57 -> 29 [ label = "" ] + 57 -> 131 [ label = "" ] + 57 -> 138 [ label = "" ] + 57 -> 129 [ label = "" ] + 133 -> 29 [ label = "" ] + 133 -> 0 [ label = "" ] + 86 -> 30 [ label = "" ] + 85 -> 30 [ label = "" ] + 84 -> 30 [ label = "" ] + 83 -> 30 [ label = "" ] + 82 -> 30 [ label = "" ] + 81 -> 30 [ label = "" ] + 80 -> 30 [ label = "" ] + 96 -> 35 [ label = "" ] + 139 -> 35 [ label = "" ] + 139 -> 28 [ label = "" ] + 140 -> 35 [ label = "" ] + 140 -> 77 [ label = "" ] + 140 -> 88 [ label = "" ] + 141 -> 35 [ label = "" ] + 141 -> 139 [ label = "" ] + 142 -> 35 [ label = "" ] + 93 -> 35 [ label = "" ] + 143 -> 35 [ label = "" ] + 94 -> 35 [ label = "" ] + 94 -> 143 [ label = "" ] + 94 -> 142 [ label = "" ] + 94 -> 141 [ label = "" ] + 94 -> 144 [ label = "" ] + 94 -> 140 [ label = "" ] + 144 -> 35 [ label = "" ] + 76 -> 38 [ label = "" ] + 145 -> 40 [ label = "" ] + 75 -> 40 [ label = "" ] + 75 -> 145 [ label = "" ] + 146 -> 41 [ label = "" ] + 147 -> 41 [ label = "" ] + 147 -> 89 [ label = "" ] + 69 -> 41 [ label = "" ] + 69 -> 147 [ label = "" ] + 69 -> 146 [ label = "" ] + 97 -> 42 [ label = "" ] + 89 -> 43 [ label = "" ] + 89 -> 95 [ label = "" ] + 95 -> 44 [ label = "" ] + 95 -> 97 [ label = "" ] + 61 -> 47 [ label = "" ] + 60 -> 47 [ label = "" ] + 70 -> 50 [ label = "" ] + 72 -> 50 [ label = "" ] + 71 -> 50 [ label = "" ] +}