Skip to content
Merged
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
249 changes: 196 additions & 53 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

57 changes: 56 additions & 1 deletion crates/socket-patch-cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,38 @@ pub(crate) fn variant_matches_installed(first_file_status: Option<&VerifyStatus>
}
}

/// Of the manifest's records for one gem version (`variants`), the one
/// written for the copy at `pkg_path` itself: the record whose `platform`
/// qualifier names the platform of that (platform gem) copy, when it
/// matches what is installed there (or under `--force`, which asks no
/// file). That record alone is then applied to the copy. `None`: the copy
/// is the `ruby` platform's, no record names its platform, or the one that
/// does is for other bytes; every matching record is applied, as for any
/// other package.
async fn own_platform_variant<'v>(
pkg_path: &Path,
variants: &'v [String],
manifest: &PatchManifest,
force: bool,
) -> Option<&'v String> {
if variants.len() < 2 {
return None;
}
let platform = gem_ext::copy_platform(pkg_path).await?;
let own = variants
.iter()
.find(|variant| gem_ext::names_platform(variant, &platform))?;
if force {
return Some(own);
}
let patch = manifest.patches.get(own)?;
let status = match representative_file(&patch.files) {
Some((name, info)) => Some(verify_file_patch(pkg_path, name, info).await.status),
None => None,
};
variant_matches_installed(status.as_ref()).then_some(own)
}

/// The file whose verify status decides whether a release variant
/// describes the installed distribution (fed to
/// [`variant_matches_installed`]).
Expand Down Expand Up @@ -2917,7 +2949,22 @@ async fn apply_patches_inner(
// that copy is a real on-disk gem some bundler loads.
let mut attempted = false;

// A platform gem's copy takes the record written for its
// own platform, when the manifest has one that matches
// it, and only that one. The ruby-platform record of the
// same version can match such a copy too (its sources are
// what the copy's binary was built from) and must not be
// applied on top.
let only_variant = if Ecosystem::from_purl(purl) == Some(Ecosystem::Gem) {
own_platform_variant(pkg_path, &variants, &manifest, args.force).await
} else {
None
};

for variant_purl in &variants {
if only_variant.is_some_and(|only| only != variant_purl) {
continue;
}
let patch = match manifest.patches.get(variant_purl) {
Some(p) => p,
None => continue,
Expand Down Expand Up @@ -2959,7 +3006,14 @@ async fn apply_patches_inner(
|| (Ecosystem::from_purl(purl) == Some(Ecosystem::Gem)
&& has_build_sibling(pkg_path, copy_paths)))
{
let first_status = match representative_file(&patch.files) {
// A native gem's copy does not hold every build
// input of its source gem (a build removes them; a
// precompiled gem never shipped them): those cannot
// tell distributions apart.
let telling =
gem_ext::discriminating_files(pkg_path, variant_purl, &patch.files)
.await;
let first_status = match representative_file(&telling) {
Some((file_name, file_info)) => Some(
verify_file_patch(pkg_path, file_name, file_info)
.await
Expand Down Expand Up @@ -2987,6 +3041,7 @@ async fn apply_patches_inner(
&gem_ext::RubyEnv {
cwd: args.common.cwd.clone(),
rebuild: !args.common.no_gem_native_rebuild,
offline: args.common.offline,
},
)
.await;
Expand Down
2 changes: 2 additions & 0 deletions crates/socket-patch-cli/src/commands/gem_refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl Policy {
ruby: RubyEnv {
cwd: common.cwd.clone(),
rebuild: !common.no_gem_native_rebuild,
offline: common.offline,
},
}
}
Expand Down Expand Up @@ -778,6 +779,7 @@ mod tests {
ruby: RubyEnv {
cwd: root.to_path_buf(),
rebuild: true,
offline: true,
},
}
}
Expand Down
19 changes: 13 additions & 6 deletions crates/socket-patch-cli/src/commands/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3622,26 +3622,33 @@ pub(crate) async fn rollback_patches_inner(
match try_rollback_local_go(purl, pkg_path, patch, common).await {
Some(r) => r,
None => {
use socket_patch_core::patch::gem_ext;
let is_gem = Ecosystem::from_purl(purl) == Some(Ecosystem::Gem);
// A build input `apply` added to a gem copy that did not
// hold it (a precompiled gem never shipped it; a build
// removed it) is removed, not restored.
let gem_files = if is_gem {
Some(gem_ext::rollback_files(pkg_path, &patch.files).await)
} else {
None
};
let mut result = rollback_package_patch(
common.read_scope(),
purl,
pkg_path,
&patch.files,
gem_files.as_ref().unwrap_or(&patch.files),
&blobs_path,
common.dry_run,
)
.await;
// A gem whose native extension `apply` rebuilt gets its
// original compiled extension back with its sources
// (#1996).
if result.success
&& !common.dry_run
&& Ecosystem::from_purl(purl) == Some(Ecosystem::Gem)
{
use socket_patch_core::patch::gem_ext;
if result.success && !common.dry_run && is_gem {
let env = gem_ext::RubyEnv {
cwd: common.cwd.clone(),
rebuild: !common.no_gem_native_rebuild,
offline: common.offline,
};
if let Err(e) = gem_ext::restore_original_build(pkg_path, &env).await {
result.success = false;
Expand Down
Loading
Loading