Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sh_test/binary fails when reading files on windows with --noenable_runfiles #22796

Closed
peakschris opened this issue Jun 19, 2024 · 2 comments
Closed
Assignees
Labels
awaiting-user-response Awaiting a response from the author P2 We'll consider working on this in future. (Assignee optional) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug

Comments

@peakschris
Copy link

peakschris commented Jun 19, 2024

Description of the bug:

sh_test and sh_binary always fails when reading files on windows when runfiles is disabled.

It does not matter whether the runfiles bash library is used or not, the files cannot be read.

The example below shows the issue with sh_test, you can change sh_test in the example to sh_binary and see exactly the same behaviour.

I've put windows in the title, this is the only platform I have tried this on. I guess it might fail the same on other platforms too.

Which category does this issue belong to?

Local Execution

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

To reproduce:

  • create a vanilla workspace, bazel 7.2.0, windows
  • touch MODULE.bazel && touch WORKSPACE && touch .bazelrc
  • set BAZEL_SH=c:/msys64/usr/bin/bash.exe
  • mkdir test
  • create these files in test: [BUILD.bazel, file1.txt, test.sh]

.bazelrc

startup --windows_enable_symlinks

BUILD.bazel

sh_test(
    name = "test",
    srcs = ["test.sh"],
    args = [
        "$(location file1.txt)",
    ],
    data = [
        "file1.txt",
        "@bazel_tools//tools/bash/runfiles",
    ],
)

file1.txt

file1

test.sh

#!/usr/bin/env bash

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
  source "$0.runfiles/$f" 2>/dev/null || \
  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

echo test.sh $1
if [[ -n $1 ]]; then
    echo $1 does not exist in cwd
fi
path1=$(rlocation $1)
echo rlocation-arg1 returns $path1

FILE1=$(cat "$path1")
if [[ "$FILE1" == "" ]]; then
    echo "failed to read file"
    echo `pwd`
    echo $path1
    exit 1
fi
if [[ "$FILE1" == "file1" ]]
then
    echo "ok"
    exit 0
else
    echo "test failed"
    echo $1 is not expected 'file1'
    exit 1
fi

set RUNFILES_LIB_DEBUG=1
bazel run //test:test --noenable_runfiles --test_output=all

D:\workdir\windows-runfiles-bug>bazel run //test:test --noenable_runfiles
INFO: Running command line: external/bazel_tools/tools/test/tw.exe test/test.exe test/file1.txt
Executing tests from //test:test
-----------------------------------------------------------------------------
test.sh test/file1.txt
test/file1.txt does not exist in cwd
INFO[runfiles.bash]: rlocation(test/file1.txt): start
INFO[runfiles.bash]: runfiles_current_repository(2): caller's path is (D:\udu\b\oeelfdds\execroot\__main__\bazel-out\x64_windows-fastbuild\bin\test\test)
INFO[runfiles.bash]: runfiles_current_repository(2): (D:/udu/b/oeelfdds/execroot/__main__/bazel-out/x64_windows-fastbuild/bin/test/test) is the target of (__main__/test/test) in the runfiles manifest
INFO[runfiles.bash]: runfiles_current_repository(2): (D:\udu\b\oeelfdds\execroot\__main__\bazel-out\x64_windows-fastbuild\bin\test\test) corresponds to rlocation path (__main__/test/test)
INFO[runfiles.bash]: runfiles_current_repository(2): (__main__/test/test) lies in repository (__main__)
INFO[runfiles.bash]: rlocation(test/file1.txt): looking up canonical name for (test) from (__main__) in (D:/udu/b/oeelfdds/execroot/__main__/bazel-out/x64_windows-fastbuild/bin/test/test.exe.repo_mapping)
INFO[runfiles.bash]: rlocation(test/file1.txt): canonical name of target repo is ()
INFO[runfiles.bash]: rlocation(test/file1.txt): looking in RUNFILES_MANIFEST_FILE (d:/udu/b/oeelfdds/execroot/__main__/bazel-out/x64_windows-fastbuild/bin/test/test.exe.runfiles/MANIFEST)
INFO[runfiles.bash]: rlocation(test/file1.txt): not found in manifest
rlocation-arg1 returns
cat: '': No such file or directory

The manifest contains:

D:\workdir\windows-runfiles-bug>cat d:/udu/b/oeelfdds/execroot/_main/bazel-out/x64_windows-fastbuild/bin/test/test.exe.runfiles/MANIFEST
_main/external/bazel_tools/tools/bash/runfiles/runfiles.bash D:/udu/b/oeelfdds/external/bazel_tools/tools/bash/runfiles/runfiles.bash
_main/test/file1.txt D:/workdir/windows-runfiles-bug/test/file1.txt
_main/test/test D:/udu/b/oeelfdds/execroot/_main/bazel-out/x64_windows-fastbuild/bin/test/test
_main/test/test.exe D:/udu/b/oeelfdds/execroot/_main/bazel-out/x64_windows-fastbuild/bin/test/test.exe
_main/test/test.sh D:/workdir/windows-runfiles-bug/test/test.sh
_repo_mapping D:/udu/b/oeelfdds/execroot/_main/bazel-out/x64_windows-fastbuild/bin/test/test.exe.repo_mapping
bazel_tools/tools/bash/runfiles/runfiles.bash D:/udu/b/oeelfdds/external/bazel_tools/tools/bash/runfiles/runfiles.bash

Looks like the issue is that the short path in manifest has 'main' prefix, but path returned by $(location test1.txt) in BUILD.bazel does not and the runfiles bash script doesn't resolve the discrepancy.

I've seen similar issues in rules_go's runfile resolution.

Which operating system are you running Bazel on?

windows

What is the output of bazel info release?

7.2.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@github-actions github-actions bot added the team-Local-Exec Issues and PRs for the Execution (Local) team label Jun 19, 2024
@peakschris peakschris changed the title sh_test fails when reading files on windows with --noenable_runfiles sh_test/binary fails when reading files on windows with --noenable_runfiles Jun 19, 2024
@fmeum fmeum added P2 We'll consider working on this in future. (Assignee optional) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. and removed untriaged team-Local-Exec Issues and PRs for the Execution (Local) team labels Jun 19, 2024
@fmeum fmeum self-assigned this Jun 19, 2024
@fmeum
Copy link
Collaborator

fmeum commented Jun 19, 2024

What happens if you replace location with rlocationpath?

@fmeum fmeum added the awaiting-user-response Awaiting a response from the author label Jun 19, 2024
@peakschris
Copy link
Author

That fixes it. I missed the deprecation on location and clear instructions -- sorry. Thank you for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-user-response Awaiting a response from the author P2 We'll consider working on this in future. (Assignee optional) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug
Projects
None yet
Development

No branches or pull requests

5 participants