Search code examples
bazelbazel-rules-nodejs

`Bazel run` won't find static file dependency in bazel-bin when running nodejs_image


I am trying to get a rules docker nodejs_image to run using bazel.

My command is

bazel run :image.binary

Here is my rule:

load("@npm//@bazel/typescript:index.bzl", "ts_project")
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")

ts_project(
    name = "typescript_build",
    srcs = glob([
        "src/**/*",
    ]),
    allow_js = True,
    out_dir = "build",
    deps = ["@npm//:node_modules"],
)

nodejs_image(
    name = "image",
    data = [
        ":package.json",
        ":typescript_build",
        "@npm//:node_modules",
    ],
    entry_point = "build/app.js",
)

Basically, I need the package.json file because it includes some important configuration information when Node executes. If I call bazel build :image and then grab/run that image, everything works fine. But if I call bazel run :image it will basically work except that it can't find the package.json.

When I check the bazel-bin/ folder, I've noticed that the package.json isn't included, but the built typescript and node_modules are. I'm guessing because I'm not running any prior rules on the package.json, it doesn't get added to the bin, but I really don't know how to work around this.


Solution

  • So, basically, it seems that if you just use a rule like copy_to_bin or js_library, I think their purpose is to help put static files into your bazel-bin.

    https://bazelbuild.github.io/rules_nodejs/Built-ins.html#copy_to_bin

    ts_project(
        name = "typescript_build",
        srcs = glob([
            "src/**/*",
        ]),
        allow_js = True,
        out_dir = "build",
        deps = ["@npm//:node_modules"],
    )
    
    js_library(
        name = "library",
        srcs = ["package.json"],
        deps = [":typescript_build"],
    )
    
    nodejs_image(
        name = "image",
        data = [
            ":library",
            "@npm//:node_modules",
        ],
        entry_point = "build/app.js",
    )