Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions crates/socket-patch-cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3519,11 +3519,10 @@ impl OtherProjectCopy {
}
}

/// The gems of `unresolved` (the manifest keys with no copy to patch and
/// none in the lock inventory) that are another project's: a Bundler lock
/// was read, it does not name them as platform builds either, and
/// `rollback`'s resolver finds
/// a copy of them where this run's (`all_packages`, the copies `apply`
/// The gems of `unresolved` (the manifest keys with no copy to patch that
/// the project's locks do not resolve, [`lockfile_resolved`]) that are
/// another project's: a Bundler lock was read, and `rollback`'s resolver
/// finds a copy of them where this run's (`all_packages`, the copies `apply`
/// writes) found none, so each sits in a `gem env` home Bundler does not
/// load the project's gems from. The detail holds for a copy an earlier
/// release patched and for one nothing ever touched.
Expand All @@ -3535,7 +3534,7 @@ async fn other_project_gem_copies(
if common.is_global() {
return Vec::new();
}
let mut candidates: Vec<String> = unresolved
let candidates: Vec<String> = unresolved
.iter()
.filter(|purl| {
Ecosystem::from_purl(purl) == Some(Ecosystem::Gem)
Expand All @@ -3554,19 +3553,6 @@ async fn other_project_gem_copies(
if !ctx.gem_lock_read().await {
return Vec::new();
}
// The lock names a gem it holds only as precompiled platform builds,
// though the lock inventory behind `unresolved` leaves it out: that
// gem is the project's, and stays not installed.
let platform_only: HashSet<PurlKey> = ctx
.gem_platform_only_purls()
.await
.iter()
.map(|purl| PurlKey::new(purl))
.collect();
candidates.retain(|purl| !platform_only.contains(&PurlKey::new(purl)));
if candidates.is_empty() {
return Vec::new();
}
let mut found = crate::ecosystem_dispatch::find_all_packages_for_rollback(
&partition_purls(&candidates, None),
&common.crawler_options(),
Expand Down Expand Up @@ -5442,8 +5428,7 @@ async fn lockfile_resolved(common: &GlobalArgs, unmatched: &[String]) -> HashSet
return HashSet::new();
}
let ctx = crate::commands::context::ProjectContext::new(common);
let entries = &ctx.locks().await.entries;
let lock_purls: HashSet<PurlKey> = entries.iter().map(|e| PurlKey::new(&e.purl)).collect();
let lock_purls = ctx.lock_resolved().await;
unmatched
.iter()
.filter(|p| lock_resolution_is_calm(p) && lock_purls.contains(&PurlKey::new(p)))
Expand Down
19 changes: 19 additions & 0 deletions crates/socket-patch-cli/src/commands/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
//! under that lock instead of trusting a pre-lock snapshot, and an embedded
//! `--vex` after the writes loads its own inputs.

use std::collections::HashSet;
use std::path::PathBuf;

use socket_patch_core::ledgers::{Ledgers, LoadedLedgers};
use socket_patch_core::utils::purl_key::PurlKey;
use socket_patch_core::vendor::lock_inventory::{
DiskSnapshot, LockfileEntry, ProjectView, ReadSet, UnsupportedNpmLayout,
};
Expand Down Expand Up @@ -99,6 +101,23 @@ impl<'a> ProjectContext<'a> {
.await
}

/// The releases the project's locks resolve: every entry of
/// [`Self::locks`], and the gems of
/// [`Self::gem_platform_only_purls`], which that inventory leaves out
/// though the lock names them. What is in here and not installed was
/// left out by the package manager on this host, not lost.
pub(crate) async fn lock_resolved(&self) -> HashSet<PurlKey> {
let locks = self.locks().await;
let platform_gems = self.gem_platform_only_purls().await;
locks
.entries
.iter()
.map(|entry| entry.purl.as_str())
.chain(platform_gems.iter().map(String::as_str))
.map(PurlKey::new)
.collect()
}

/// Whether bundler loads a lock in the project that socket-patch read
/// (`gem_lock_read_in`), through the same snapshot as [`Self::locks`].
pub(crate) async fn gem_lock_read(&self) -> bool {
Expand Down
8 changes: 2 additions & 6 deletions crates/socket-patch-cli/src/commands/scan/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,8 @@ async fn keep_project_entries(common: &GlobalArgs, prunable: &mut Vec<String>) {
crate::ecosystem_dispatch::crawl_ecosystems_for_name(&project.crawler_options(), None)
.await;
let ctx = crate::commands::context::ProjectContext::new(&project);
let locked = &ctx.locks().await.entries;
let accounted: HashSet<PurlKey> = installed
.iter()
.map(|pkg| PurlKey::new(&pkg.purl))
.chain(locked.iter().map(|entry| PurlKey::new(&entry.purl)))
.collect();
let mut accounted = ctx.lock_resolved().await;
accounted.extend(installed.iter().map(|pkg| PurlKey::new(&pkg.purl)));
prunable.retain(|purl| !accounted.contains(&PurlKey::new(purl)));
}

Expand Down
17 changes: 16 additions & 1 deletion crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3002,7 +3002,7 @@ fn omission_lines(failed: &[FailedPatch], verbose: bool) -> Vec<String> {
"Warning: omitting {} packages from VEX: {} ({reason}): {}{}",
purls.len(),
omission_phrase(reason),
crate::ui::folded(&crate::ui::package_names(&purls), NAMES_SHOWN),
crate::ui::folded(&crate::ui::build_names(&purls), NAMES_SHOWN),
if purls.len() > NAMES_SHOWN {
" (--verbose lists them all)"
} else {
Expand Down Expand Up @@ -3397,6 +3397,21 @@ mod tests {
(package_not_found)",
]
);
// The platform builds of one gem are told apart (#2452).
let builds = ["arm64-darwin", "ruby", "x86_64-linux-musl", "x86_64-darwin"].map(|p| {
failed(
&format!("pkg:gem/nokogiri@1.19.7?platform={p}"),
"package_not_found",
)
});
assert_eq!(
omission_lines(&builds, false),
[
"Warning: omitting 4 packages from VEX: the package is not installed \
(package_not_found): nokogiri@1.19.7 (arm64-darwin), nokogiri@1.19.7 (ruby), \
nokogiri@1.19.7 (x86_64-linux-musl) and 1 more (--verbose lists them all)"
]
);
let verbose = omission_lines(&patches, true);
assert_eq!(verbose.len(), 85);
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions crates/socket-patch-cli/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub(crate) use prompt::confirm;
pub use prompt::{select_from, select_one, SelectError};
pub(crate) use status::StatusLine;
pub(crate) use text::{
agent_next_steps, folded, manifest_error_message, next_steps, next_steps_installed,
package_names, plural, printable, sentence_case, shell_word, short_uuid, sweep_failure,
truncate, worst_and_ids,
agent_next_steps, build_names, folded, manifest_error_message, next_steps,
next_steps_installed, package_names, plural, printable, sentence_case, shell_word, short_uuid,
sweep_failure, truncate, worst_and_ids,
};

/// [`text::no_patches_message`] for this run: the manifest path is named
Expand Down
59 changes: 59 additions & 0 deletions crates/socket-patch-cli/src/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ pub fn package_names(purls: &[&str]) -> Vec<String> {
.collect()
}

/// [`package_names`] for a listing with one entry per patch record, where
/// the platform builds of one gem release are entries of their own: a name
/// that would repeat carries its build, `nokogiri@1.19.7 (arm64-darwin)`.
pub fn build_names(purls: &[&str]) -> Vec<String> {
use socket_patch_core::crawlers::types::Ecosystem;
use socket_patch_core::utils::purl::purl_qualifier;
let names = package_names(purls);
let mut listed: std::collections::HashMap<&str, usize> = std::collections::HashMap::new();
for name in &names {
*listed.entry(name).or_default() += 1;
}
let repeats = |name: &str| listed.get(name).is_some_and(|count| *count > 1);
let is_gem = |purl: &str| Ecosystem::from_purl(purl) == Some(Ecosystem::Gem);
names
.iter()
.zip(purls)
.map(|(name, purl)| {
let build = purl_qualifier(purl, "platform")
.filter(|platform| is_gem(purl) && !platform.is_empty());
match build {
Some(platform) if repeats(name) => format!("{name} ({platform})"),
_ => name.clone(),
}
})
.collect()
}

/// `items` for a sentence, the first `shown` of them named: `a, b, c and
/// 5 more`.
pub fn folded(items: &[String], shown: usize) -> String {
Expand Down Expand Up @@ -362,6 +389,38 @@ mod tests {
assert!(package_names(&[]).is_empty());
}

/// The builds of one gem release are told apart; a name that is
/// listed once stays as it is.
#[test]
fn builds_of_one_release_are_named_with_their_platform() {
assert_eq!(
build_names(&[
"pkg:gem/nokogiri@1.19.7?platform=arm64-darwin",
"pkg:gem/nokogiri@1.19.7?platform=ruby",
"pkg:gem/rack@3.2.9?platform=ruby",
]),
[
"nokogiri@1.19.7 (arm64-darwin)",
"nokogiri@1.19.7 (ruby)",
"rack@3.2.9"
]
);
// Named by purl beside another ecosystem, and still told apart.
assert_eq!(
build_names(&[
"pkg:gem/ffi@1.17.0?platform=x86_64-linux-gnu",
"pkg:gem/ffi@1.17.0",
"pkg:npm/b@2.0.0",
]),
[
"pkg:gem/ffi@1.17.0 (x86_64-linux-gnu)",
"pkg:gem/ffi@1.17.0",
"pkg:npm/b@2.0.0"
]
);
assert_eq!(build_names(&["pkg:npm/a@1.0.0"]), ["a@1.0.0"]);
}

/// One rule for the severity and the ids of a set of advisories,
/// wherever a patch is named in a line: the `list` row, `show`'s
/// title and the patch menu.
Expand Down
Loading
Loading