3

I want to compile some Typescript files and create a Node.js Image from the compiled Javascript files. It works fine when I have only one Typescript file with this BUILD file:

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "compile",
    srcs = ["src/index.ts"],
)

filegroup(
    name = "files",
    srcs = ["compile"],
    output_group = "es5_sources",
)

nodejs_image(
    name = "server",
    entry_point = "files",
)

But as soon as I introduce multiple Typescript files like this:

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "compile",
    srcs = ["src/index.ts", "src/util.ts"],
)

filegroup(
    name = "files",
    srcs = ["compile"],
    output_group = "es5_sources",
)

nodejs_image(
    name = "server",
    data = ["files"],
    entry_point = "files/src/index.js",
)

Bazel doesn't find the entrypoint file:

ERROR: missing input file '//server:files/src/index.js'
ERROR: /home/flo/Desktop/my-own-minimal-bazel/server/BUILD:15:1: //server:server: missing input file '//server:files/src/index.js'
Target //server:server failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/flo/Desktop/my-own-minimal-bazel/server/BUILD:15:1 1 input file(s) do not exist

3 Answers 3

6
+100

I can only get the repo to build if I break up the ts_library into two seperate ts_libs, one for the library, and one for the index.ts:

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "lib",
    srcs = [
        "util.ts",
    ],
)

ts_library(
    name = "main",
    srcs = [
        "index.ts",
    ],
    deps = [":lib"],
)

filegroup(
    name = "libfiles",
    srcs = ["lib"],
    output_group = "es5_sources",
)

filegroup(
    name = "mainfile",
    srcs = ["main"],
    output_group = "es5_sources",
)

nodejs_image(
    name = "server",
    data = [
        ":libfiles",
        ":mainfile",
    ],
    entry_point = ":mainfile",
)

The above code is in a pull request to your repo. I'm not sure why you are unable to reference a single file of the filegroup!

Sign up to request clarification or add additional context in comments.

4 Comments

that works, but the dist folder isn't build by Bazel. I would need to run tsc --build manually. Is it possible to build the Typescript files with Bazel into the dist folder?
ohhh I see, I assumed that bazel was building that folder for you based off the tsconfig. Trying again.
I forgot to put the dist folder into my .gitignore. My fault
It works 🎉 Do you know, why it has this weird behavior?
0

(Blind fix, I cannot try it now.)

I believe you need to use entry_point = "src/index.js", because files is just the name of the filegroup, it's not part of the paths.

Either that, or entry_point = "server/src/index.js", in case nodejs_image expects a package-root-relative path.

4 Comments

entry_point = "src/index.js" produces: ERROR: missing input file '//server:src/index.js' and entry_point = "server/src/index.js" outputs ERROR: missing input file '//server:server/src/index.js'
If you want to try it, you can checkout this repo: github.com/flolude/minimal-bazel-monorepo
Have you found the issue?
No, i have not.
0

You should be able to fix this with the $(location LABEL) make variable pointed at the output file by name, src/index.js:

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "compile",
    srcs = ["src/index.ts", "src/util.ts"],
)

filegroup(
    name = "files",
    srcs = ["compile"],
    output_group = "es5_sources",
)

nodejs_image(
    name = "server",
    data = ["files"],
    entry_point = "$(location src/index.js)",
)

Note the entry_point is set to "$(location src/index.js)" - this locates the output file by relative path, relative to the BUILD file, that the files filegroup has collected.

3 Comments

When I use the code from your answer, I get this error: Error: Cannot find module 'example/server/$(location src/index.js)'. Please verify that the package.json has a valid "main" entry
Are you sure you've only modified the entry_point line? I just sent a PR github.com/flolude/minimal-bazel-monorepo/pull/2, that branch builds for me locally with bazel build //...
Yeah, it builds. But it doesn't run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.