From 0c21ad9d69430a069dc4afc7723b9552eab8c676 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 9 May 2023 10:38:15 -0700 Subject: [PATCH] chore: bzlmod (#72) * chore: bzlmod * Update WORKSPACE.bzlmod * fix e2e --- .bcr/config.yml | 3 ++ .bcr/metadata.template.json | 13 +++++++ .bcr/presubmit.yml | 10 +++++ .bcr/source.template.json | 5 +++ .github/workflows/ci.yaml | 15 +++++++- .github/workflows/release_prep.sh | 2 +- MODULE.bazel | 20 ++++++++++ README.md | 2 +- e2e/{workspace => smoke}/.bazelrc | 0 e2e/smoke/BUILD | 16 ++++++++ e2e/smoke/MODULE.bazel | 18 +++++++++ e2e/smoke/README.md | 4 ++ .../WORKSPACE => smoke/WORKSPACE.bazel} | 4 +- e2e/smoke/WORKSPACE.bzlmod | 2 + e2e/{workspace => smoke}/config.js | 0 e2e/{workspace => smoke}/main.js | 0 rollup/extensions.bzl | 37 +++++++++++++++++++ 17 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 .bcr/config.yml create mode 100644 .bcr/metadata.template.json create mode 100644 .bcr/presubmit.yml create mode 100644 .bcr/source.template.json create mode 100644 MODULE.bazel rename e2e/{workspace => smoke}/.bazelrc (100%) create mode 100644 e2e/smoke/BUILD create mode 100644 e2e/smoke/MODULE.bazel create mode 100644 e2e/smoke/README.md rename e2e/{workspace/WORKSPACE => smoke/WORKSPACE.bazel} (90%) create mode 100644 e2e/smoke/WORKSPACE.bzlmod rename e2e/{workspace => smoke}/config.js (100%) rename e2e/{workspace => smoke}/main.js (100%) create mode 100644 rollup/extensions.bzl diff --git a/.bcr/config.yml b/.bcr/config.yml new file mode 100644 index 0000000..7a9e9d8 --- /dev/null +++ b/.bcr/config.yml @@ -0,0 +1,3 @@ +fixedReleaser: + login: gregmagolan + email: greg@aspect.dev diff --git a/.bcr/metadata.template.json b/.bcr/metadata.template.json new file mode 100644 index 0000000..bdeb7b4 --- /dev/null +++ b/.bcr/metadata.template.json @@ -0,0 +1,13 @@ +{ + "homepage": "https://docs.aspect.build/rules/aspect_rules_rollup", + "maintainers": [ + { + "email": "hello@aspect.dev", + "github": "aspect-build", + "name": "Aspect team" + } + ], + "repository": ["github:aspect-build/rules_rollup"], + "versions": [], + "yanked_versions": {} +} diff --git a/.bcr/presubmit.yml b/.bcr/presubmit.yml new file mode 100644 index 0000000..0027914 --- /dev/null +++ b/.bcr/presubmit.yml @@ -0,0 +1,10 @@ +bcr_test_module: + module_path: 'e2e/smoke' + matrix: + platform: ['debian10', 'macos', 'ubuntu2004', 'windows'] + tasks: + run_tests: + name: 'Run test module' + platform: ${{ platform }} + test_targets: + - '//...' diff --git a/.bcr/source.template.json b/.bcr/source.template.json new file mode 100644 index 0000000..fc31da1 --- /dev/null +++ b/.bcr/source.template.json @@ -0,0 +1,5 @@ +{ + "integrity": "**leave this alone**", + "strip_prefix": "{REPO}-{VERSION}", + "url": "https://github.com/{OWNER}/{REPO}/releases/download/{TAG}/rules_rollup-{TAG}.tar.gz" +} diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 63edbc7..4a29690 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -60,14 +60,18 @@ jobs: matrix: os: ${{ fromJSON(needs.matrix-prep-os.outputs.os) }} bazelversion: ${{ fromJSON(needs.matrix-prep-bazelversion.outputs.bazelversions) }} + bzlmodEnabled: [true, false] folder: - '.' - - 'e2e/workspace' + - 'e2e/smoke' exclude: # Don't test macos with Bazel 5 to minimize macOS minutes (billed at 10X) # https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#included-storage-and-minutes - os: macos-latest bazelversion: 5.3.2 + # Don't test bzlmod with Bazel 5 (not supported) + - bazelversion: 5.3.2 + bzlmodEnabled: true # Steps represent a sequence of tasks that will be executed as part of the job steps: @@ -101,9 +105,16 @@ jobs: # then use .bazelversion to determine which Bazel version to use run: echo "${{ matrix.bazelversion }}" > .bazelversion + - name: Set bzlmod flag + # 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 + run: echo "bzlmod_flag=--enable_bzlmod" >> $GITHUB_OUTPUT + - name: bazel test //... env: # Bazelisk will download bazel to here, ensure it is cached between runs. XDG_CACHE_HOME: ~/.cache/bazel-repo working-directory: ${{ matrix.folder }} - run: bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc test //... + run: bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc test ${{ steps.set_bzlmod_flag.outputs.bzlmod_flag }} //... diff --git a/.github/workflows/release_prep.sh b/.github/workflows/release_prep.sh index 860823e..7651260 100755 --- a/.github/workflows/release_prep.sh +++ b/.github/workflows/release_prep.sh @@ -23,5 +23,5 @@ http_archive( ) EOF -awk 'f;/--SNIP--/{f=1}' e2e/workspace/WORKSPACE +awk 'f;/--SNIP--/{f=1}' e2e/smoke/WORKSPACE.bazel echo "\`\`\`" diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..ad0f72b --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,20 @@ +"aspect-build/rules_rollup" + +module( + name = "aspect_rules_rollup", + compatibility_level = 1, + version = "0.0.0", +) + +# Lower-bound (minimum) versions of direct dependencies +bazel_dep(name = "bazel_skylib", version = "1.3.0") +# Need #397 to skip stardoc targets +bazel_dep(name = "aspect_bazel_lib", version = "1.30.2") +# Need attribute 'dev' in 'npm_package_store_internal' rule +bazel_dep(name = "aspect_rules_js", version = "1.19.0") +bazel_dep(name = "rules_nodejs", version = "5.5.3") +bazel_dep(name = "platforms", version = "0.0.5") + +rollup = use_extension("@aspect_rules_rollup//rollup:extensions.bzl", "rollup") +rollup.toolchain(name = "rollup", rollup_version = "v2.70.2") +use_repo(rollup, "rollup") diff --git a/README.md b/README.md index bc4ac7e..6e97ca0 100644 --- a/README.md +++ b/README.md @@ -14,5 +14,5 @@ copy the WORKSPACE snippet into your `WORKSPACE` file. ## Usage -See the API documentation in the [docs](docs/) folder and the example usage in the [example](example/) folder. +See the [API documentation](./docs/rollup) and the example usage in the [example](example/) folder. Note that the example also relies on code in the `/WORKSPACE` file in the root of this repo. diff --git a/e2e/workspace/.bazelrc b/e2e/smoke/.bazelrc similarity index 100% rename from e2e/workspace/.bazelrc rename to e2e/smoke/.bazelrc diff --git a/e2e/smoke/BUILD b/e2e/smoke/BUILD new file mode 100644 index 0000000..5298ad1 --- /dev/null +++ b/e2e/smoke/BUILD @@ -0,0 +1,16 @@ +load("@aspect_rules_rollup//rollup:defs.bzl", "rollup") +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@npm//:defs.bzl", "npm_link_all_packages") + +npm_link_all_packages(name = "node_modules") + +rollup( + name = "bundle", + entry_point = "main.js", + node_modules = "//:node_modules", +) + +build_test( + name = "test", + targets = [":bundle"], +) diff --git a/e2e/smoke/MODULE.bazel b/e2e/smoke/MODULE.bazel new file mode 100644 index 0000000..567a8b5 --- /dev/null +++ b/e2e/smoke/MODULE.bazel @@ -0,0 +1,18 @@ +"Bazel dependencies" +bazel_dep(name = "aspect_rules_rollup", dev_dependency = True, version = "0.0.0") +bazel_dep(name = "bazel_skylib", dev_dependency = True, version = "1.3.0") +bazel_dep(name = "aspect_rules_js", dev_dependency = True, version = "1.24.1") + +local_path_override( + module_name = "aspect_rules_rollup", + path = "../..", +) + +npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm") + +npm.npm_translate_lock( + name = "npm", + pnpm_lock = "@aspect_rules_rollup//:pnpm-lock.yaml", +) + +use_repo(npm, "npm") diff --git a/e2e/smoke/README.md b/e2e/smoke/README.md new file mode 100644 index 0000000..717ecc4 --- /dev/null +++ b/e2e/smoke/README.md @@ -0,0 +1,4 @@ +# smoke test + +This e2e exercises the repo from an end-users perpective. +It catches mistakes in our install instructions, or usages that fail when called from an "external" repository to rules_rollup. diff --git a/e2e/workspace/WORKSPACE b/e2e/smoke/WORKSPACE.bazel similarity index 90% rename from e2e/workspace/WORKSPACE rename to e2e/smoke/WORKSPACE.bazel index ae8a4ec..e613ce6 100644 --- a/e2e/workspace/WORKSPACE +++ b/e2e/smoke/WORKSPACE.bazel @@ -30,9 +30,7 @@ load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock") npm_translate_lock( name = "npm", - npmrc = "//:.npmrc", - pnpm_lock = "//:pnpm-lock.yaml", - verify_node_modules_ignored = "//:.bazelignore", + pnpm_lock = "@aspect_rules_rollup//:pnpm-lock.yaml", ) load("@npm//:repositories.bzl", "npm_repositories") diff --git a/e2e/smoke/WORKSPACE.bzlmod b/e2e/smoke/WORKSPACE.bzlmod new file mode 100644 index 0000000..20af353 --- /dev/null +++ b/e2e/smoke/WORKSPACE.bzlmod @@ -0,0 +1,2 @@ +# Marker file that this is the root of a Bazel workspace +# This file shadows WORKSPACE.bazel when --enable_bzlmod is true diff --git a/e2e/workspace/config.js b/e2e/smoke/config.js similarity index 100% rename from e2e/workspace/config.js rename to e2e/smoke/config.js diff --git a/e2e/workspace/main.js b/e2e/smoke/main.js similarity index 100% rename from e2e/workspace/main.js rename to e2e/smoke/main.js diff --git a/rollup/extensions.bzl b/rollup/extensions.bzl new file mode 100644 index 0000000..a681e35 --- /dev/null +++ b/rollup/extensions.bzl @@ -0,0 +1,37 @@ +"extensions for bzlmod" + +load(":repositories.bzl", "rollup_repositories") + +rollup_toolchain = tag_class(attrs = { + "name": attr.string(doc = "Base name for generated repositories"), + "rollup_version": attr.string(doc = "Explicit version of rollup."), + # TODO: support this variant + # "rollup_version_from": attr.string(doc = "Location of package.json which may have a version for @rollup/core."), +}) + +def _toolchain_extension(module_ctx): + registrations = {} + for mod in module_ctx.modules: + for toolchain in mod.tags.toolchain: + if toolchain.name in registrations.keys(): + if toolchain.rollup_version == registrations[toolchain.name]: + # No problem to register a matching toolchain twice + continue + fail("Multiple conflicting toolchains declared for name {} ({} and {}".format( + toolchain.name, + toolchain.rollup_version, + registrations[toolchain.name], + )) + else: + registrations[toolchain.name] = toolchain.rollup_version + for name, rollup_version in registrations.items(): + rollup_repositories( + name = name, + rollup_version = rollup_version, + register = False, + ) + +rollup = module_extension( + implementation = _toolchain_extension, + tag_classes = {"toolchain": rollup_toolchain}, +)