aspect-build / aspect-build/rules_js

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

Open
#1,267 1 comment 0 reactions 0 assignees View on GitHub
bug need: investigation
Dominant language
Starlark
Stars
378
Forks
183
Avg merge
1d 9h
Merged PRs (30d)
32

Description

### 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:

```bash
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_

Contributor guide

Open the contributing guide

Research direction

Start by tracing RUNFILES resolution for the listed transitive invocation cases, especially the bazel run path, using the generated sh wrapper and its JS_BINARY__RUNFILES variables. Reproduce the intermittent choice between X.runfiles and Y.runfiles, then verify that the resolved path points to X.runfiles and allows the node binary check to succeed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.