Skip to content

Commit

Permalink
Fix npm setup
Browse files Browse the repository at this point in the history
Setting root_package in MODULE.bazel didn't work. So we are moving the
node_modules folder to the package root. As the BUILD file loads
@flatbuffers_npm we also need to set those transitive dependencies in
the WORKSPACE version of tests/bazel_repository_test_dir (Bzlmod will
resolve this transitively).
  • Loading branch information
mering committed Jan 23, 2025
1 parent ad6d663 commit 0c32245
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ts/node_modules
node_modules
3 changes: 3 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@flatbuffers_npm//:defs.bzl", "npm_link_all_packages")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

licenses(["notice"])
Expand Down Expand Up @@ -126,3 +127,5 @@ cc_library(
linkstatic = 1,
strip_include_prefix = "/include",
)

npm_link_all_packages(name = "node_modules")
8 changes: 8 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@ bazel_dep(
version = "1.7.1",
)

pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
pnpm.pnpm(name = "pnpm")

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm")
npm.npm_translate_lock(
name = "flatbuffers_npm",
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
# Set this to True when the lock file needs to be updated, commit the
# changes, then set to False again.
# Alternatively, run:
# $ bazel run -- @pnpm//:pnpm --dir $PWD install --lockfile-only
update_pnpm_lock = False,
verify_node_modules_ignored = "//:.bazelignore",
)
use_repo(npm, "flatbuffers_npm")
Expand Down
23 changes: 23 additions & 0 deletions tests/bazel_repository_test_dir/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
workspace(name = "bazel_repository_test")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "com_github_google_flatbuffers",
path = "../../",
)

http_archive(
name = "aspect_rules_js",
sha256 = "124ed29fb0b3d0cba5b44f8f8e07897cf61b34e35e33b1f83d1a943dfd91b193",
strip_prefix = "rules_js-1.24.0",
url = "https://github.com/aspect-build/rules_js/releases/download/v1.24.0/rules_js-v1.24.0.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@com_github_google_flatbuffers//ts:repositories.bzl", "flatbuffers_npm")

flatbuffers_npm(
name = "flatbuffers_npm",
)

load("@flatbuffers_npm//:repositories.bzl", flatbuffers_npm_repositories = "npm_repositories")

flatbuffers_npm_repositories()
5 changes: 1 addition & 4 deletions ts/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@flatbuffers_npm//:npm_link_all_packages.bzl", "npm_link_all_packages")

filegroup(
name = "distribution",
Expand All @@ -14,8 +13,6 @@ filegroup(
visibility = ["//visibility:public"],
)

npm_link_all_packages(name = "node_modules")

# Add an index to emulate the top-level package.json's "main" entry.
genrule(
name = "generate_index.ts",
Expand Down Expand Up @@ -52,7 +49,7 @@ ts_project(
},
visibility = ["//visibility:public"],
deps = [
":node_modules/@types/node",
"//:node_modules/@types/node",
],
)

Expand Down
4 changes: 0 additions & 4 deletions ts/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ def flatbuffers_npm(name):
name = name,
npmrc = "@com_github_google_flatbuffers//:.npmrc",
pnpm_lock = "@com_github_google_flatbuffers//:pnpm-lock.yaml",
# Override the Bazel package where pnpm-lock.yaml is located and link
# to the specified package instead.
root_package = "ts",
# Set this to True when the lock file needs to be updated, commit the
# changes, then set to False again.
# Alternatively, run:
# $ bazel run -- @pnpm//:pnpm --dir $PWD install --lockfile-only
update_pnpm_lock = False,
verify_node_modules_ignored = "@com_github_google_flatbuffers//:.bazelignore",
defs_bzl_filename = "npm_link_all_packages.bzl",
data = [
"@com_github_google_flatbuffers//:package.json",
],
Expand Down

0 comments on commit 0c32245

Please sign in to comment.