From 60cf0b6a2012291ccaf614f2ae6fdfe3aa91c33b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 17:22:57 +0000 Subject: [PATCH 1/4] Start: read Ruby's gem dirs, not the gemrc's Refs #2317. Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU From fc201bc17c0d12581b759a72b621b51c2f158001 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 17:30:13 +0000 Subject: [PATCH 2/4] Test the gem dirs read under a gemrc gemhome The gem command applies the gemrc keys gemhome and gempath to its own directories. Ruby and Bundler never read them, so under such a gemrc `gem env` names a directory the project loads nothing from. Three tests that fail on main: - core, crawler_ruby_e2e: with a gem that answers as the real one does under a gemrc, the project and the global paths are Ruby's Gem.dir and Gem.path. - CLI, apply suite: apply and apply --check act on the copy Ruby loads, and leave a copy in the gemrc's directory alone. - CLI, e2e_gem_native_ext_build: the same with the real gem and ruby, a GEMRC file and GEM_HOME. Refs #2317. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU Assisted-by: Claude Code:claude-fable-5-2 --- .../apply/in_process_gem_fallback_home.rs | 105 ++++++++++++++ .../tests/e2e_gem_native_ext_build.rs | 130 ++++++++++++++++++ .../tests/crawler_ruby_e2e.rs | 112 +++++++++++++++ 3 files changed, 347 insertions(+) diff --git a/crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs b/crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs index bf5739974a..4f19798f2d 100644 --- a/crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs +++ b/crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs @@ -1143,3 +1143,108 @@ fn mismatched_home_copy_warns_on_human_stderr() { ); assert_eq!(std::fs::read(&fx.home_file).unwrap(), foreign); } + +// ── A gemrc that moves the `gem` command's directories (#2317) ────────── + +/// A `gem` that answers as the real command does under a gemrc that sets +/// `gemhome`: `env gemdir` and `env gempath` name `gemrc_home`, and only +/// with `--norc` the home Ruby itself loads from, `ruby_home`. +fn install_fake_gem_with_gemrc(bin_dir: &Path, gemrc_home: &Path, ruby_home: &Path) { + use std::os::unix::fs::PermissionsExt; + let script = format!( + "#!/bin/sh\n\ + [ \"$1\" = env ] || exit 1\n\ + case \"$2\" in gemdir|gempath) ;; *) exit 1 ;; esac\n\ + for arg in \"$@\"; do\n\ + if [ \"$arg\" = --norc ]; then printf '%s\\n' \"{ruby}\"; exit 0; fi\n\ + done\n\ + printf '%s\\n' \"{gemrc}\"\n", + ruby = ruby_home.display(), + gemrc = gemrc_home.display(), + ); + let bin = bin_dir.join("gem"); + std::fs::write(&bin, script).unwrap(); + std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap(); +} + +/// The system-gems fixture under such a gemrc: `gem-home` is the home +/// Ruby loads the project's gem from. Returns the copy in the gemrc's +/// directory, staged when `gemrc_copy` is given. +fn under_a_gemrc(fx: &Fixture, gemrc_copy: Option<&[u8]>) -> PathBuf { + let gemrc_home = fx.project.path().join("gemrc-home"); + std::fs::create_dir_all(gemrc_home.join("gems")).unwrap(); + install_fake_gem_with_gemrc(&fx.bin_dir, &gemrc_home, &home_dir(fx)); + match gemrc_copy { + Some(bytes) => stage_copy(&gemrc_home, bytes), + None => gemrc_home.join("gems/rack-3.1.0/lib/rack.rb"), + } +} + +/// `gemhome` in a gemrc, and the gem installed where Bundler put it +/// (Ruby's own gem home): the gem is installed, so `apply` patches it and +/// the check judges it. On `main` both exited 0 with the patch "not +/// installed, skipped", and the copy Ruby loads stayed unpatched. +#[test] +fn a_gemrc_gemhome_does_not_hide_the_copy_ruby_loads() { + let fx = build_fixture(Store::SystemGems, Home::Installed, ORIGINAL, BASE_PURL); + write_lock(&fx); + let gemrc_file = under_a_gemrc(&fx, None); + + let (code, stdout, stderr) = run_command(&fx, &["apply", "--check"], &["--json"]); + let env = parse_env(&stdout); + assert_eq!( + code, 1, + "the unpatched copy Ruby loads is drift.\nenvelope: {env}\nstderr:\n{stderr}" + ); + + let (code, stdout, stderr) = run_apply(&fx, &["--json"]); + let env = parse_env(&stdout); + assert_eq!(code, 0, "envelope: {env}\nstderr:\n{stderr}"); + assert_eq!(env["summary"]["applied"], 1, "envelope: {env}"); + assert_eq!( + std::fs::read(&fx.home_file).unwrap(), + patched_bytes(), + "the copy Ruby loads must be patched" + ); + assert!(!gemrc_file.exists(), "nothing is written for the gemrc"); + + let (code, stdout, stderr) = run_command(&fx, &["apply", "--check"], &["--json"]); + assert_eq!(code, 0, "stdout:\n{stdout}\nstderr:\n{stderr}"); +} + +/// The gemrc's directory holds the same gem version (it is where +/// `gem install` puts gems on that machine). The project loads nothing +/// from it: the copy Ruby loads is the one patched and checked, and the +/// gemrc's copy is not written. On `main` the gemrc's copy was patched +/// and attested while the application kept loading unpatched code. +#[test] +fn a_copy_in_the_gemrc_gemhome_is_not_patched_in_place_of_rubys() { + let fx = build_fixture(Store::SystemGems, Home::Installed, ORIGINAL, BASE_PURL); + write_lock(&fx); + let gemrc_file = under_a_gemrc(&fx, Some(ORIGINAL)); + + let (code, stdout, stderr) = run_apply(&fx, &["--json"]); + let env = parse_env(&stdout); + assert_eq!(code, 0, "envelope: {env}\nstderr:\n{stderr}"); + assert_eq!(env["summary"]["applied"], 1, "envelope: {env}"); + assert_eq!( + std::fs::read(&fx.home_file).unwrap(), + patched_bytes(), + "the copy Ruby loads must be patched" + ); + assert_eq!( + std::fs::read(&gemrc_file).unwrap(), + ORIGINAL, + "the gemrc's copy is not one the project loads" + ); + + // The check reads the copy Ruby loads: put the upstream bytes back + // there, leave patched bytes in the gemrc's copy, and it is drift. + std::fs::write(&fx.home_file, ORIGINAL).unwrap(); + std::fs::write(&gemrc_file, patched_bytes()).unwrap(); + let (code, stdout, stderr) = run_command(&fx, &["apply", "--check"], &["--json"]); + assert_eq!( + code, 1, + "a patched copy in the gemrc's directory proves nothing.\n{stdout}\n{stderr}" + ); +} diff --git a/crates/socket-patch-cli/tests/e2e_gem_native_ext_build.rs b/crates/socket-patch-cli/tests/e2e_gem_native_ext_build.rs index 6c6d8a898e..60159ece21 100644 --- a/crates/socket-patch-cli/tests/e2e_gem_native_ext_build.rs +++ b/crates/socket-patch-cli/tests/e2e_gem_native_ext_build.rs @@ -5463,3 +5463,133 @@ fn a_home_of_two_rubies_is_attested_from_the_first_graft() { assert_eq!(code, 0, "rollback failed:\n{stdout}\n{stderr}"); assert_tree(&proj.join("vendor"), &tree_before, "after rollback"); } + +// ── A gemrc that moves the `gem` command's directories (#2317) ────────── + +/// One line of `cmd args` under `env`. +fn tool_says(cmd: &str, args: &[&str], env: &[(&str, &str)]) -> String { + let out = Command::new(cmd) + .args(args) + .envs(env.iter().copied()) + .env_remove("RUBYOPT") + .output() + .unwrap(); + assert!( + out.status.success(), + "{cmd} {args:?} failed:\n{}", + String::from_utf8_lossy(&out.stderr) + ); + String::from_utf8_lossy(&out.stdout).trim().to_string() +} + +/// The `gem` command applies the gemrc keys `gemhome` and `gempath` to +/// its own directories; Ruby and Bundler never read them. With the real +/// `gem` and `ruby`: a gemrc sends the `gem` command to one directory +/// while Ruby loads from another, both hold the gem, and agent mode must +/// patch and check the copy Ruby loads and leave the other alone. On +/// `main` the gemrc's copy was patched and `apply --check` passed over +/// the unpatched copy Ruby loads. +#[test] +#[ignore = "host capstone: asks the real gem and ruby for their directories; \ + the unpinned `test` job skips it, the pinned-Ruby e2e leg runs it via --ignored"] +fn a_gemrc_gemhome_is_not_read_in_place_of_rubys_gem_dirs() { + if !(tool_works("ruby", &["-v"]) && tool_works("gem", &["--version"])) { + assert!( + !required(), + "SOCKET_PATCH_BUNDLER_E2E_REQUIRED=1 but ruby or gem is missing" + ); + eprintln!("SKIP: ruby or gem is not available"); + return; + } + const GEM_PURL: &str = "pkg:gem/sp_gemrc@1.0.0"; + const BEFORE: &[u8] = b"module SpGemrc\n VALUE = 1\nend\n"; + const AFTER: &[u8] = b"module SpGemrc\n VALUE = 2\nend\n"; + + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path().canonicalize().unwrap(); + let stage = |home: &Path| { + let file = home.join("gems/sp_gemrc-1.0.0/lib/sp_gemrc.rb"); + std::fs::create_dir_all(file.parent().unwrap()).unwrap(); + std::fs::write(&file, BEFORE).unwrap(); + std::fs::create_dir_all(home.join("specifications")).unwrap(); + file + }; + // The home Ruby loads from (`GEM_HOME`), and the one the gemrc names. + let ruby_home = root.join("ruby-home"); + let gemrc_home = root.join("gemrc-home"); + let ruby_copy = stage(&ruby_home); + let gemrc_copy = stage(&gemrc_home); + let gemrc = root.join("gemrc"); + std::fs::write(&gemrc, format!("gemhome: {}\n", gemrc_home.display())).unwrap(); + + let proj = root.join("proj"); + std::fs::create_dir_all(&proj).unwrap(); + std::fs::write(proj.join("Gemfile"), "source \"https://rubygems.org\"\n").unwrap(); + std::fs::write( + proj.join("Gemfile.lock"), + "GEM\n remote: https://rubygems.org/\n specs:\n sp_gemrc (1.0.0)\n\n\ + PLATFORMS\n ruby\n\nDEPENDENCIES\n sp_gemrc\n\nBUNDLED WITH\n 2.5.22\n", + ) + .unwrap(); + let socket = proj.join(".socket"); + let (bh, ah) = (git_sha256(BEFORE), git_sha256(AFTER)); + write_minimal_manifest( + &socket, + GEM_PURL, + "23172317-2317-4317-8317-231723172317", + &[PatchEntry { + file_name: "lib/sp_gemrc.rb", + before_hash: &bh, + after_hash: &ah, + }], + ); + write_blob(&socket, &ah, AFTER); + write_blob(&socket, &bh, BEFORE); + + let env = [ + ("GEMRC", gemrc.to_str().unwrap()), + ("GEM_HOME", ruby_home.to_str().unwrap()), + ("SOCKET_RUBYGEMS_URL", NO_REGISTRY), + ]; + // The premise, from the real tools: the two disagree under the gemrc. + assert_eq!( + tool_says("gem", &["env", "gemdir"], &env), + gemrc_home.to_str().unwrap(), + "the gem command follows the gemrc" + ); + let gem_path = tool_says("ruby", &["-e", "puts Gem.path"], &env); + let gem_path: Vec<&str> = gem_path.lines().collect(); + assert!( + gem_path.contains(&ruby_home.to_str().unwrap()), + "{gem_path:?}" + ); + assert!( + !gem_path.contains(&gemrc_home.to_str().unwrap()), + "Ruby does not read the gemrc's gemhome: {gem_path:?}" + ); + + let run = |args: &[&str]| common::run_with_env(&proj, args, &env); + let (code, stdout, stderr) = run(&["apply", "--check", "--ecosystems", "gem"]); + assert_eq!( + code, 1, + "the copy Ruby loads is unpatched:\n{stdout}\n{stderr}" + ); + + let (code, stdout, stderr) = run(&["apply", "--json", "--offline", "--ecosystems", "gem"]); + assert_eq!(code, 0, "apply failed:\n{stdout}\n{stderr}"); + let event = common::envelope::event_for(&parse_json_envelope(&stdout), GEM_PURL).clone(); + assert_eq!(event["action"], "applied", "{stdout}"); + assert_eq!( + std::fs::read(&ruby_copy).unwrap(), + AFTER, + "the copy Ruby loads must be patched" + ); + assert_eq!( + std::fs::read(&gemrc_copy).unwrap(), + BEFORE, + "the gemrc's copy is not one Ruby loads" + ); + + let (code, stdout, stderr) = run(&["apply", "--check", "--ecosystems", "gem"]); + assert_eq!(code, 0, "{stdout}\n{stderr}"); +} diff --git a/crates/socket-patch-core/tests/crawler_ruby_e2e.rs b/crates/socket-patch-core/tests/crawler_ruby_e2e.rs index bf1c45f169..93324f5318 100644 --- a/crates/socket-patch-core/tests/crawler_ruby_e2e.rs +++ b/crates/socket-patch-core/tests/crawler_ruby_e2e.rs @@ -81,6 +81,38 @@ fn install_fake_gem_with_gempath(bin_dir: &Path, gemdir: &Path, gempath: &str) { std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap(); } +/// A `gem` that answers the way the real command does under a gemrc that +/// sets `gemhome` (#2317): `gem env gemdir` and `gem env gempath` name +/// `gemrc_home`, and only with `--norc` does it name the directories +/// Ruby itself uses, `ruby_gemdir` and `ruby_gempath`. +#[cfg(unix)] +fn install_fake_gem_with_gemrc( + bin_dir: &Path, + gemrc_home: &Path, + ruby_gemdir: &Path, + ruby_gempath: &str, +) { + use std::os::unix::fs::PermissionsExt; + let script = format!( + "#!/bin/sh\n\ + [ \"$1\" = env ] || exit 1\n\ + norc=no\n\ + for arg in \"$@\"; do [ \"$arg\" = --norc ] && norc=yes; done\n\ + if [ $norc = no ]; then printf '%s\\n' \"{gemrc}\"; exit 0; fi\n\ + case \"$2\" in\n\ + gemdir) printf '%s\\n' \"{dir}\" ;;\n\ + gempath) printf '%s\\n' \"{path}\" ;;\n\ + *) exit 1 ;;\n\ + esac\n", + gemrc = gemrc_home.display(), + dir = ruby_gemdir.display(), + path = ruby_gempath, + ); + let bin = bin_dir.join("gem"); + std::fs::write(&bin, script).unwrap(); + std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap(); +} + /// Swap `PATH` for the duration of `f`, restoring the previous value (or /// unsetting it when it started unset) even though `f` returns a value. #[cfg(unix)] @@ -1378,3 +1410,83 @@ async fn moved_bundle_gemfile_root_crawls_the_loaded_copy() { assert_eq!(unused, vec![system_gems.clone()], "{label}"); } } + +/// A gemrc that sets `gemhome` (or `gempath`) moves the directories the +/// `gem` command works on, and nothing else: Ruby and Bundler never read +/// those keys, so `require` and `bundle exec` still load from `Gem.dir` +/// and `Gem.path`. The project's gems must be looked up there, and the +/// gemrc's directory, which the project loads nothing from, must not be +/// read in their place (#2317). Global mode asks the same question. +#[cfg(unix)] +#[tokio::test] +#[serial] +async fn get_gem_paths_reads_rubys_gem_dirs_not_the_gemrcs() { + let tmp = tempfile::tempdir().unwrap(); + let project = tmp.path().join("app"); + std::fs::create_dir_all(&project).unwrap(); + std::fs::write(project.join("Gemfile"), "source 'https://rubygems.org'\n").unwrap(); + + // Where the gemrc sends `gem install`; it holds the gem too. + let gemrc_home = tmp.path().join("gemrc-home"); + stage_gem(&gemrc_home.join("gems"), "colorize", "1.1.0").await; + // Ruby's own: the default directory and the user directory. + let default_dir = tmp.path().join("default"); + let user_dir = tmp.path().join("user"); + let default_copy = stage_gem(&default_dir.join("gems"), "colorize", "1.1.0").await; + let user_copy = stage_gem(&user_dir.join("gems"), "multi_json", "1.15.0").await; + let gempath = std::env::join_paths([&user_dir, &default_dir]).unwrap(); + + let bin = tempfile::tempdir().unwrap(); + install_fake_gem_with_gemrc( + bin.path(), + &gemrc_home, + &default_dir, + gempath.to_str().unwrap(), + ); + + let (local, global, found) = with_path(bin.path(), || async { + let crawler = RubyCrawler; + let local = crawler + .get_gem_paths_with_env(&options_at(&project), None, None, None, None) + .await + .unwrap(); + let global = crawler + .get_gem_paths(&CrawlerOptions { + global: true, + ..options_at(&project) + }) + .await + .unwrap(); + let purls = vec![ + "pkg:gem/colorize@1.1.0".to_string(), + "pkg:gem/multi_json@1.15.0".to_string(), + ]; + let mut found = Vec::new(); + for gems_dir in &local { + let hits = crawler.find_by_purls(gems_dir, &purls).await.unwrap(); + found.extend(hits.into_values().map(|package| package.path)); + } + found.sort(); + (local, global, found) + }) + .await; + + let rubys = vec![default_dir.join("gems"), user_dir.join("gems")]; + assert_eq!( + local, rubys, + "a project's gems are in Ruby's `Gem.dir` and `Gem.path`" + ); + for dir in &rubys { + assert!( + global.contains(dir), + "global mode must read {dir:?}: {global:?}" + ); + } + assert!( + !global.contains(&gemrc_home.join("gems")), + "the gemrc's directory is not one Ruby loads from: {global:?}" + ); + let mut loaded = vec![default_copy, user_copy]; + loaded.sort(); + assert_eq!(found, loaded, "only the copies Ruby loads are found"); +} From bf39b979371624a6cf972e2223d56f1bc4620b7b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 17:43:29 +0000 Subject: [PATCH 3/4] Ask gem env for Ruby's gem dirs, not the gemrc's A Bundler project with no bundle path of its own, and a --global run, find the gem directories with `gem env gemdir` and `gem env gempath`. The gem command applies the gemrc keys gemhome and gempath to its own directories. Ruby and Bundler never read them: require, bundle install and bundle exec load from Gem.dir and Gem.path as the environment gives them. With either key in a gemrc, the run read a directory the project loads nothing from and skipped the ones it does. An installed gem was reported as not installed, and a copy of the same version in the gemrc's directory was patched, checked and attested in place of the unpatched one the application loads. Both questions now pass --norc, which answers with Gem.dir and Gem.path. GEM_HOME and GEM_PATH are the environment's and count as before. Every caller shares the one probe: the project fallback, the global paths, the hosted stale-install probe and the rollback lookup. Fixes #2317. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU Assisted-by: Claude Code:claude-fable-5-2 --- crates/socket-patch-cli/CLI_CONTRACT.md | 7 ++++++- .../src/crawlers/ruby_crawler.rs | 21 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 5e266c2943..d1c9a37dad 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -1709,7 +1709,12 @@ the model is **not uniform** today: touch it (#709). Explicit env/config/standalone roots only count when `--cwd` holds a Bundler manifest/lockfile. When the default `vendor/bundle` root holds no store, the gem homes `gem env` reports are appended (default gems like json/uri only ever live there); which of the copies - found there are written is the "Copy classes" rule below. When the Bundler tier that + found there are written is the "Copy classes" rule below. The question is asked as + `gem env gemdir --norc` and `gem env gempath --norc`, which is Ruby's own `Gem.dir` and + `Gem.path` (`GEM_HOME` and `GEM_PATH` included): the gemrc keys `gemhome` and `gempath` move + only the `gem` command's directories, Ruby and Bundler never read them, so a directory only a + gemrc names is not one the project's (or, under `--global`, the machine's) gems are loaded + from, and it is not read (#2317). When the Bundler tier that decides the install path (the first of the app config, the environment and the global config that sets `path`, `path.system` or `disable_shared_gems`) sets a truthy `path.system` (Bundler's own coercion: anything but `false`/`f`/`no`/`n`/`0`/empty), bundler installs into and loads from the diff --git a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs index 288418faa9..2f82aeca92 100644 --- a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs @@ -311,9 +311,10 @@ impl RubyCrawler { false } - /// The gem homes `gem env` itself reports, each mapped to its `gems/` - /// subdirectory: `gemdir` (the active `GEM_HOME`) first, then every - /// `gempath` (`GEM_PATH`) entry. Non-existent homes and duplicates are + /// The gem homes `gem env` reports with no gemrc applied + /// ([`Self::run_gem_env`]), each mapped to its `gems/` subdirectory: + /// `gemdir` (Ruby's `Gem.dir`, the active `GEM_HOME`) first, then + /// every `gempath` (`Gem.path`, `GEM_PATH`) entry. Non-existent homes and duplicates are /// dropped, so the result is the deduped set of installed-gem roots in /// RubyGems' own precedence order. /// @@ -1059,11 +1060,21 @@ impl RubyCrawler { .await } - /// Run `gem env ` (on the blocking pool — it waits on a + /// Run `gem env --norc` (on the blocking pool — it waits on a /// subprocess) and return the trimmed stdout. + /// + /// `--norc` makes the answer Ruby's own `Gem.dir` / `Gem.path`. The + /// `gem` command applies the gemrc keys `gemhome` and `gempath` + /// (`~/.gemrc`, the system gemrc, a `GEMRC` file) to its own + /// directories, and nothing else reads them: `require`, `bundle + /// install` and `bundle exec` load from the directories the + /// environment gives. Without the flag a gemrc that sets either key + /// names a directory the project loads nothing from and hides the + /// ones it does (#2317). `GEM_HOME` and `GEM_PATH` are the + /// environment's and count either way. async fn run_gem_env(key: &'static str, runner: ProbeRunner) -> Option { run_blocking(move || { - let stdout = runner.run("gem", &["env", key]); + let stdout = runner.run("gem", &["env", key, "--norc"]); parse_gem_env_output(stdout.as_deref().unwrap_or("")) }) .await From 4b2e385b17e71afa6675a529671a2cbd2f59b6a7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 17:54:24 +0000 Subject: [PATCH 4/4] Ask a gem without --norc the plain question A gem command that gives no answer to `gem env --norc` (one too old to know the flag, or a wrapper that refuses it) left a system-gems project with no gem directory at all: every gem read as not installed, and apply --check passed over unpatched code. Such a gem is now asked again without the flag, which is the question every earlier release asked. Its answer is right wherever no gemrc sets gemhome or gempath. Found by the review of this branch. The new test fails on the previous commit. Refs #2317. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU Assisted-by: Claude Code:claude-fable-5-2 --- crates/socket-patch-cli/CLI_CONTRACT.md | 3 +- .../src/crawlers/ruby_crawler.rs | 12 +++++- .../tests/crawler_ruby_e2e.rs | 43 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index d1c9a37dad..3d4de1527a 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -1714,7 +1714,8 @@ the model is **not uniform** today: `Gem.path` (`GEM_HOME` and `GEM_PATH` included): the gemrc keys `gemhome` and `gempath` move only the `gem` command's directories, Ruby and Bundler never read them, so a directory only a gemrc names is not one the project's (or, under `--global`, the machine's) gems are loaded - from, and it is not read (#2317). When the Bundler tier that + from, and it is not read (#2317). A `gem` that gives no answer with `--norc` is asked the same + question without it. When the Bundler tier that decides the install path (the first of the app config, the environment and the global config that sets `path`, `path.system` or `disable_shared_gems`) sets a truthy `path.system` (Bundler's own coercion: anything but `false`/`f`/`no`/`n`/`0`/empty), bundler installs into and loads from the diff --git a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs index 2f82aeca92..e1a4102478 100644 --- a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs @@ -1072,10 +1072,18 @@ impl RubyCrawler { /// names a directory the project loads nothing from and hides the /// ones it does (#2317). `GEM_HOME` and `GEM_PATH` are the /// environment's and count either way. + /// + /// A `gem` that gives no answer with the flag (one too old to know + /// it, or a wrapper that refuses it) is asked again without it, the + /// question every release before this one asked: its answer is right + /// wherever no gemrc sets those keys, and no answer at all would + /// report every gem of a system-gems project as not installed. async fn run_gem_env(key: &'static str, runner: ProbeRunner) -> Option { run_blocking(move || { - let stdout = runner.run("gem", &["env", key, "--norc"]); - parse_gem_env_output(stdout.as_deref().unwrap_or("")) + let ask = |args: &[&str]| { + parse_gem_env_output(runner.run("gem", args).as_deref().unwrap_or("")) + }; + ask(&["env", key, "--norc"]).or_else(|| ask(&["env", key])) }) .await } diff --git a/crates/socket-patch-core/tests/crawler_ruby_e2e.rs b/crates/socket-patch-core/tests/crawler_ruby_e2e.rs index 93324f5318..6eaa3f0de6 100644 --- a/crates/socket-patch-core/tests/crawler_ruby_e2e.rs +++ b/crates/socket-patch-core/tests/crawler_ruby_e2e.rs @@ -1490,3 +1490,46 @@ async fn get_gem_paths_reads_rubys_gem_dirs_not_the_gemrcs() { loaded.sort(); assert_eq!(found, loaded, "only the copies Ruby loads are found"); } + +/// A `gem` that does not take `--norc` (it exits non-zero, as on an +/// option it does not know) still answers the plain question, and that +/// answer is used: the project's gems are found as they were before the +/// flag was passed (#2317). +#[cfg(unix)] +#[tokio::test] +#[serial] +async fn get_gem_paths_asks_a_gem_without_norc_the_plain_question() { + use std::os::unix::fs::PermissionsExt; + let tmp = tempfile::tempdir().unwrap(); + let project = tmp.path().join("app"); + std::fs::create_dir_all(&project).unwrap(); + std::fs::write(project.join("Gemfile"), "source 'https://rubygems.org'\n").unwrap(); + let home = tmp.path().join("home"); + stage_gem(&home.join("gems"), "colorize", "1.1.0").await; + + let bin = tempfile::tempdir().unwrap(); + let gem = bin.path().join("gem"); + std::fs::write( + &gem, + format!( + "#!/bin/sh\n\ + for arg in \"$@\"; do\n\ + if [ \"$arg\" = --norc ]; then echo 'invalid option: --norc' >&2; exit 1; fi\n\ + done\n\ + if [ \"$1\" = env ] && [ \"$2\" = gemdir ]; then printf '%s\\n' \"{}\"; exit 0; fi\n\ + exit 1\n", + home.display() + ), + ) + .unwrap(); + std::fs::set_permissions(&gem, std::fs::Permissions::from_mode(0o755)).unwrap(); + + let paths = with_path(bin.path(), || async { + RubyCrawler + .get_gem_paths_with_env(&options_at(&project), None, None, None, None) + .await + .unwrap() + }) + .await; + assert_eq!(paths, vec![home.join("gems")]); +}