Skip to content

Commit

Permalink
ci: build and run unit tests with bzlmod
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Feb 22, 2023
1 parent 37772fc commit c1cd040
Show file tree
Hide file tree
Showing 21 changed files with 281 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .bazelrc.common
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ build --incompatible_strict_action_env

# Enable with --config=release
build:release --stamp --workspace_status_command=$(pwd)/workspace_status.sh

# Config to enable building and testing with bzlmod
common:bzlmod --enable_bzlmod
common:bzlmod --//:flag_bzlmod
17 changes: 11 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ jobs:
- bazelversion: 5.1.1
bzlmodEnabled: true
# TODO: un-exclude the following bzlmod tests once they work
- folder: .
bzlmodEnabled: true
- folder: e2e/js_image_docker
bzlmodEnabled: true
- folder: e2e/js_image_oci
Expand Down Expand Up @@ -236,17 +234,24 @@ jobs:
ENGFLOW_CLIENT_CRT: ${{ secrets.ENGFLOW_CLIENT_CRT }}
ENGFLOW_PRIVATE_KEY: ${{ secrets.ENGFLOW_PRIVATE_KEY }}

- name: Set bzlmod flag
- name: Set bzlmod flag (for integration tests)
# Store the --enable_bzlmod flag that we add to the test command below
# only when we're running bzlmod in our test matrix.
id: set_bzlmod_flag
if: matrix.bzlmodEnabled
if: ${{ matrix.bzlmodEnabled && matrix.folder != '.'}}
run: echo "bzlmod_flag=--enable_bzlmod" >> $GITHUB_OUTPUT

- name: Set bzlmod configuration (for test //...)
# Store the --enable_bzlmod flag that we add to the test command below
# only when we're running bzlmod in our test matrix.
id: set_bzlmod_config
if: ${{ matrix.bzlmodEnabled && matrix.folder == '.'}}
run: echo "bzlmod_config=--config=bzlmod" >> $GITHUB_OUTPUT

- name: bazel test //...
working-directory: ${{ matrix.folder }}
run: |
bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc test --config=${{ matrix.config }} ${{ steps.set_bzlmod_flag.outputs.bzlmod_flag }} //...
bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc test --config=${{ matrix.config }} ${{ steps.set_bzlmod_flag.outputs.bzlmod_flag }} ${{ steps.set_bzlmod_config.outputs.bzlmod_config }} //...
ls $(bazel info output_base)/external | grep -v __links | grep -vz unused
env:
# Bazelisk will download bazel to here
Expand Down Expand Up @@ -274,7 +279,7 @@ jobs:
if: matrix.config == 'local'
working-directory: ${{ matrix.folder }}
run: |
bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc coverage --config=${{ matrix.config }} --instrument_test_targets //...
bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc coverage --config=${{ matrix.config }} ${{ steps.set_bzlmod_config.outputs.bzlmod_config }} --instrument_test_targets //...
env:
# Bazelisk will download bazel to here
XDG_CACHE_HOME: ~/.cache/bazel-repo
Expand Down
13 changes: 13 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@pnpm__links//:defs.bzl", npm_link_pnpm = "npm_link_imported_package")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//npm:defs.bzl", "npm_link_package")

# Link all packages from the /WORKSPACE npm_translate_lock(name = "npm") and also packages from
Expand Down Expand Up @@ -61,3 +62,15 @@ buildifier(
lint_mode = "warn",
mode = "diff",
)

bool_flag(
name = "flag_bzlmod",
build_setting_default = False,
)

config_setting(
name = "bzlmod",
flag_values = {
":flag_bzlmod": "true",
},
)
131 changes: 131 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ bazel_dep(name = "aspect_bazel_lib", version = "1.27.0")
bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "rules_nodejs", version = "5.8.0")
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "gazelle", version = "0.28.0", repo_name = "bazel_gazelle")
bazel_dep(name = "buildifier_prebuilt", version = "6.0.0.1", dev_dependency = True)
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependency = True)

ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "ext")

ext.host()

use_repo(ext, "aspect_bazel_lib_host")

node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")

node.toolchain(
name = "nodejs",
node_version = "16.9.0",
)

use_repo(node, "nodejs_toolchains")
Expand All @@ -38,4 +48,125 @@ pnpm.pnpm(
)

use_repo(pnpm, "pnpm")
use_repo(pnpm, "pnpm__links")

############################################
# Example npm dependencies

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)

npm.npm_translate_lock(
name = "npm",
bins = {
# derived from "bin" attribute in node_modules/typescript/package.json
"typescript": [
"tsc=./bin/tsc",
"tsserver=./bin/tsserver",
]
},
custom_postinstalls = {
"@aspect-test/c": "echo moo > cow.txt",
"@aspect-test/c@2.0.2": "echo mooo >> cow.txt",
},
data = [
"//:examples/npm_deps/patches/meaning-of-life@1.0.0-pnpm.patch",
"//:package.json",
"//:pnpm-workspace.yaml",
"//examples/js_binary:package.json",
"//examples/macro:package.json",
"//examples/npm_deps:package.json",
"//examples/npm_package/libs/lib_a:package.json",
"//examples/npm_package/packages/pkg_a:package.json",
"//examples/npm_package/packages/pkg_b:package.json",
"//examples/webpack_cli:package.json",
"//js/private/coverage/bundle:package.json",
"//js/private/worker/src:package.json",
"//npm/private/test:package.json",
"//npm/private/test:vendored/lodash-4.17.21.tgz",
"//npm/private/test/npm_package:package.json",
"//npm/private/test/vendored/is-odd:package.json",
"//npm/private/test/vendored/semver-max:package.json",
],
generate_bzl_library_targets = True,
lifecycle_hooks = {
# we fetch @kubernetes/client-node from source and it has a `prepare` lifecycle hook that needs to be run
"@kubernetes/client-node": ["prepare"],
# 'install' hook fails as it assumes the following path to `node-pre-gyp`: ./node_modules/.bin/node-pre-gyp
# https://github.com/stultuss/protoc-gen-grpc-ts/blob/53d52a9d0e1fe3cbe930dec5581eca89b3dde807/package.json#L28
"protoc-gen-grpc@2.0.3": [],
},
lifecycle_hooks_execution_requirements = {
"*": [
"no-sandbox",
],
# If @kubernetes/client-node is not sandboxed, will fail with
# ```
# src/azure_auth.ts(97,43): error TS2575: No overload expects 2 arguments, but overloads do exist that expect either 1 or 4 arguments.
# src/azure_auth.ts(98,34): error TS2575: No overload expects 2 arguments, but overloads do exist that expect either 1 or 4 arguments.
# src/gcp_auth.ts(93,43): error TS2575: No overload expects 2 arguments, but overloads do exist that expect either 1 or 4 arguments.
# src/gcp_auth.ts(94,34): error TS2575: No overload expects 2 arguments, but overloads do exist that expect either 1 or 4 arguments.
# ```
# since a `jsonpath-plus@7.2.0` that is newer then the transitive dep `jsonpath-plus@0.19.0` is found outside of the sandbox that
# includes typings that don't match the 0.19.0 "any" usage.
"@kubernetes/client-node": [],
"@figma/nodegit": [
"no-sandbox",
# Workaround Engflow not honoring requires-network on build actions
"no-remote-exec",
"requires-network",
],
"esbuild": [
"no-sandbox",
# Workaround Engflow not honoring requires-network on build actions
"no-remote-exec",
"requires-network",
],
"segfault-handler": [
"no-sandbox",
# Workaround Engflow not honoring requires-network on build actions
"no-remote-exec",
"requires-network",
],
},
npmrc = "//:.npmrc",
patch_args = {
"*": ["-p1"],
"@gregmagolan/test-a": ["-p4"],
},
patches = {
"@gregmagolan/test-a": ["//examples/npm_deps:patches/test-a.patch"],
"@gregmagolan/test-a@0.0.1": ["//examples/npm_deps:patches/test-a@0.0.1.patch"],
"@gregmagolan/test-b": ["//examples/npm_deps:patches/test-b.patch"],
"meaning-of-life@1.0.0": ["//examples/npm_deps:patches/meaning-of-life@1.0.0-after_pnpm.patch"],
},
pnpm_lock = "//:pnpm-lock.yaml",
pnpm_version = "7.25.0",
public_hoist_packages = {
# Instructs the linker to hoist the ms@2.1.3 npm package to `node_modules/ms` in the `examples/npm_deps` package.
# Similar to adding `public-hoist-pattern[]=ms` in .npmrc but with control over which version to hoist and where
# to hoist it. This hoisted package can be referenced by the label `//examples/npm_deps:node_modules/ms` same as
# other direct dependencies in the `examples/npm_deps/package.json`.
"ms@2.1.3": ["examples/npm_deps"],
},
update_pnpm_lock = True,
verify_node_modules_ignored = "//:.bazelignore",
verify_patches = "//examples/npm_deps/patches:patches",
)

use_repo(npm, "npm", "npm__rollup__2.70.2", "npm__webpack-bundle-analyzer__4.5.0__bufferutil_4.0.7")

# As an example, manually import a package using explicit coordinates.
# Just a demonstration of the syntax de-sugaring.
npm.npm_import(
name = "acorn__8.4.0",
bins = {"acorn": "./bin/acorn"},
integrity = "sha512-ULr0LDaEqQrMFGyQ3bhJkLsbtrQ8QibAseGZeaSUiT/6zb9IvIkomWHJIvgvwad+hinRAgsI51JcWk2yvwyL+w==",
package = "acorn",
# Root package where to link the virtual store
root_package = "",
version = "8.4.0",
)

use_repo(npm, "acorn__8.4.0")
use_repo(npm, "acorn__8.4.0__links")

Empty file added WORKSPACE.bzlmod
Empty file.
53 changes: 52 additions & 1 deletion docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,97 @@ load("@aspect_bazel_lib//lib:docs.bzl", "stardoc_with_diff_test", "update_docs")
stardoc_with_diff_test(
name = "js_binary",
bzl_library_target = "//js/private:js_binary",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "js_library",
bzl_library_target = "//js/private:js_library",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "js_run_binary",
bzl_library_target = "//js/private:js_run_binary",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "js_run_devserver",
bzl_library_target = "//js/private:js_run_devserver",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "js_filegroup",
bzl_library_target = "//js/private:js_filegroup",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "js_image_layer",
bzl_library_target = "//js/private:js_image_layer",
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "npm_package",
bzl_library_target = "//npm/private:npm_package",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "npm_link_package",
bzl_library_target = "//npm/private:npm_link_package",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

stardoc_with_diff_test(
name = "npm_import",
bzl_library_target = "//npm:npm_import",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

update_docs(name = "update")
update_docs(
name = "update",
# https://github.com/bazelbuild/stardoc/pull/141
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
1 change: 1 addition & 0 deletions e2e/pnpm_workspace/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")

node.toolchain(
name = "nodejs",
node_version = "16.9.0",
)

use_repo(node, "nodejs_toolchains")
Expand Down
1 change: 1 addition & 0 deletions e2e/pnpm_workspace_deps/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")

node.toolchain(
name = "nodejs",
node_version = "16.9.0",
)

use_repo(node, "nodejs_toolchains")
Expand Down
1 change: 1 addition & 0 deletions e2e/pnpm_workspace_rerooted/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")

node.toolchain(
name = "nodejs",
node_version = "16.9.0",
)

use_repo(node, "nodejs_toolchains")
Expand Down
12 changes: 11 additions & 1 deletion examples/js_library/two/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ bzl_library(
stardoc_with_diff_test(
name = "tsc-docs",
bzl_library_target = ":tsc",
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

update_docs(name = "docs")
update_docs(
name = "docs",
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
12 changes: 11 additions & 1 deletion examples/macro/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ bzl_library(
stardoc_with_diff_test(
name = "mocha-docs",
bzl_library_target = ":mocha",
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

update_docs(name = "docs")
update_docs(
name = "docs",
target_compatible_with = select({
"//:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
Loading

0 comments on commit c1cd040

Please sign in to comment.