forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove npm/rules_js dependency for C++ only use cases (google#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>
- Loading branch information
1 parent
da64720
commit 7106d86
Showing
23 changed files
with
213 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
node_modules | ||
ts/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# We cannot use "common" here because the "version" command doesn't support | ||
# --deleted_packages. We need to specify it for both build and query instead. | ||
build --deleted_packages=tests/ts/bazel_repository_test_dir | ||
query --deleted_packages=tests/ts/bazel_repository_test_dir | ||
build --deleted_packages=tests/bazel_repository_test_dir,tests/ts/bazel_repository_test_dir | ||
query --deleted_packages=tests/bazel_repository_test_dir,tests/ts/bazel_repository_test_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build --symlink_prefix=/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bazel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This test doesn't actually make use of the flatbuffers library. It's just | ||
# here to make sure we can link the library properly when it comes from an | ||
# external repository. You're welcome to expand this test to do more. | ||
cc_test( | ||
name = "pulls_in_flatbuffers_test", | ||
srcs = ["pulls_in_flatbuffers_test.cpp"], | ||
deps = [ | ||
"@com_github_google_flatbuffers//:flatbuffers", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
This directory is not intended to be used independently of the flatbuffers | ||
repository. Instead, this whole directory serves as a unit test for the | ||
C++ integration in the flatbuffers repo. | ||
|
||
Run this test from the top-level of the flatbuffers repo. | ||
```console | ||
$ bazel test //tests:bazel_repository_test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
workspace(name = "bazel_repository_test") | ||
|
||
local_repository( | ||
name = "com_github_google_flatbuffers", | ||
path = "../../", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int main() { return 0; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Helper macros and rules for tests.""" | ||
|
||
load("@bazel_skylib//lib:paths.bzl", "paths") | ||
load("@bazel_skylib//rules:expand_template.bzl", "expand_template") | ||
|
||
def flatbuffers_as_external_repo_test(name, directory): | ||
"""Run all tests in a bazel workspace that imports flatbuffers as an external repository. | ||
Args: | ||
name: The name of the test target. | ||
directory: The directory in which the bazel workspace is located. This is the directory | ||
that imports flatbuffers as an external repository. | ||
""" | ||
expand_template( | ||
name = name + "__template_expansion", | ||
out = name + ".sh", | ||
substitutions = { | ||
"{{REPOSITORY_DIR}}": paths.join(native.package_name(), directory), | ||
}, | ||
template = "//tests:bazel_repository_test_template.sh", | ||
) | ||
|
||
native.sh_test( | ||
name = name, | ||
srcs = [":%s.sh" % name], | ||
data = [ | ||
"//:distribution", | ||
"@bazel_linux_x86_64//file", | ||
] + native.glob( | ||
[ | ||
directory + "/**/*", | ||
], | ||
exclude = [ | ||
directory + "/bazel-*/**", | ||
], | ||
), | ||
tags = [ | ||
# Since we have bazel downloading external repositories inside this | ||
# test, we need to give it access to the internet. | ||
"requires-network", | ||
], | ||
# We only have x86_64 Linux bazel exposed so restrict the test to that. | ||
target_compatible_with = [ | ||
"@platforms//cpu:x86_64", | ||
"@platforms//os:linux", | ||
], | ||
deps = ["@bazel_tools//tools/bash/runfiles"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
This directory is not intended to be used independently of the flatbuffers | ||
repository. Instead, this whole directory serves as a unit test for the | ||
`rules_js` integration in the flatbuffers repo. | ||
|
||
Run this test from the top-level of the flatbuffers repo. | ||
```console | ||
$ bazel test //tests/ts:bazel_repository_test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/ts/bazel_repository_test_dir/independent_deps_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This test has nothing to do with flatbuffers. It only exists to validate | ||
// that other projects can use their own set of dependencies without having to | ||
// explicitly pull in flatbuffers's dependencies. | ||
// | ||
// We pick lodash here not for any particular reason. It could be any package, | ||
// really. I chose it because it's a relatively simple package. | ||
|
||
import assert from 'node:assert/strict' | ||
|
||
import _ from 'lodash' | ||
|
||
function main() { | ||
console.log(_); | ||
assert.deepStrictEqual(_.defaults({ 'a': 1 }, { 'a': 3, 'b': 2 }), { 'a': 1, 'b': 2 }); | ||
assert.deepStrictEqual(_.partition([1, 2, 3, 4], n => n % 2), [[1, 3], [2, 4]]); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
"type": "module", | ||
"private": true, | ||
"devDependencies": { | ||
"@types/node": "18.15.11" | ||
"lodash": "4.17.21" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.