Skip to content

Commit c1e7aee

Browse files
philscdbaileychess
andauthoredMay 3, 2023
Migrate from rules_nodejs to rules_js/rules_ts (take 2) (#7928)
* Migrate from rules_nodejs to rules_js/rules_ts (take 2) This is the second version of patch #7923. The first version got reverted because bazel query was failing: $ bazel --nosystem_rc --nohome_rc query tests(set('//...')) except tests(attr("tags", "manual", set('//...'))) ERROR: Traceback (most recent call last): File "/workdir/tests/ts/bazel_repository_test_dir/BUILD", line 6, column 22, in <toplevel> npm_link_all_packages(name = "node_modules") File "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/external/npm/defs.bzl", line 188, column 13, in npm_link_all_packages fail(msg) Error in fail: The npm_link_all_packages() macro loaded from @npm//:defs.bzl and called in bazel package 'tests/ts/bazel_repository_test_dir' may only be called in bazel packages that correspond to the pnpm root package '' and pnpm workspace projects '' This was happening because the `.bazelrc` file only added `--deleted_packages` to the `build` command. We also need it for the `query` command. This second version of the patch fixes that. Original commit message: This patch migrates the current use of rules_nodejs to the new rules_js. rules_js is the intended replacement of rules_nodejs as per this note: https://github.com/aspect-build/rules_js#relationship-to-rules_nodejs > rules_js is an alternative to the build_bazel_rules_nodejs Bazel module > and accompanying npm packages hosted in > https://github.com/bazelbuild/rules_nodejs, which is now > unmaintained. All users are recommended to use rules_js instead. There are a few notable changes in this patch: 1. The `flatbuffer_ts_library` macro no longer accepts a `package_name` attribute. This is because rules_js appears to manage the import naming of dependencies via top-level `npm_link_package` targets. Users will have to migrate. 2. I added a few more arguments to `flatbuffer_library_public()`. These helped with exposing esbuild to `ts/compile_flat_file.sh`. 3. I pinned the version of `typescript` in `package.json` so that rules_ts can download the exact same version. rules_ts doesn't know what to do if the version isn't exact. 4. Since rules_js uses the pnpm locking mechanism, we now have a `pnpm-lock.yaml` file instead of a yarn lock file. 4. I added bazel targets for a few of the existing tests in `tests/ts`. They can be run with `bazel test //test/ts:all`. Since there is no flexbuffers bazel target, I did not add a bazel target for the corresponding test. 5. I added a separate workspace in `tests/ts/bazel_repository_test_dir/` to validate that the flatbuffers code can be imported as an external repository. You can run the test with `bazel test //test/ts:bazel_repository_test`. For this to work, I needed to expose a non-trivial chunk of the flatbuffers code to the test. I achieved this through some recursive `distribution` filegroups. This is inspired by rules_python's workspace tests. I did not do anything special to validate that the `gen_reflections` parameter works the same. This patch doesn't change anything about the TypeScript generation. As a side note: I am not an expert with rules_js. This patch is my attempt based on my limited understanding of the rule set. Fixes #7817 * Fix the query --------- Co-authored-by: Derek Bailey <derekbailey@google.com>
1 parent 75143f8 commit c1e7aee

35 files changed

+1700
-1214
lines changed
 

Diff for: ‎.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: ‎.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# We cannot use "common" here because the "version" command doesn't support
2+
# --deleted_packages. We need to specify it for both build and query instead.
3+
build --deleted_packages=tests/ts/bazel_repository_test_dir
4+
query --deleted_packages=tests/ts/bazel_repository_test_dir

Diff for: ‎.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hoist=false

Diff for: ‎BUILD.bazel

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
2+
load("@npm//:defs.bzl", "npm_link_all_packages")
13
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
24

35
licenses(["notice"])
@@ -6,6 +8,13 @@ package(
68
default_visibility = ["//visibility:public"],
79
)
810

11+
npm_link_all_packages(name = "node_modules")
12+
13+
npm_link_package(
14+
name = "node_modules/flatbuffers",
15+
src = "//ts:flatbuffers",
16+
)
17+
918
exports_files([
1019
"LICENSE",
1120
"tsconfig.json",
@@ -25,6 +34,23 @@ config_setting(
2534
],
2635
)
2736

37+
filegroup(
38+
name = "distribution",
39+
srcs = [
40+
"BUILD.bazel",
41+
"WORKSPACE",
42+
"build_defs.bzl",
43+
"typescript.bzl",
44+
"//grpc/src/compiler:distribution",
45+
"//reflection:distribution",
46+
"//src:distribution",
47+
"//ts:distribution",
48+
] + glob([
49+
"include/flatbuffers/*.h",
50+
]),
51+
visibility = ["//visibility:public"],
52+
)
53+
2854
# Public flatc library to compile flatbuffer files at runtime.
2955
cc_library(
3056
name = "flatbuffers",

Diff for: ‎WORKSPACE

+67-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "com_github_google_flatbuffers")
22

3-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
44

55
http_archive(
66
name = "platforms",
@@ -76,30 +76,80 @@ load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
7676
grpc_extra_deps()
7777

7878
# rules_go from https://github.com/bazelbuild/rules_go/releases/tag/v0.34.0
79+
80+
http_archive(
81+
name = "aspect_rules_js",
82+
sha256 = "124ed29fb0b3d0cba5b44f8f8e07897cf61b34e35e33b1f83d1a943dfd91b193",
83+
strip_prefix = "rules_js-1.24.0",
84+
url = "https://github.com/aspect-build/rules_js/releases/download/v1.24.0/rules_js-v1.24.0.tar.gz",
85+
)
86+
87+
load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
88+
89+
rules_js_dependencies()
90+
91+
load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock", "pnpm_repository")
92+
93+
pnpm_repository(name = "pnpm")
94+
7995
http_archive(
80-
name = "build_bazel_rules_nodejs",
81-
sha256 = "965ee2492a2b087cf9e0f2ca472aeaf1be2eb650e0cfbddf514b9a7d3ea4b02a",
82-
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.2.0/rules_nodejs-5.2.0.tar.gz"],
96+
name = "aspect_rules_ts",
97+
sha256 = "8eb25d1fdafc0836f5778d33fb8eaac37c64176481d67872b54b0a05de5be5c0",
98+
strip_prefix = "rules_ts-1.3.3",
99+
url = "https://github.com/aspect-build/rules_ts/releases/download/v1.3.3/rules_ts-v1.3.3.tar.gz",
83100
)
84101

85-
load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies")
102+
load("@aspect_rules_ts//ts:repositories.bzl", "rules_ts_dependencies")
86103

87-
build_bazel_rules_nodejs_dependencies()
104+
rules_ts_dependencies(
105+
# Since rules_ts doesn't always have the newest integrity hashes, we
106+
# compute it manually here.
107+
# $ curl --silent https://registry.npmjs.org/typescript/5.0.4 | jq ._integrity
108+
ts_integrity = "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==",
109+
ts_version_from = "//:package.json",
110+
)
88111

89-
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")
112+
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")
90113

91-
node_repositories()
114+
nodejs_register_toolchains(
115+
name = "nodejs",
116+
node_version = DEFAULT_NODE_VERSION,
117+
)
92118

93-
yarn_install(
119+
npm_translate_lock(
94120
name = "npm",
95-
exports_directories_only = False,
96-
# Unfreeze to add/remove packages.
97-
frozen_lockfile = False,
98-
package_json = "//:package.json",
99-
symlink_node_modules = False,
100-
yarn_lock = "//:yarn.lock",
121+
npmrc = "//:.npmrc",
122+
pnpm_lock = "//:pnpm-lock.yaml",
123+
# Set this to True when the lock file needs to be updated, commit the
124+
# changes, then set to False again.
125+
update_pnpm_lock = False,
126+
verify_node_modules_ignored = "//:.bazelignore",
101127
)
102128

103-
load("@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
129+
load("@npm//:repositories.bzl", "npm_repositories")
104130

105-
esbuild_repositories(npm_repository = "npm")
131+
npm_repositories()
132+
133+
http_archive(
134+
name = "aspect_rules_esbuild",
135+
sha256 = "2ea31bd97181a315e048be693ddc2815fddda0f3a12ca7b7cc6e91e80f31bac7",
136+
strip_prefix = "rules_esbuild-0.14.4",
137+
url = "https://github.com/aspect-build/rules_esbuild/releases/download/v0.14.4/rules_esbuild-v0.14.4.tar.gz",
138+
)
139+
140+
# Register a toolchain containing esbuild npm package and native bindings
141+
load("@aspect_rules_esbuild//esbuild:repositories.bzl", "LATEST_VERSION", "esbuild_register_toolchains")
142+
143+
esbuild_register_toolchains(
144+
name = "esbuild",
145+
esbuild_version = LATEST_VERSION,
146+
)
147+
148+
http_file(
149+
name = "bazel_linux_x86_64",
150+
downloaded_file_path = "bazel",
151+
sha256 = "e89747d63443e225b140d7d37ded952dacea73aaed896bca01ccd745827c6289",
152+
urls = [
153+
"https://github.com/bazelbuild/bazel/releases/download/6.1.2/bazel-6.1.2-linux-x86_64",
154+
],
155+
)

Diff for: ‎build_defs.bzl

+14-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def flatbuffer_library_public(
4848
restricted_to = None,
4949
target_compatible_with = None,
5050
flatc_path = "@com_github_google_flatbuffers//:flatc",
51-
output_to_bindir = False):
51+
output_to_bindir = False,
52+
tools = None,
53+
extra_env = None,
54+
**kwargs):
5255
"""Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler.
5356
5457
Args:
@@ -73,6 +76,11 @@ def flatbuffer_library_public(
7376
to use.
7477
flatc_path: Bazel target corresponding to the flatc compiler to use.
7578
output_to_bindir: Passed to genrule for output to bin directory.
79+
tools: Optional, passed to genrule for list of tools to make available
80+
during the action.
81+
extra_env: Optional, must be a string of "VAR1=VAL1 VAR2=VAL2". These get
82+
set as environment variables that "flatc_path" sees.
83+
**kwargs: Passed to the underlying genrule.
7684
7785
7886
This rule creates a filegroup(name) with all generated source files, and
@@ -83,6 +91,8 @@ def flatbuffer_library_public(
8391
include_paths = default_include_paths(flatc_path)
8492
include_paths_cmd = ["-I %s" % (s) for s in include_paths]
8593

94+
extra_env = extra_env or ""
95+
8696
# '$(@D)' when given a single source target will give the appropriate
8797
# directory. Appending 'out_prefix' is only necessary when given a build
8898
# target with multiple sources.
@@ -92,7 +102,7 @@ def flatbuffer_library_public(
92102
genrule_cmd = " ".join([
93103
"SRCS=($(SRCS));",
94104
"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
95-
"OUTPUT_FILE=\"$(OUTS)\" $(location %s)" % (flatc_path),
105+
"OUTPUT_FILE=\"$(OUTS)\" %s $(location %s)" % (extra_env, flatc_path),
96106
" ".join(include_paths_cmd),
97107
" ".join(flatc_args),
98108
language_flag,
@@ -105,12 +115,13 @@ def flatbuffer_library_public(
105115
srcs = srcs + includes,
106116
outs = outs,
107117
output_to_bindir = output_to_bindir,
108-
tools = [flatc_path],
118+
tools = (tools or []) + [flatc_path],
109119
cmd = genrule_cmd,
110120
compatible_with = compatible_with,
111121
target_compatible_with = target_compatible_with,
112122
restricted_to = restricted_to,
113123
message = "Generating flatbuffer files for %s:" % (name),
124+
**kwargs
114125
)
115126
if reflection_name:
116127
reflection_genrule_cmd = " ".join([

Diff for: ‎grpc/src/compiler/BUILD.bazel

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ package(
44
default_visibility = ["//visibility:public"],
55
)
66

7+
filegroup(
8+
name = "distribution",
9+
srcs = [
10+
"BUILD.bazel",
11+
] + glob([
12+
"*.cc",
13+
"*.h",
14+
]),
15+
)
16+
717
filegroup(
818
name = "common_headers",
919
srcs = [

Diff for: ‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@
3636
"homepage": "https://google.github.io/flatbuffers/",
3737
"dependencies": {},
3838
"devDependencies": {
39-
"@bazel/typescript": "5.2.0",
4039
"@types/node": "18.15.11",
4140
"@typescript-eslint/eslint-plugin": "^5.57.0",
4241
"@typescript-eslint/parser": "^5.57.0",
4342
"esbuild": "^0.17.14",
4443
"eslint": "^8.37.0",
45-
"typescript": "^5.0.3"
44+
"typescript": "5.0.4"
4645
}
4746
}

Diff for: ‎pnpm-lock.yaml

+1,184
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎reflection/BUILD.bazel

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
filegroup(
2+
name = "distribution",
3+
srcs = [
4+
"BUILD.bazel",
5+
"reflection.fbs",
6+
],
7+
visibility = ["//visibility:public"],
8+
)
9+
110
filegroup(
211
name = "reflection_fbs_schema",
312
srcs = ["reflection.fbs"],

Diff for: ‎reflection/ts/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ genrule(
99

1010
flatbuffer_ts_library(
1111
name = "reflection_ts_fbs",
12-
package_name = "flatbuffers_reflection",
1312
srcs = [":reflection.fbs"],
1413
visibility = ["//visibility:public"],
1514
)

Diff for: ‎src/BUILD.bazel

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ package(
55
default_visibility = ["//visibility:private"],
66
)
77

8+
filegroup(
9+
name = "distribution",
10+
srcs = [
11+
"BUILD.bazel",
12+
] + glob([
13+
"*.cpp",
14+
"*.h",
15+
]),
16+
visibility = ["//visibility:public"],
17+
)
18+
819
cc_library(
920
name = "code_generators",
1021
srcs = ["code_generators.cpp"],

Diff for: ‎tests/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
12
load("@rules_cc//cc:defs.bzl", "cc_test")
23
load("//:build_defs.bzl", "flatbuffer_cc_library")
34

45
package(default_visibility = ["//visibility:private"])
56

7+
# rules_js works around various JS tooling limitations by copying everything
8+
# into the output directory. Make the test data available to the tests this way.
9+
copy_to_bin(
10+
name = "test_data_copied_to_bin",
11+
srcs = glob([
12+
"*.mon",
13+
"*.json",
14+
]),
15+
visibility = ["//tests/ts:__subpackages__"],
16+
)
17+
618
# Test binary.
719
cc_test(
820
name = "flatbuffers_test",

Diff for: ‎tests/ts/BUILD.bazel

+66
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_test")
12
load("//:typescript.bzl", "flatbuffer_ts_library")
23

34
package(default_visibility = ["//visibility:private"])
@@ -10,3 +11,68 @@ flatbuffer_ts_library(
1011
"//tests/ts/test_dir:typescript_transitive_ts_fbs",
1112
],
1213
)
14+
15+
TEST_DATA = glob([
16+
"my-game/*.js",
17+
"my-game/example/*.js",
18+
"my-game/example2/*.js",
19+
])
20+
21+
TEST_UNION_VECTOR_DATA = glob([
22+
"union_vector/*.js",
23+
])
24+
25+
TEST_COMPLEX_ARRAYS_DATA = glob([
26+
"arrays_test_complex/**/*.js",
27+
])
28+
29+
# Here we're running the tests against the checked-in generated files. These
30+
# are kept up-to-date with a CI-based mechanism. The intent of running these
31+
# tests here via bazel is not to validate that they're up-to-date. Instead, we
32+
# just want to make it easy to run these tests while making other changes. For
33+
# example, this is useful when making changes to the rules_js setup to validate
34+
# that the basic infrastructure is still working.
35+
[js_test(
36+
name = "%s_test" % test,
37+
chdir = package_name(),
38+
data = data + [
39+
"package.json",
40+
"//:node_modules/flatbuffers",
41+
"//tests:test_data_copied_to_bin",
42+
],
43+
entry_point = "%s.js" % test,
44+
) for test, data in (
45+
("JavaScriptTest", TEST_DATA),
46+
("JavaScriptUnionVectorTest", TEST_UNION_VECTOR_DATA),
47+
# TODO(philsc): Figure out how to run this test with flexbuffers available.
48+
# At the moment the flexbuffer library is not exposed as a bazel target.
49+
#("JavaScriptFlexBuffersTest", TBD_DATA)
50+
("JavaScriptComplexArraysTest", TEST_COMPLEX_ARRAYS_DATA),
51+
)]
52+
53+
sh_test(
54+
name = "bazel_repository_test",
55+
srcs = ["bazel_repository_test.sh"],
56+
data = [
57+
"//:distribution",
58+
"@bazel_linux_x86_64//file",
59+
] + glob(
60+
[
61+
"bazel_repository_test_dir/**/*",
62+
],
63+
exclude = [
64+
"bazel_repository_test_dir/bazel-*/**",
65+
],
66+
),
67+
tags = [
68+
# Since we have bazel downloading external repositories inside this
69+
# test, we need to give it access to the internet.
70+
"requires-network",
71+
],
72+
# We only have x86_64 Linux bazel exposed so restrict the test to that.
73+
target_compatible_with = [
74+
"@platforms//cpu:x86_64",
75+
"@platforms//os:linux",
76+
],
77+
deps = ["@bazel_tools//tools/bash/runfiles"],
78+
)

Diff for: ‎tests/ts/bazel_repository_test.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# This test makes sure that a separate repository can import the flatbuffers
4+
# repository and use it in their JavaScript code.
5+
6+
# --- begin runfiles.bash initialization v3 ---
7+
# Copy-pasted from the Bazel Bash runfiles library v3.
8+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
9+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
10+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
11+
source "$0.runfiles/$f" 2>/dev/null || \
12+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
13+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
14+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
15+
# --- end runfiles.bash initialization v3 ---
16+
17+
BAZEL_BIN="$(rlocation bazel_linux_x86_64/file/bazel)"
18+
readonly BAZEL_BIN
19+
20+
if [[ ! -e "${BAZEL_BIN}" ]]; then
21+
echo "Failed to find the bazel binary." >&2
22+
exit 1
23+
fi
24+
25+
export PATH="$(dirname "${BAZEL_BIN}"):${PATH}"
26+
27+
cd tests/ts/bazel_repository_test_dir/
28+
29+
bazel test //...

Diff for: ‎tests/ts/bazel_repository_test_dir/.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: ‎tests/ts/bazel_repository_test_dir/.bazelrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --symlink_prefix=/

Diff for: ‎tests/ts/bazel_repository_test_dir/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*

Diff for: ‎tests/ts/bazel_repository_test_dir/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../.npmrc

Diff for: ‎tests/ts/bazel_repository_test_dir/BUILD

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_test")
2+
load("@com_github_google_flatbuffers//:typescript.bzl", "flatbuffer_ts_library")
3+
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
4+
load("@npm//:defs.bzl", "npm_link_all_packages")
5+
6+
npm_link_all_packages(name = "node_modules")
7+
8+
npm_link_package(
9+
name = "node_modules/flatbuffers",
10+
src = "@com_github_google_flatbuffers//ts:flatbuffers",
11+
)
12+
13+
flatbuffer_ts_library(
14+
name = "one_fbs",
15+
srcs = ["one.fbs"],
16+
)
17+
18+
flatbuffer_ts_library(
19+
name = "two_fbs",
20+
srcs = ["two.fbs"],
21+
deps = [":one_fbs"],
22+
)
23+
24+
js_test(
25+
name = "import_test",
26+
data = [
27+
"package.json",
28+
":node_modules/flatbuffers",
29+
":two_fbs",
30+
],
31+
entry_point = "import_test.js",
32+
)

Diff for: ‎tests/ts/bazel_repository_test_dir/WORKSPACE

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
workspace(name = "bazel_repository_test")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
local_repository(
6+
name = "com_github_google_flatbuffers",
7+
path = "../../../",
8+
)
9+
10+
http_archive(
11+
name = "aspect_rules_js",
12+
sha256 = "124ed29fb0b3d0cba5b44f8f8e07897cf61b34e35e33b1f83d1a943dfd91b193",
13+
strip_prefix = "rules_js-1.24.0",
14+
url = "https://github.com/aspect-build/rules_js/releases/download/v1.24.0/rules_js-v1.24.0.tar.gz",
15+
)
16+
17+
load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
18+
19+
rules_js_dependencies()
20+
21+
load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock", "pnpm_repository")
22+
23+
pnpm_repository(name = "pnpm")
24+
25+
http_archive(
26+
name = "aspect_rules_ts",
27+
sha256 = "8eb25d1fdafc0836f5778d33fb8eaac37c64176481d67872b54b0a05de5be5c0",
28+
strip_prefix = "rules_ts-1.3.3",
29+
url = "https://github.com/aspect-build/rules_ts/releases/download/v1.3.3/rules_ts-v1.3.3.tar.gz",
30+
)
31+
32+
load("@aspect_rules_ts//ts:repositories.bzl", "rules_ts_dependencies")
33+
34+
rules_ts_dependencies(
35+
# curl --silent https://registry.npmjs.org/typescript/5.0.3 | jq ._integrity
36+
ts_integrity = "sha512-xv8mOEDnigb/tN9PSMTwSEqAnUvkoXMQlicOb0IUVDBSQCgBSaAAROUZYy2IcUy5qU6XajK5jjjO7TMWqBTKZA==",
37+
ts_version = "5.0.3",
38+
)
39+
40+
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")
41+
42+
nodejs_register_toolchains(
43+
name = "nodejs",
44+
node_version = DEFAULT_NODE_VERSION,
45+
)
46+
47+
npm_translate_lock(
48+
name = "npm",
49+
npmrc = "//:.npmrc",
50+
pnpm_lock = "//:pnpm-lock.yaml",
51+
verify_node_modules_ignored = "//:.bazelignore",
52+
)
53+
54+
load("@npm//:repositories.bzl", "npm_repositories")
55+
56+
npm_repositories()
57+
58+
http_archive(
59+
name = "aspect_rules_esbuild",
60+
sha256 = "2ea31bd97181a315e048be693ddc2815fddda0f3a12ca7b7cc6e91e80f31bac7",
61+
strip_prefix = "rules_esbuild-0.14.4",
62+
url = "https://github.com/aspect-build/rules_esbuild/releases/download/v0.14.4/rules_esbuild-v0.14.4.tar.gz",
63+
)
64+
65+
# Register a toolchain containing esbuild npm package and native bindings
66+
load("@aspect_rules_esbuild//esbuild:repositories.bzl", "LATEST_VERSION", "esbuild_register_toolchains")
67+
68+
esbuild_register_toolchains(
69+
name = "esbuild",
70+
esbuild_version = LATEST_VERSION,
71+
)

Diff for: ‎tests/ts/bazel_repository_test_dir/import_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import assert from 'assert'
2+
import * as flatbuffers from 'flatbuffers'
3+
4+
import two_cjs from './two_generated.cjs'
5+
6+
const bazel_repository_test = two_cjs.bazel_repository_test;
7+
8+
function main() {
9+
// Validate building a table with a table field.
10+
var fbb = new flatbuffers.Builder(1);
11+
12+
bazel_repository_test.One.startOne(fbb);
13+
bazel_repository_test.One.addInformation(fbb, 42);
14+
var one = bazel_repository_test.One.endOne(fbb);
15+
16+
bazel_repository_test.Two.startTwo(fbb);
17+
bazel_repository_test.Two.addOne(fbb, one);
18+
var two = bazel_repository_test.Two.endTwo(fbb);
19+
20+
fbb.finish(two);
21+
22+
// Call as a sanity check. Would be better to validate actual output here.
23+
fbb.asUint8Array();
24+
25+
console.log('FlatBuffers bazel repository test: completed successfully');
26+
}
27+
28+
main();

Diff for: ‎tests/ts/bazel_repository_test_dir/one.fbs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace bazel_repository_test;
2+
3+
table One {
4+
information:int;
5+
}
6+
7+
root_type One;

Diff for: ‎tests/ts/bazel_repository_test_dir/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "bazel_repository_test",
3+
"type": "module",
4+
"private": true,
5+
"devDependencies": {
6+
"@types/node": "18.15.11"
7+
}
8+
}

Diff for: ‎tests/ts/bazel_repository_test_dir/pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎tests/ts/bazel_repository_test_dir/two.fbs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include 'one.fbs';
2+
3+
namespace bazel_repository_test;
4+
5+
table Two {
6+
one:One;
7+
}
8+
9+
root_type Two;

Diff for: ‎tests/ts/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"type": "module",
33
"dependencies": {
4-
"@grpc/grpc-js": "^1.7.0",
54
"flatbuffers": "../../"
65
}
76
}

Diff for: ‎tests/ts/test_dir/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_test")
12
load("//:typescript.bzl", "flatbuffer_ts_library")
23

34
flatbuffer_ts_library(
@@ -12,3 +13,14 @@ flatbuffer_ts_library(
1213
visibility = ["//visibility:public"],
1314
deps = [":typescript_transitive_ts_fbs"],
1415
)
16+
17+
js_test(
18+
name = "import_test",
19+
chdir = package_name(),
20+
data = [
21+
"package.json",
22+
":include_ts_fbs",
23+
"//:node_modules/flatbuffers",
24+
],
25+
entry_point = "import_test.js",
26+
)

Diff for: ‎tests/ts/test_dir/import_test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import assert from 'assert'
2+
import * as flatbuffers from 'flatbuffers'
3+
4+
import typescript_include from './typescript_include_generated.cjs'
5+
6+
const foobar = typescript_include.foobar;
7+
8+
function main() {
9+
// Validate the enums.
10+
assert.strictEqual(foobar.Abc.a, 0);
11+
assert.strictEqual(foobar.class_.arguments_, 0);
12+
13+
// Validate building a table.
14+
var fbb = new flatbuffers.Builder(1);
15+
var name = fbb.createString("Foo Bar");
16+
17+
foobar.Tab.startTab(fbb);
18+
foobar.Tab.addAbc(fbb, foobar.Abc.a);
19+
foobar.Tab.addArg(fbb, foobar.class_.arguments_);
20+
foobar.Tab.addName(fbb, name);
21+
var tab = foobar.Tab.endTab(fbb);
22+
23+
fbb.finish(tab);
24+
25+
// Call as a sanity check. Would be better to validate actual output here.
26+
fbb.asUint8Array();
27+
28+
console.log('FlatBuffers Bazel Import test: completed successfully');
29+
}
30+
31+
main();

Diff for: ‎tests/ts/test_dir/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"flatbuffers": "../../../"
5+
}
6+
}

Diff for: ‎tests/ts/test_dir/typescript_include.fbs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
include 'typescript_transitive_include.fbs';
2+
23
namespace foobar;
34

45
enum class: int {
56
arguments,
67
}
8+
9+
table Tab {
10+
abc:Abc;
11+
arg:class;
12+
name:string;
13+
}

Diff for: ‎ts/BUILD.bazel

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
load("@npm//@bazel/typescript:index.bzl", "ts_project")
2-
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
1+
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
2+
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
3+
4+
filegroup(
5+
name = "distribution",
6+
srcs = [
7+
"BUILD.bazel",
8+
"compile_flat_file.sh",
9+
] + glob([
10+
"*.ts",
11+
]),
12+
visibility = ["//visibility:public"],
13+
)
14+
15+
# Add an index to emulate the top-level package.json's "main" entry.
16+
genrule(
17+
name = "generate_index.ts",
18+
outs = ["index.ts"],
19+
cmd = """echo "export * from './flatbuffers.js'" > $(OUTS)""",
20+
)
321

422
ts_project(
523
name = "flatbuffers_ts",
@@ -11,6 +29,7 @@ ts_project(
1129
"flatbuffers.ts",
1230
"types.ts",
1331
"utils.ts",
32+
":index.ts",
1433
],
1534
declaration = True,
1635
tsconfig = {
@@ -28,14 +47,19 @@ ts_project(
2847
},
2948
},
3049
visibility = ["//visibility:public"],
31-
deps = ["@npm//@types/node"],
50+
deps = [
51+
# Because the main repository instantiates the @npm repository, we need
52+
# to depend on the main repository's node import.
53+
"@//:node_modules/@types/node",
54+
],
3255
)
3356

34-
js_library(
57+
npm_package(
3558
name = "flatbuffers",
36-
package_name = "flatbuffers",
59+
srcs = [":flatbuffers_ts"],
60+
include_external_repositories = ["*"],
61+
package = "flatbuffers",
3762
visibility = ["//visibility:public"],
38-
deps = [":flatbuffers_ts"],
3963
)
4064

4165
sh_binary(
@@ -44,7 +68,6 @@ sh_binary(
4468
data = [
4569
"@com_github_google_flatbuffers//:flatc",
4670
"@nodejs_linux_amd64//:node_bin",
47-
"@npm//esbuild/bin:esbuild",
4871
],
4972
# We just depend directly on the linux amd64 nodejs binary, so only support
5073
# running this script on amd64 for now.

Diff for: ‎ts/compile_flat_file.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
1414
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
1515
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
1616
# --- end runfiles.bash initialization v2 ---
17-
set -e
17+
set -eu
1818
runfiles_export_envvars
1919
FLATC=$(rlocation com_github_google_flatbuffers/flatc)
20-
ESBUILD=$(rlocation npm/node_modules/esbuild/bin/esbuild)
2120
TS_FILE=$(${FLATC} $@ | grep "Entry point.*generated" | grep -o "bazel-out.*ts")
22-
export PATH=$(rlocation nodejs_linux_amd64/bin/nodejs/bin)
23-
${ESBUILD} ${TS_FILE} --format=cjs --bundle --outfile="${OUTPUT_FILE}" --external:flatbuffers --log-level=warning
21+
export PATH="$(rlocation nodejs_linux_amd64/bin/nodejs/bin):${PATH}"
22+
${ESBUILD_BIN} ${TS_FILE} --format=cjs --bundle --outfile="${OUTPUT_FILE}" --external:flatbuffers --log-level=warning

Diff for: ‎typescript.bzl

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Rules for building typescript flatbuffers with Bazel.
33
"""
44

5-
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
5+
load("@aspect_rules_js//js:defs.bzl", "js_library")
66
load(":build_defs.bzl", "flatbuffer_library_public")
77

88
DEFAULT_FLATC_TS_ARGS = [
@@ -24,8 +24,7 @@ def flatbuffer_ts_library(
2424
flatc_args = DEFAULT_FLATC_TS_ARGS,
2525
visibility = None,
2626
restricted_to = None,
27-
gen_reflections = False,
28-
package_name = None):
27+
gen_reflections = False):
2928
"""Generates a ts_library rule for a given flatbuffer definition.
3029
3130
Args:
@@ -46,7 +45,6 @@ def flatbuffer_ts_library(
4645
to use.
4746
gen_reflections: Optional, if true this will generate the flatbuffer
4847
reflection binaries for the schemas.
49-
package_name: Optional, Package name to use for the generated code.
5048
"""
5149
srcs_lib = "%s_srcs" % (name)
5250
out_base = [s.replace(".fbs", "").split("/")[-1].split(":")[-1] for s in srcs]
@@ -64,13 +62,16 @@ def flatbuffer_ts_library(
6462
language_flag = "--ts",
6563
includes = includes,
6664
include_paths = include_paths,
65+
extra_env = "ESBUILD_BIN=$(ESBUILD_BIN)",
6766
flatc_args = flatc_args + ["--filename-suffix _generated"],
6867
compatible_with = compatible_with,
6968
restricted_to = restricted_to,
7069
reflection_name = reflection_name,
7170
reflection_visibility = visibility,
7271
target_compatible_with = target_compatible_with,
7372
flatc_path = "@com_github_google_flatbuffers//ts:compile_flat_file",
73+
toolchains = ["@aspect_rules_esbuild//esbuild:resolved_toolchain"],
74+
tools = ["@aspect_rules_esbuild//esbuild:resolved_toolchain"],
7475
)
7576
js_library(
7677
name = name,
@@ -79,7 +80,6 @@ def flatbuffer_ts_library(
7980
restricted_to = restricted_to,
8081
target_compatible_with = target_compatible_with,
8182
srcs = outs,
82-
package_name = package_name,
8383
)
8484
native.filegroup(
8585
name = "%s_includes" % (name),

Diff for: ‎yarn.lock

-1,174
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.