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
1 change: 1 addition & 0 deletions crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,7 @@ command's integration tests check its output.
| `vendor_unsupported_ecosystem` | `skipped` | vendor: no vendor backend for this purl's ecosystem (jsr). |
| `already_vendored` | `skipped` | vendor: artifact + wiring already in sync for this patch uuid. |
| `vendor_gem_version_not_locked` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (gem): no section of the project's `Gemfile.lock` resolves the gem at this version (typically a gem Ruby ships in the gem home, or another project's copy in a shared gem home, which the crawl of that gem home returns). Decided before any download: the patch is not fetched, the Gemfile and lock stay byte-identical, and nothing is written. Human: `[skip] <purl> (vendor_gem_version_not_locked): …` in the download phase of `scan` / `get` (both identifier paths) and in their dry run, `Skipping <purl>: …` in `vendor`. The vendored twin of `redirect_gem_version_not_locked`. |
| `vendor_gem_patched_in_other_mode` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (gem): the gem builds a native extension, which vendored mode refuses (`native_extensions_unsupported`), and it already holds the selected patch in a mode that refusal names. Either `Gemfile.lock` still pins that patch hosted, or the manifest records it for agent mode; and every installed copy of the gem that the crawl finds holds the patch (a hosted copy by its files; a copy patched in place as `vex` and `apply --check` judge it, its rebuilt extension included). The reason names the mode and the patch uuid. Nothing is written: the hosted pin, the manifest record and the installed copy stay as they were. Human: `Skipping <purl>: <gem> builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch <uuid>)`. The closing totals of `scan` count the gem as patched. A native gem that holds another patch or none, or with an installed copy that does not hold the patch (a hosted pin whose gem was not reinstalled, an unpatched copy in the tree of another Ruby), is still refused with `native_extensions_unsupported` (exit 1). A dry run of `scan` / `get` previews neither the refusal nor this skip. |
| `vendor_maven_version_not_selected` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (Maven): no module of the build resolves the artifact at the patch's base version; the reason names the versions Maven selects instead (a nearer declaration, the project's own declaration or management, an `<exclusion>`, a version range), or says that no module resolves the artifact. Decided before any download: the patch is not fetched and no file is written, except that a pin an earlier run wrote for that version is taken out of the poms (the reason says so), since it would keep forcing a version the build no longer selects. Human: `[skip] <purl> (vendor_maven_version_not_selected): …` in the download phase of `scan` / `get` and in their dry run, `Skipping <purl>: …` in `vendor`. The vendored twin of `redirect_maven_version_not_selected`. See [Maven version selection](#maven-version-selection-hosted-and-vendored-maven). |
| `vendor_maven_selection_undetermined` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (Maven): what the build selects for the artifact cannot be read (the reason says why: typically a pom the local repository does not hold, because the project was never resolved) and no declaration names the patch's base version, so nothing is pinned on a guess and no file is written. The reason carries the remedy that fits its cause (resolve the project once and re-run when a pom is missing from the local repository; otherwise declare the artifact at the base version). The vendored twin of `redirect_maven_selection_undetermined`. |
| `vendor_maven_pin_would_move_module` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (Maven reactor): the only poms that resolve the artifact at the patch's base version are parent poms above a module that resolves another version. A `<dependencyManagement>` pin in the parent is inherited by that module and would move it, and Maven has no management that reaches a parent and not its children, so nothing is pinned and no file is written; the reason names the parent, the module and its version. When a sibling module does select the base version it is pinned and the same sentence is the `vendor_jvm_degraded` reason `pin_would_move_module`. |
Expand Down
109 changes: 106 additions & 3 deletions crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ use crate::commands::vex::{
generate_vex_from_manifest_path, generate_vex_without_manifest, ManifestlessVex, VexEmbedArgs,
};
use crate::ecosystem_dispatch::{
find_packages_for_rollback_reusing, npm_paths_by_identity, npm_paths_by_identity_in,
partition_purls, NpmCrawlSnapshot,
find_all_packages_for_purls, find_packages_for_rollback_reusing, npm_paths_by_identity,
npm_paths_by_identity_in, partition_purls, NpmCrawlSnapshot,
};
use crate::json_envelope::{
Command, Envelope, EnvelopeError, PatchAction, PatchEvent, RunWarning, Status, VexSummary,
Expand Down Expand Up @@ -207,6 +207,98 @@ async fn keep_older_vendored_patch(
})
}

/// The mode in which the gem `candidate` already holds `record`, the
/// patch the run selected for it, for a gem the vendored backend refused
/// for its native extension: `hosted` when the lock still pins that patch
/// (`live_pin`, the candidate's hosted pin unless this run restored its
/// upstream entry), `agent` when the manifest records it. Either way
/// every installed copy of the gem must hold the patch, as `vex` and
/// `apply --check` judge it: a hosted copy by its files, a copy patched
/// in place by its rebuilt extension as well. `None` when the gem holds
/// another patch or none, or some installed copy does not hold this one
/// (a hosted pin whose gem was never reinstalled, say): the refusal then
/// stands, and the remedy it names puts the patch in place.
async fn gem_patch_held_in(
common: &GlobalArgs,
candidate: &str,
record: &PatchRecord,
live_pin: Option<&HostedPin>,
) -> Option<&'static str> {
let hosted = live_pin.is_some_and(|pin| pin.uuid == record.uuid);
let in_place = match hosted {
true => None,
false => read_manifest(&common.resolved_manifest_path())
.await
.ok()
.flatten()?
.patches
.into_iter()
.find(|(purl, r)| r.uuid == record.uuid && PurlKey::same(purl, candidate)),
};
if !hosted && in_place.is_none() {
return None;
}
// Every installed copy, not the one the loop vendors from.
let purls = [candidate.to_string()];
let copies: Vec<std::path::PathBuf> = find_all_packages_for_purls(
&partition_purls(&purls, None),
&common.crawler_options(),
true,
)
.await
.into_iter()
.filter(|(purl, _)| PurlKey::same(purl, candidate))
.flat_map(|(_, paths)| paths)
.collect();
if copies.is_empty() {
return None;
}
match in_place {
Some((purl, recorded)) => {
socket_patch_core::vex::verify::gem_copies_patched_in_place(&purl, &copies, &recorded)
.await
.then_some("agent")
}
None => {
for copy in &copies {
socket_patch_core::vex::verify::verify_patch_record(copy, record)
.await
.ok()?;
}
Some("hosted")
}
}
}

/// A backend refusal as the run reports it: a gem refused for its native
/// extension that already holds the selected patch in hosted or agent
/// mode is the skip [`vendor::gem::GEM_PATCHED_IN_OTHER_MODE`] (#2269);
/// every other refusal is itself.
async fn settle_gem_refusal(
common: &GlobalArgs,
candidate: &str,
record: &PatchRecord,
live_pin: Option<&HostedPin>,
code: &'static str,
detail: String,
) -> (&'static str, String) {
if code != vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED {
return (code, detail);
}
match gem_patch_held_in(common, candidate, record, live_pin).await {
Some(mode) => {
let purl = normalize_purl(candidate);
let name = socket_patch_core::utils::purl::purl_name_version(&purl)
.map_or(&*purl, |(name, _)| name);
(
vendor::gem::GEM_PATCHED_IN_OTHER_MODE,
vendor::gem::patched_in_other_mode_detail(name, mode, &record.uuid),
)
}
None => (code, detail),
}
}

/// The `vendor_dir_symlink_unsupported` detail when `purl`'s vendor dir
/// (`.socket/vendor`, its ecosystem dir, or the `uuid` unit) is a link.
fn linked_vendor_dir_refusal(project_root: &Path, purl: &str, uuid: &str) -> Option<String> {
Expand Down Expand Up @@ -3922,6 +4014,9 @@ pub(crate) async fn vendor_records_reusing(
// does not vendor the purl, and the advisories that only hold
// once it does (see `TakeoverUndo`).
let mut takeover_undo: Option<TakeoverUndo> = None;
// The candidate's hosted pin while the lock still holds it:
// gone once this run's restore of its upstream entry stands.
let mut live_pin = hosted_pin_of(candidate);
if let Some(pin) = hosted_pin_of(candidate) {
let origins = crate::commands::hosted_unwind::patch_server_origins(common);
let restore_opts = socket_patch_core::patch::redirect::upstream::RestoreOptions {
Expand Down Expand Up @@ -4179,6 +4274,7 @@ pub(crate) async fn vendor_records_reusing(
takeover_undo = Some(undo);
} else {
undo.settle(env, common, candidate, &mut vlt_takeover_targets);
live_pin = None;
Comment thread
mikolalysenko marked this conversation as resolved.
Resolved
}
}
}
Expand Down Expand Up @@ -4279,6 +4375,7 @@ pub(crate) async fn vendor_records_reusing(
matches!(&outcome, Some(VendorOutcome::Done { entry, .. }) if entry.is_some());
if vendored || recorded || undo.savepoint.is_none() || group.is_none() {
undo.settle(env, common, candidate, &mut vlt_takeover_targets);
live_pin = None;
} else {
undo.abandon(group.as_ref());
}
Expand All @@ -4296,6 +4393,8 @@ pub(crate) async fn vendor_records_reusing(
);
}
Some(VendorOutcome::Refused { code, detail }) => {
let (code, detail) =
settle_gem_refusal(common, candidate, record, live_pin, code, detail).await;
if refusal_is_benign(code) {
// An expected skip, not an error: informational.
if !common.silent && !common.json {
Expand Down Expand Up @@ -4767,7 +4866,11 @@ fn print_vendor_closing(
.iter()
.filter(|e| {
e.action == PatchAction::Applied
|| e.error_code.as_deref() == Some("already_vendored")
|| matches!(
e.error_code.as_deref(),
// In place before the run, here or in another mode.
Some("already_vendored" | vendor::gem::GEM_PATCHED_IN_OTHER_MODE)
)
})
.filter_map(|e| e.purl.as_deref())
.collect();
Expand Down
Loading
Loading