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
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ jobs:
cd /
"$launcher" 1 2
if: matrix.os != 'windows-latest'
# Binaries from other repositories, such as Bundler binstubs inside the
# generated bin/private directory, are always located through runfiles.
- name: Run binstub launcher from another directory
shell: bash
env:
# Git Bash rewrites `@bundle//bin:rake` into `@bundle/bin:rake` on Windows.
MSYS_NO_PATHCONV: 1
run: |
bazel build @bundle//bin:rake
launcher="$PWD/$(bazel cquery --output=starlark --starlark:expr='target.files_to_run.executable.path' @bundle//bin:rake)"
cd /
"$launcher" --version
- if: failure() && runner.debug == '1'
uses: mxschmitt/action-tmate@v3

Expand Down
31 changes: 14 additions & 17 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,23 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {},
if binary and binary != toolchain.ruby:
binary_path = binary.short_path

# Runfiles library for Windows does not support generated directories,
# so for now we skip locating the binary in runfiles. This only prevents
# running binary scripts directly and should not affect normal `bazel run`.
# See BATCH_RLOCATION_FUNCTION comments for more details.
if _is_windows(ctx):
# Binaries outside the main repository are always expected to be
# locatable through runfiles. Binaries in the main repository are
# resolved through runfiles when possible, so that they can be
# executed from any working directory (e.g. via --run_under or by
# running the launcher directly), and fall back to the relative
# path, which requires the default `bazel run` working directory.
if binary.short_path.startswith("../"):
binary_path = _to_rlocation_path(ctx, binary)
locate_binary_in_runfiles = "true"
elif _is_windows(ctx):
# The batch rlocation function exits on a miss, so the Windows
# launcher has no fallback and uses the relative path.
binary_path = _normalize_path(ctx, binary_path)
else:
binary_path = _to_rlocation_path(ctx, binary)

# Binaries outside the main repository are always expected to be
# locatable through runfiles. Binaries in the main repository are
# resolved through runfiles when possible, so that they can be
# executed from any working directory (e.g. via --run_under or by
# running the launcher directly), and fall back to the relative
# path, which requires the default `bazel run` working directory.
if binary.short_path.startswith("../"):
locate_binary_in_runfiles = "true"
else:
binary_fallback_path = _normalize_path(ctx, binary.short_path)
locate_binary_in_runfiles = "fallback"
binary_fallback_path = _normalize_path(ctx, binary.short_path)
locate_binary_in_runfiles = "fallback"

# JRuby's .cmd stubs are plain batch files, so they must be executed
# directly. MRI's are Ruby polyglots and only work via the interpreter.
Expand Down
8 changes: 7 additions & 1 deletion ruby/private/binary/binary.cmd.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ if "{bundler_command}" neq "" (
)
)

if "{locate_binary_in_runfiles}" neq "" (
call :rlocation "{binary}" binary
) else (
set "binary={binary}"
)

if "%COVERAGE%" == "1" (
call :rlocation {coverage_helper} _coverage_helper_path
for %%a in ("!_coverage_helper_path!\..") do set "RUBYLIB=%%~fa;!RUBYLIB!"
Expand All @@ -53,6 +59,6 @@ if "%COVERAGE%" == "1" (
)
)

{bundler_command} {ruby_binary_name} {binary} {args} %*
{bundler_command} {ruby_binary_name} !binary! {args} %*

:: vim: ft=dosbatch
42 changes: 25 additions & 17 deletions ruby/private/utils.bzl

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,37 @@ set abs_path=
for /F "tokens=2* usebackq" %%i in (`%SYSTEMROOT%\system32\findstr.exe /l /c:"!runfile_path! " "%MF%"`) do (
set abs_path=%%i
)
:: There should be a way to locate the fully resolved path to a file in runfiles
:: because the manifest doesn't contain paths to files in directories. This prevents
:: finding Bundler-generated binstubs (runfiles manifest has only `bin/private` record).
:: It works fine on runfiles.bash, so it's a matter of making a better batch script that
:: handles all the cases. PRs are welcome but current Ruby ruleset maintainers don't
:: have enough Windows scripting knowledge. Below is a naive attempt to make it work
:: but it fails in certain cases, so this functionality is completely disabled.
::
:: if "!abs_path!" equ "" (
:: if exist %~f0.runfiles\!runfile_path:/=\! (
:: set pshpath=%~f0.runfiles\!runfile_path:/=\!
:: for /f %%A in ('powershell -Command "Get-Item -LiteralPath \"!pshpath!\" | Get-ItemProperty | Select-Object -ExpandProperty Target"') do (
:: set abs_path=%%A
:: set abs_path=!abs_path:\=/!
:: )
:: )
:: )
:: The manifest has no entries for files inside directories (e.g. Bundler
:: binstubs under `bin/private`), so look up the nearest parent directory entry
:: and append the rest of the path to it.
if "!abs_path!" equ "" (
set "rlocation_dir=!runfile_path!"
set "rlocation_rest="
call :rlocation_parent
)
if "!abs_path!" equ "" (
echo>&2 ERROR: !runfile_path! not found in runfiles manifest
exit 1
)
set %~2=!abs_path!
exit /b 0
:rlocation_parent
if "!rlocation_dir!" equ "" exit /b 0
set "rlocation_char=!rlocation_dir:~-1!"
set "rlocation_dir=!rlocation_dir:~0,-1!"
if "!rlocation_char!" neq "/" (
set "rlocation_rest=!rlocation_char!!rlocation_rest!"
goto :rlocation_parent
)
for /F "tokens=2* usebackq" %%i in (`%SYSTEMROOT%\system32\findstr.exe /b /l /c:"!rlocation_dir! " "%MF%"`) do (
set abs_path=%%i
)
if "!abs_path!" neq "" (
set "abs_path=!abs_path!/!rlocation_rest!"
exit /b 0
)
set "rlocation_rest=/!rlocation_rest!"
goto :rlocation_parent
:rlocation_end
:: End of rlocation
""".replace("\r\n", "\n").replace("\n", "\r\n")
Expand Down
Loading