Skip to content

fix: resolve rb_binary main scripts from any working directory - #427

Merged
p0deje merged 1 commit into
bazel-contrib:mainfrom
acecilia:fix/rb-binary-cwd-independent-main
Oct 26, 2026
Merged

p0deje merged 1 commit into
bazel-contrib:mainfrom
acecilia:fix/rb-binary-cwd-independent-main

Conversation

@acecilia

@acecilia acecilia commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

rb_binary launchers fail with a LoadError when they run with a working directory other than the bazel run default:

ruby: No such file or directory -- lib/gem/add.rb (LoadError)

This affects:

  • bazel run --run_under="cd ... &&" ... (e.g. wrappers that preserve the caller's cwd)
  • running the generated launcher binary directly (e.g. from an IDE or a script)

Root cause

generate_rb_binary_script only locates the main script through runfiles when it lives outside the main repository (../-prefixed short path). Mains in the main repository are baked into the launcher as raw relative paths, so they only resolve when the cwd matches bazel run's default (<launcher>.runfiles/_main).

Fix

Locate the main script through runfiles for non-Windows platforms regardless of repository, with a runtime fallback to the previous relative-path behavior:

  • external-repository mains: unchanged (strict rlocation, fails with a hint if missing)
  • main-repository mains: rlocation, falling back to the relative path when the lookup misses (e.g. a main not included in runfiles), so plain bazel run behaves exactly as before
  • Windows: unchanged (raw path, see the existing runfiles-library caveat)

Related

Testing

  • examples/gem: bazel run lib/gem:add-numbers 2 — unchanged ✅
  • direct launcher invocation from / — previously LoadError, now prints 3 ✅
  • bazel run --run_under="cd / &&" lib/gem:add-numbers -- 2 — now prints 3 ✅
  • added a CI step to examples-gem (non-Windows) exercising direct launcher invocation from a foreign cwd
  • bazel run :buildifier.check ✅
  • bazel test --ignore_dev_dependency //ruby/tests:all ✅ (7/7)

Created with AI ✨ but reviewed thoroughly by me 👨‍💻

The launcher resolves main scripts in the main repository relative to the
working directory, which only matches bazel run's default cwd. Invocations
that change the cwd (e.g. bazel --run_under="cd ...", or running the
launcher binary directly) fail with a LoadError.

Locate these scripts through runfiles instead, falling back to the relative
path so behavior under plain bazel run is unchanged. Windows behavior is
unchanged.
@acecilia
acecilia marked this pull request as ready for review October 22, 2026 23:05
@p0deje
p0deje merged commit 14e9863 into bazel-contrib:main Oct 26, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants