Skip to content

Commit 7106d86

Browse files
philscdbaileychess
andauthored
Remove npm/rules_js dependency for C++ only use cases (#7990)
When flatbuffers is being used from a project that has no use for JavaScript, users encounter an error similar to the following: ERROR: Skipping '@com_github_google_flatbuffers//:flatbuffers': error loading package '@com_github_google_flatbuffers//': Unable to find package for @npm//:defs.bzl: The repository '@npm' could not be resolved: Repository '@npm' is not defined. WARNING: Target pattern parsing failed. ERROR: error loading package '@com_github_google_flatbuffers//': Unable to find package for @npm//:defs.bzl: The repository '@npm' could not be resolved: Repository '@npm' is not defined. INFO: Elapsed time: 0.023s INFO: 0 processes. FAILED: Build did NOT complete successfully (0 packages loaded) currently loading: @com_github_google_flatbuffers// That's not ideal. Users that only care about C++ for example shouldn't be forced to deal with rules_js and friends. This patch attempts to fix that by moving the rules_js-specific things into the `ts` and `tests/ts` directories. This should allow non-JavaScript projects to ignore rules_js and friends completely. Here I basically followed the `rules_foo` example from rules_js: https://github.com/aspect-build/rules_js/tree/main/e2e/rules_foo The idea is that flatbuffers has its own npm dependencies regardless of what other projects may have. This means we should not force the user to import flatbuffers's npm dependencies. The new `ts/repositories.bzl` file is used by dependents to import flatbuffers's dependencies. They can still import their own dependencies. This cleanup allowed me to move all JavaScript-specific stuff from the top-level directory into subdirectories. There should be no changes in this patch in terms of functionality. It's just a refactor of the rules_js call sites. Users will have to add a call to the function in `ts/repositories.bzl` in their own `WORKSPACE` file. They can use `tests/ts/bazel_repository_test/WORKSPACE` as an example. Co-authored-by: Derek Bailey <derekbailey@google.com>
1 parent da64720 commit 7106d86

23 files changed

+213
-59
lines changed

.bazelignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules
1+
ts/node_modules

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# We cannot use "common" here because the "version" command doesn't support
22
# --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
3+
build --deleted_packages=tests/bazel_repository_test_dir,tests/ts/bazel_repository_test_dir
4+
query --deleted_packages=tests/bazel_repository_test_dir,tests/ts/bazel_repository_test_dir

BUILD.bazel

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
2-
load("@npm//:defs.bzl", "npm_link_all_packages")
31
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
42

53
licenses(["notice"])
@@ -8,13 +6,6 @@ package(
86
default_visibility = ["//visibility:public"],
97
)
108

11-
npm_link_all_packages(name = "node_modules")
12-
13-
npm_link_package(
14-
name = "node_modules/flatbuffers",
15-
src = "//ts:flatbuffers",
16-
)
17-
189
exports_files([
1910
"LICENSE",
2011
"tsconfig.json",
@@ -37,9 +28,13 @@ config_setting(
3728
filegroup(
3829
name = "distribution",
3930
srcs = [
31+
".bazelignore",
32+
".npmrc",
4033
"BUILD.bazel",
4134
"WORKSPACE",
4235
"build_defs.bzl",
36+
"package.json",
37+
"pnpm-lock.yaml",
4338
"typescript.bzl",
4439
"//grpc/src/compiler:distribution",
4540
"//reflection:distribution",

WORKSPACE

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ http_archive(
1111
],
1212
)
1313

14+
# Import our own version of skylib before other rule sets (e.g. rules_swift)
15+
# has a chance to import an old version.
16+
http_archive(
17+
name = "bazel_skylib",
18+
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa",
19+
urls = [
20+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz",
21+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz",
22+
],
23+
)
24+
25+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
26+
27+
bazel_skylib_workspace()
28+
1429
http_archive(
1530
name = "build_bazel_rules_apple",
1631
sha256 = "34c41bfb59cdaea29ac2df5a2fa79e5add609c71bb303b2ebb10985f93fa20e7",
@@ -101,7 +116,7 @@ load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
101116

102117
rules_js_dependencies()
103118

104-
load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock", "pnpm_repository")
119+
load("@aspect_rules_js//npm:npm_import.bzl", "pnpm_repository")
105120

106121
pnpm_repository(name = "pnpm")
107122

@@ -129,17 +144,13 @@ nodejs_register_toolchains(
129144
node_version = DEFAULT_NODE_VERSION,
130145
)
131146

132-
npm_translate_lock(
133-
name = "npm",
134-
npmrc = "//:.npmrc",
135-
pnpm_lock = "//:pnpm-lock.yaml",
136-
# Set this to True when the lock file needs to be updated, commit the
137-
# changes, then set to False again.
138-
update_pnpm_lock = False,
139-
verify_node_modules_ignored = "//:.bazelignore",
147+
load("@com_github_google_flatbuffers//ts:repositories.bzl", "flatbuffers_npm")
148+
149+
flatbuffers_npm(
150+
name = "flatbuffers_npm",
140151
)
141152

142-
load("@npm//:repositories.bzl", "npm_repositories")
153+
load("@flatbuffers_npm//:repositories.bzl", "npm_repositories")
143154

144155
npm_repositories()
145156

tests/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
22
load("@rules_cc//cc:defs.bzl", "cc_test")
33
load("//:build_defs.bzl", "DEFAULT_FLATC_ARGS", "flatbuffer_cc_library")
4+
load(":defs.bzl", "flatbuffers_as_external_repo_test")
45

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

8+
exports_files([
9+
"bazel_repository_test_template.sh",
10+
])
11+
712
# rules_js works around various JS tooling limitations by copying everything
813
# into the output directory. Make the test data available to the tests this way.
914
copy_to_bin(
@@ -264,3 +269,8 @@ flatbuffer_cc_library(
264269
name = "alignment_test_cc_fbs",
265270
srcs = ["alignment_test.fbs"],
266271
)
272+
273+
flatbuffers_as_external_repo_test(
274+
name = "bazel_repository_test",
275+
directory = "bazel_repository_test_dir",
276+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --symlink_prefix=/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This test doesn't actually make use of the flatbuffers library. It's just
2+
# here to make sure we can link the library properly when it comes from an
3+
# external repository. You're welcome to expand this test to do more.
4+
cc_test(
5+
name = "pulls_in_flatbuffers_test",
6+
srcs = ["pulls_in_flatbuffers_test.cpp"],
7+
deps = [
8+
"@com_github_google_flatbuffers//:flatbuffers",
9+
],
10+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This directory is not intended to be used independently of the flatbuffers
2+
repository. Instead, this whole directory serves as a unit test for the
3+
C++ integration in the flatbuffers repo.
4+
5+
Run this test from the top-level of the flatbuffers repo.
6+
```console
7+
$ bazel test //tests:bazel_repository_test
8+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
workspace(name = "bazel_repository_test")
2+
3+
local_repository(
4+
name = "com_github_google_flatbuffers",
5+
path = "../../",
6+
)

0 commit comments

Comments
 (0)