aspect-build / rules_js

High-performance Bazel rules for running Node.js tools and building JavaScript projects
https://docs.aspect.build/rules/aspect_rules_js
Apache License 2.0
295 stars 101 forks source link

[Bug]: RUNFILES dir resolution sometimes wrong in "transitive" case #1267

Open matthewjh opened 10 months ago

matthewjh commented 10 months ago

What happened?

Hi,

I have a case of (3) in:

# Set a RUNFILES environment variable to the root of the runfiles tree
# since RUNFILES_DIR is not set by Bazel in all contexts.
# For example, `RUNFILES=/path/to/my_js_binary.sh.runfiles`.
#
# Call this program X. X was generated by a genrule and may be invoked
# in many ways:
#   1a) directly by a user, with $0 in the output tree
#   1b) via 'bazel run' (similar to case 1a)
#   2) directly by a user, with $0 in X's runfiles
#   3) by another program Y which has a data dependency on X, with $0 in Y's
#      runfiles
#   4a) via 'bazel test'
#   4b) case 3 in the context of a test
#   5a) by a genrule cmd, with $0 in the output tree
#   6a) case 3 in the context of a genrule

that sporadically fails. sh_binary X has a dep on js_binary Y. RUNFILES should resolve to X.sh.runfiles, and most of the time it does, but sporadically it resolves to Y.runfiles, which manifests as a downstream failure to resolve node in the following block of the sh wrapper, because Y.runfiles doesn't exist:

export JS_BINARY__NODE_BINARY="$JS_BINARY__RUNFILES/web/../tw_node_18_darwin_arm64/node"
    if [ ! -f "$JS_BINARY__NODE_BINARY" ]; then
        logf_fatal "node binary '%s' not found in runfiles" "$JS_BINARY__NODE_BINARY"
        exit 1
    fi

Y.runfiles is printed in the failing case; X.runfiles in the working cases.

Note the binary is being invoked via bazel run.

Version

Development (host) and target OS/architectures:

Output of bazel --version: 6.3.1 Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file:

    http_archive(
        name = "build_bazel_rules_nodejs",
        sha256 = "709cc0dcb51cf9028dd57c268066e5bc8f03a119ded410a13b5c3925d6e43c48",
        urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.4/rules_nodejs-5.8.4.tar.gz"],
    )

    http_archive(
        name = "rules_nodejs",
        sha256 = "8fc8e300cb67b89ceebd5b8ba6896ff273c84f6099fc88d23f24e7102319d8fd",
        urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.4/rules_nodejs-core-5.8.4.tar.gz"],
    )

    http_archive(
        name = "aspect_rules_js",
        sha256 = "7b2a4d1d264e105eae49a27e2e78065b23e2e45724df2251eacdd317e95bfdfd",
        strip_prefix = "rules_js-1.31.0",
        url = "https://github.com/aspect-build/rules_js/releases/download/v1.31.0/rules_js-v1.31.0.tar.gz",
    )

Language(s) and/or frameworks involved:

How to reproduce

No response

Any other information?

No response

gregmagolan commented 2 months ago

This came up in the context of a question on Bazel Slack: https://bazelbuild.slack.com/archives/CEZUUKQ6P/p1715513010992479?thread_ts=1715434089.351019&cid=CEZUUKQ6P.

"sporadically" is strange and interesting. Did you have --nobuild_runfiles_links set when you saw this @matthewjh?