Skip to content
This repository was archived by the owner on Sep 10, 2026. It is now read-only.
This repository was archived by the owner on Sep 10, 2026. It is now read-only.

Executing node_binary clobbers current working directory #36

Closed

Description

@driveraid

Hi,
We're using a node binary to implement a small IDL compiler:

node_binary(
    name = "gen",
    srcs = [...]
    ...)

The binary gets used later in a genrule, something like this:

genrule(
    name = "gen_osl",
    srcs = ["example/file.idl"],
    tools = [":gen"],
    cmd = "$(location :gen) ...options... $(location example/file.idl)",
    ...)

However, we find that the generated runner for the gen target has the following content,

#!/usr/bin/env bash
set -e
ROOT=$(dirname $0)
export PATH="$ROOT:$PATH"
cd $ROOT/gen_bundle && exec node node_modules/platform/tools/reaktor/gen_module $@
  1. Is there a special reason as to why to chdir into the gen_bundle directory? It destroys our ability to locate the source file.
  2. Do you know of some other way I could work around?

Activity

  1. pcj commented on Oct 9, 2017

    @pcj
    Contributor

    Hi @driveraid. In the previous version of rules_node there was a longstanding issue of how to run a node binary in the context of various different execution scenarios (bazel run :foo, ./bazel-bin/foo, bazel test :foo_test) and so forth. Copying over the node binary to the bundle directory and exec`ing there was a way to avoid having to detect where runfiles directory was, etc. Very clever, or so I thought!

    Unfortunately as I see now this does not work in a genfile context! It should be possible to redo the create_launcher function to accomodate this use case, hopefully in a non-hacky way.

    https://github.com/pubref/rules_node/blob/master/node/internal/node_binary.bzl#L77-L93

    As possible short-term workaround might be to adjust the location of the example/file.idl location based on the knowledge of where the script is cd'ing into. You can experiment by putting in ls -al . or find . into your genrule cmd to debug that.

  2. driveraid commented on Oct 9, 2017

    @driveraid
    Author

    Thanks @pcj.
    For a short term workaround, we realized that in this case we were able to modify the IDL compiler to accept a new --chdir option, enabling bridging of the WD like follows:

    genrule(
        name = "gen_osl",
        srcs = ["example/file.idl"],
        tools = [":gen"],
        cmd = "export GEN_WD=$$(pwd) && $(location :gen) --chdir $$GEN_WD ...options... $(location example/file.idl)",
        ...)

    It's a bit wierd, we suddenly had to disable sandboxing by issuing --genrule_strategy=standalone because the cd invocation from the runner stopped working (gen_bundle not present in sandbox)... but it's late... could be that too :)

    It would be nice with a cleaner solution to this issue!

    Thanks

  3. pcj commented on Oct 9, 2017

    @pcj
    Contributor

    In regard to the sandbox issue, if you write your genrule as an actual build rule, you could access the files property on the node_binary provider and propagate that to the ctx.action.inputs file list.

    https://github.com/pubref/rules_node/blob/master/node/internal/node_binary.bzl#L142-L144

    But yeah, I get that it deserves a clean solution.

  4. pcj commented on Oct 26, 2017

    @pcj
    Contributor

    I believe this should be fixed by #41. Please re-open if remains unresolved.

  5. markushandell commented on Nov 20, 2017

    @markushandell

    I can confirm that the CWD issue is fixed with 0.4.1, thanks @pcj! Will now try to go for a sandbox-safe solution as advised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions