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
4 changes: 2 additions & 2 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

170 changes: 170 additions & 0 deletions crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ enum Driver {
/// [`Driver::ScanVexSourceBlock`] whose `source … do` follows another
/// statement and a `;` on its line (#1531).
ScanVexSourceBlockAfterSemicolon,
/// [`Driver::ScanVex`] on a Gemfile with a second top-level `source`
/// line (#2162). Bundler 2.2 to 2.7 lock such a Gemfile into ONE `GEM`
/// section naming both remotes, which `bundle lock` writes again as it
/// is: the run refuses the gem, names the Gemfile's sources as the
/// cause, and the remedy it prescribes (the second source in a block,
/// `bundle lock`) makes the next scan wire the gem.
ScanVexTwoGlobalSources,
/// A heredoc option continues beyond the declaration's physical line.
ScanVexHeredocDeclaration,
/// A double-quoted interpolation can itself contain a heredoc opener.
Expand Down Expand Up @@ -605,6 +612,9 @@ impl Driver {
Driver::ScanVexTwin => "scan --mode hosted (Gemfile + gems.rb twin)",
Driver::ScanVexGroupBlock => "scan --mode hosted (gem in a group block)",
Driver::ScanVexSourceBlock => "scan --mode hosted (gem in a source block)",
Driver::ScanVexTwoGlobalSources => {
"scan --mode hosted (Gemfile with two top-level sources)"
}
Driver::ScanVexSourceBlockAssignment => {
"scan --mode hosted (gem in a source block after `v = if … end`)"
}
Expand Down Expand Up @@ -740,6 +750,11 @@ async fn redirect_scanned_project(
{
Some("custom lockfiles need bundler >= 4")
}
// Before 2.2 the lock is the merged layout anyway; bundler 4
// refuses the Gemfile.
Driver::ScanVexTwoGlobalSources if !bundler.at_least(2, 2) || bundler.at_least(4, 0) => {
Some("a Gemfile with two top-level sources is locked into one multi-remote GEM section by bundler 2.2 to 2.7 only")
}
_ => None,
};
if let Some(why) = only {
Expand Down Expand Up @@ -919,6 +934,30 @@ async fn redirect_scanned_project(
"source \"{up}/upstream\"\n\nsource \"{up}/upstream\" do\n gem \"{DEP}\", \"{DEP_VERSION}\"\nend\n",
up = server.uri()
),
Driver::ScanVexTwoGlobalSources => {
// A second gem server with a gem of its own, as a private
// server beside rubygems.org has.
let gem = build_gem(
&stage,
SECOND_SOURCE_GEM,
"1.0.0",
"second_only.rb",
TINY_LIB,
&[],
);
let second = IndexGem {
name: SECOND_SOURCE_GEM,
version: "1.0.0",
deps: vec![],
gem,
};
mount_compact_index(&server, "/second", &[second]).await;
format!(
"source \"{up}/upstream\"\nsource \"{up}/second\"\n\ngem \"{DEP}\"\n\
gem \"{SECOND_SOURCE_GEM}\"\n",
up = server.uri()
)
}
Driver::ScanVexSourceBlockAssignment => format!(
"source \"{up}/upstream\"\n\nsource \"{up}/upstream\" do\n v = if ENV[\"ZZ\"]\n \"a\"\n \
else\n \"b\"\n end\n w = case ENV[\"ZZ\"]\n when \"x\" then \"a\"\n \
Expand Down Expand Up @@ -1211,6 +1250,7 @@ async fn redirect_scanned_project(
| Driver::ScanVexEvalGemfile
| Driver::ScanVexGroupBlock
| Driver::ScanVexSourceBlock
| Driver::ScanVexTwoGlobalSources
| Driver::ScanVexSourceBlockAssignment
| Driver::ScanVexSourceBlockMultiLineOpener
| Driver::ScanVexSourceBlockBraces
Expand Down Expand Up @@ -1329,6 +1369,7 @@ async fn redirect_scanned_project(
| Driver::ScanVexSourceBlockMultiLineOpener
| Driver::ScanVexSourceBlockBraces
| Driver::ScanVexSourceBlockAfterSemicolon => Some("redirect_gem_source_option"),
Driver::ScanVexTwoGlobalSources => Some("redirect_gem_lock_unconvergeable"),
Driver::ScanVexHeredocDeclaration | Driver::ScanVexInterpolatedHeredocDeclaration => {
Some("redirect_gem_unrecognized_declaration")
}
Expand All @@ -1348,6 +1389,9 @@ async fn redirect_scanned_project(
&pristine_gemfile,
&pristine_lock,
);
if driver == Driver::ScanVexTwoGlobalSources {
assert_two_global_sources_remedy_wires(&proj, &api, &index_url, &lock_before, &stdout);
}
return None;
}
assert_eq!(
Expand Down Expand Up @@ -1449,6 +1493,7 @@ async fn redirect_scanned_project(
| Driver::ScanVexMirrorAllEnv
| Driver::ScanVexCustomGitSource
| Driver::ScanVexSourceBlock
| Driver::ScanVexTwoGlobalSources
| Driver::ScanVexSourceBlockAssignment
| Driver::ScanVexSourceBlockMultiLineOpener
| Driver::ScanVexSourceBlockBraces
Expand Down Expand Up @@ -1593,6 +1638,108 @@ fn assert_unwirable_declaration_redirects_nothing(
);
}

/// The gem only the second source of [`Driver::ScanVexTwoGlobalSources`]
/// serves.
const SECOND_SOURCE_GEM: &str = "second_only";

/// #2162, after the refused scan of [`Driver::ScanVexTwoGlobalSources`]:
/// the fixture lock is the one multi-remote `GEM` section the report is
/// about and `bundle lock` leaves it alone; the warning names the Gemfile's
/// top-level sources and the block remedy, never an old bundler or
/// vendored mode; and that remedy works: with the second source in a
/// block, `bundle lock` gives each source a section, the next scan wires
/// the gem, and bundler installs the pair frozen.
fn assert_two_global_sources_remedy_wires(
proj: &Path,
api: &str,
index_url: &str,
lock_before: &str,
refused_stdout: &str,
) {
assert_eq!(gem_remotes(lock_before).len(), 2, "{lock_before}");
assert_eq!(
lock_before.matches("GEM\n").count(),
1,
"one GEM section names both remotes:\n{lock_before}"
);
let relock = bundle(proj, &["lock"]);
assert!(relock.status.success(), "bundle lock failed");
assert_eq!(
std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap(),
lock_before,
"`bundle lock` leaves the two-remote lock as it is"
);
for said in [
"Gemfile has 2 top-level `source` lines",
"do … end` block",
"--mode agent",
] {
assert!(
refused_stdout.contains(said),
"the warning says {said:?}:\n{refused_stdout}"
);
}
for unsaid in ["bundler < 2.2", "--mode vendored"] {
assert!(
!refused_stdout.contains(unsaid),
"the warning must not say {unsaid:?}:\n{refused_stdout}"
);
}

// The remedy the warning names.
let gemfile = std::fs::read_to_string(proj.join("Gemfile")).unwrap();
let second = gemfile
.lines()
.nth(1)
.filter(|line| line.starts_with("source \"") && line.ends_with("/second\""))
.unwrap_or_else(|| panic!("the second top-level source: {gemfile}"));
let own_gem = format!("gem \"{SECOND_SOURCE_GEM}\"\n");
assert!(gemfile.ends_with(&own_gem), "{gemfile}");
let remedied = format!(
"{}\n{second} do\n {own_gem}end\n",
gemfile
.replacen(&format!("{second}\n"), "", 1)
.replacen(&own_gem, "", 1)
);
std::fs::write(proj.join("Gemfile"), &remedied).unwrap();
let relock = bundle(proj, &["lock"]);
assert!(
relock.status.success(),
"bundle lock over the remedied Gemfile failed:\n{}",
String::from_utf8_lossy(&relock.stderr)
);
// The install the helper above ran is stale for the scan to come.
std::fs::remove_dir_all(proj.join("vendor")).expect("remove the materialization");
let (code, stdout, stderr) = run_hosted_scan(proj, api);
assert_eq!(code, 0, "stdout:\n{stdout}\nstderr:\n{stderr}");
assert!(
!stdout.contains("redirect_gem_lock_unconvergeable"),
"the remedied project is wired:\n{stdout}"
);
let wired = std::fs::read_to_string(proj.join("Gemfile")).unwrap();
assert_eq!(
wired,
with_source_option(&remedied, &format!("gem \"{DEP}\""), index_url),
"the declaration gains the `source:` option"
);
let lock = std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap();
assert!(
gem_remotes(&lock).contains(&index_url),
"the lock records the patch source:\n{lock}"
);
let install = bundle_env(proj, &["install"], &[("BUNDLE_FROZEN", "true")]);
assert!(
install.status.success(),
"a frozen install must accept the wired pair:\n{}",
String::from_utf8_lossy(&install.stderr)
);
assert_eq!(
std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap(),
lock,
"the frozen install leaves the lock alone"
);
}

/// The contract of a hosted scan that must refuse every gem: #749's
/// `custom.lock` named in `.bundle/config` (bundler 4), or #751's
/// `Gemfile` + `gems.rb` twin. The scan reports `refusal`, redirects and
Expand Down Expand Up @@ -3733,6 +3880,29 @@ async fn gem_hosted_gem_in_a_user_source_block_is_refused_and_still_installs() {
}
}

/// #2162: a Gemfile with two top-level `source` lines, which bundler 2.2 to
/// 2.7 lock into one `GEM` section with two remotes. The scan refuses the
/// gem and leaves the project installable, as for any lock it cannot
/// follow; the warning names the two top-level sources and the remedy
/// (the second source in a block, `bundle lock`), and after that remedy
/// the same scan wires the gem and bundler installs it frozen. See
/// [`assert_two_global_sources_remedy_wires`].
#[tokio::test(flavor = "multi_thread")]
#[ignore = "host capstone: shells out to a real ruby/gem/bundler (2.2 to 2.7 for this arm); \
the unpinned `test` job skips it, an e2e job with a pinned toolchain runs it via --ignored"]
async fn gem_hosted_two_top_level_sources_are_refused_with_the_block_remedy() {
let fx = redirect_scanned_project(
"two-global-sources",
Spelling::Gemfile,
false,
true,
None,
Driver::ScanVexTwoGlobalSources,
)
.await;
assert!(fx.is_none(), "the driver asserts in place");
}

/// A pin socket-patch 5.0 / 5.1 wrote: the declaration replaced by a
/// `source "<index-url>" do … end` block declaring the exact version, the
/// lock (one without CHECKSUMS) left on the registry — the pair a frozen
Expand Down
93 changes: 92 additions & 1 deletion crates/socket-patch-core/src/formats/gem/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

use super::gemfile::NON_SOURCE_KEYS;
use super::is_plain_gem_token;
use super::ruby::{is_ident_byte, masked_source, ruby_words, source_call_args};
use super::ruby::{block_stack, is_ident_byte, masked_source, ruby_words, source_call_args};
use crate::utils::line_endings;

/// The trailing comment of a line the hosted rewriter ADDED for a gem the
Expand Down Expand Up @@ -717,6 +717,64 @@ pub(crate) fn rubygems_sources(text: &str) -> Vec<String> {
out
}

/// How many top-level sources a manifest declares: its `source <url>`
/// calls that take no block and sit outside every other construct, a
/// literal url written twice counted once. Bundler 2.2 to 2.7 lock a
/// manifest with more than one of them into ONE `GEM` section that names
/// them all (and deprecate it; bundler 4 refuses the manifest).
///
/// A url that is computed counts too, since only the number matters. A
/// call inside an `if`, a `group` or any other construct does not (bundler
/// may never run it), nor does one whose arguments run past its line: the
/// count is a floor, for a caller that words a diagnosis with it.
pub(crate) fn top_level_source_count(text: &str) -> usize {
let masked = masked_source(text);
let mut literal: Vec<&str> = Vec::new();
let mut computed = 0;
for (word, start, end) in ruby_words(&masked) {
if word != "source" {
continue;
}
let line_start = masked[..start].rfind('\n').map_or(0, |i| i + 1);
let lead = masked[line_start..start].trim();
let Some(paren) = source_call_args(&masked[end..]) else {
continue;
};
if !(lead.is_empty() || lead.ends_with(';')) {
continue;
}
let line_end = masked[end..].find('\n').map_or(masked.len(), |i| end + i);
let call = masked[end..line_end].trim_end();
// A block (`do`, `do |x|`, `{`), a trailing modifier (`… if x`),
// or arguments that go on: not known to be a top-level source.
let qualified = call.contains('{')
|| ruby_words(call).any(|(word, _, _)| {
matches!(word, "do" | "if" | "unless" | "while" | "until" | "rescue")
})
|| (paren && !call.contains(')'))
|| call.ends_with([',', '\\']);
if qualified || !block_stack(text, line_start).is_empty() {
continue;
}
// Past the `(` or the blank that follows the word.
let args = &masked[end + 1..line_end];
let arg_at = end + 1 + (args.len() - args.trim_start().len());
let url = masked[arg_at..line_end]
.chars()
.next()
.filter(|q| matches!(q, '"' | '\''))
.and_then(|q| masked[arg_at + 1..line_end].find(q))
.map(|len| &text[arg_at + 1..arg_at + 1 + len])
.filter(|url| !url.contains('\\') && !url.contains("#{"));
match url {
Some(url) if literal.iter().any(|seen| super::same_remote(seen, url)) => {}
Some(url) => literal.push(url),
None => computed += 1,
}
}
literal.len() + computed
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1028,6 +1086,39 @@ mod tests {
assert!(!is_added_line(&text, &one(&text)));
}

/// Only a block-less `source` call outside every construct is a
/// top-level source; a literal url written twice (a trailing slash
/// apart) counts once, a computed one counts.
#[test]
fn top_level_source_count_is_the_blockless_calls_outside_any_construct() {
let two = "source \"https://rubygems.org\"\r\n\
source('https://second.example/')\r\n\
source \"https://rubygems.org/\"\r\n\
# source \"https://commented.example\"\r\n\
source \"https://block.example\" do\r\n gem \"c\"\r\nend\r\n\
source(\"https://brace.example\") { gem \"d\" }\r\n\
gem \"a\", source: \"https://a.example/\"\r\n";
assert_eq!(top_level_source_count(two), 2);
// A computed url is a source all the same.
for computed in [
"source \"https://rubygems.org\"\nsource ENV[\"GEM_SOURCE\"]\n",
"source \"https://rubygems.org\"\nsource \"https://#{ENV['TOKEN']}@gems.acme.example/\"\n",
"source 'https://rubygems.org'; source PRIVATE_SOURCE\n",
] {
assert_eq!(top_level_source_count(computed), 2, "{computed}");
}
// Bundler runs one branch of a conditional, and maybe none.
for one in [
"if ENV['MIRROR']\n source \"https://mirror.example\"\nelse\n source \"https://rubygems.org\"\nend\n",
"source \"https://rubygems.org\"\nsource \"https://mirror.example\" if ENV['MIRROR']\n",
"source \"https://rubygems.org\"\nsource(\n \"https://block.example\"\n) do\n gem \"c\"\nend\n",
"source \"https://rubygems.org\"\nsource = \"https://not-a-call.example\"\n",
] {
assert!(top_level_source_count(one) < 2, "{one}");
}
assert_eq!(top_level_source_count("gem \"a\"\n"), 0);
}

/// Bundler < 2.2 lists the global sources first, then each block or
/// option source in the order it last meets it.
#[test]
Expand Down
Loading
Loading