diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 31934d2..aabe02a 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -1,47 +1,44 @@ --- +.reusable_config: &reusable_config + build_targets: + - "--" + - "//..." + - "@bazel_skylib_gazelle_plugin//..." + test_targets: + - "--" + - "//..." + - "@bazel_skylib_gazelle_plugin//..." + tasks: ubuntu1804_latest: + <<: *reusable_config name: "Latest Bazel" platform: ubuntu1804 bazel: latest - build_targets: - - "//..." - test_targets: - - "//..." test_flags: - "--test_env=PATH" ubuntu1604_latest: + <<: *reusable_config name: "Latest Bazel" platform: ubuntu1604 bazel: latest - build_targets: - - "//..." - test_targets: - - "//..." test_flags: - "--test_env=PATH" macos_latest: + <<: *reusable_config name: "Latest Bazel" platform: macos bazel: latest - build_targets: - - "//..." - test_targets: - - "//..." test_flags: - "--test_env=PATH" windows_latest: + <<: *reusable_config name: "Latest Bazel" platform: windows bazel: latest - build_targets: - - "//..." - test_targets: - - "--" - - "//..." test_flags: # TODO(laszlocsomor): remove "--test_env=LOCALAPPDATA" after # https://github.com/bazelbuild/bazel/issues/7761 is fixed @@ -49,47 +46,34 @@ tasks: - "--test_tag_filters=-no_windows" ubuntu1804_last_green: + <<: *reusable_config name: "Last Green Bazel" platform: ubuntu1804 bazel: last_green - build_targets: - - "//..." - test_targets: - - "//..." test_flags: - "--test_env=PATH" ubuntu1604_last_green: + <<: *reusable_config name: "Last Green Bazel" platform: ubuntu1604 bazel: last_green - build_targets: - - "//..." - test_targets: - - "//..." test_flags: - "--test_env=PATH" macos_last_green: + <<: *reusable_config name: "Last Green Bazel" platform: macos bazel: last_green - build_targets: - - "//..." - test_targets: - - "//..." test_flags: - "--test_env=PATH" windows_last_green: + <<: *reusable_config name: "Last Green Bazel" platform: windows bazel: last_green - build_targets: - - "//..." - test_targets: - - "--" - - "//..." test_flags: # TODO(laszlocsomor): remove "--test_env=LOCALAPPDATA" after # https://github.com/bazelbuild/bazel/issues/7761 is fixed @@ -97,13 +81,10 @@ tasks: - "--test_tag_filters=-no_windows" ubuntu1804_last_green_bzlmod: + <<: *reusable_config name: "Last Green Bazel (with bzlmod)" platform: ubuntu1804 bazel: last_green - build_targets: - - "//..." - test_targets: - - "//..." build_flags: - "--experimental_enable_bzlmod" test_flags: diff --git a/.bazelignore b/.bazelignore new file mode 100644 index 0000000..dbff643 --- /dev/null +++ b/.bazelignore @@ -0,0 +1 @@ +gazelle/ diff --git a/BUILD b/BUILD index 8425403..3a0651a 100644 --- a/BUILD +++ b/BUILD @@ -10,6 +10,7 @@ package(default_visibility = ["//visibility:public"]) exports_files([ "LICENSE", "MODULE.bazel", + "WORKSPACE", ]) filegroup( @@ -68,10 +69,10 @@ bzl_library( filegroup( name = "distribution", srcs = [ - "LICENSE", "BUILD", "CODEOWNERS", "CONTRIBUTORS", + "LICENSE", "WORKSPACE.bzlmod", "//lib:distribution", "//rules:distribution", diff --git a/CHANGELOG.md b/CHANGELOG.md index ef9c469..de9b0bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,25 @@ +Release 1.4.1 + +Bugfix release: fixes gazelle plugin failure with +`--incompatible_disallow_empty_glob` (#431) and (for released versions) with +`--incompatible_disable_starlark_host_transitions` (#433). + +**Contributors** +Alexandre Rostovtsev, Chuck Grindel + + +Release 1.4.0 + +**New Features** +- The Gazelle plugin is marked stable for general use (#400, #424) + +**Other Notable Changes** +- copy_file/copy_directory again allow sandboxing (#392) + +**Contributors** +Alexandre Rostovtsev, Nick Gooding, Simon Stewart, Xùdōng Yáng + + Release 1.3.0 **New Features** diff --git a/MODULE.bazel b/MODULE.bazel index 64ce586..2c3cc0c 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,7 +1,8 @@ module( name = "bazel_skylib", + # Keep in sync with version.bzl and @bazel_skylib_gazelle_plugin//:MODULE.bazel + version = "1.4.1", compatibility_level = 1, - version = "1.3.0", ) register_toolchains( @@ -13,21 +14,16 @@ bazel_dep(name = "platforms", version = "0.0.4") ### INTERNAL ONLY - lines after this are not included in the release packaging. -# Gazelle extension is experimental -bazel_dep(name = "rules_go", repo_name = "io_bazel_rules_go", version = "0.33.0") -bazel_dep(name = "gazelle", repo_name = "bazel_gazelle", version = "0.26.0") - -go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps") - -go_deps.module( - path = "github.com/bazelbuild/buildtools", - sum = "h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=", - version = "v0.0.0-20220531122519-a43aed7014c8", -) - -use_repo(go_deps, "com_github_bazelbuild_buildtools") - # Build-only / test-only dependencies -bazel_dep(name = "stardoc", dev_dependency = True, repo_name = "io_bazel_stardoc", version = "0.5.3") -bazel_dep(name = "rules_pkg", dev_dependency = True, version = "0.5.1") -bazel_dep(name = "rules_cc", dev_dependency = True, version = "0.0.1") +bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc") +bazel_dep(name = "rules_pkg", version = "0.7.0", dev_dependency = True) +bazel_dep(name = "rules_cc", version = "0.0.2", dev_dependency = True) + +# Needed for bazelci and for building distribution tarballs. +# If using an unreleased version of bazel_skylib via git_override, apply +# MODULE.bazel-remove-override.patch to remove the following lines: +bazel_dep(name = "bazel_skylib_gazelle_plugin", dev_dependency = True) +local_path_override( + module_name = "bazel_skylib_gazelle_plugin", + path = "gazelle", +) diff --git a/MODULE.bazel-remove-override.patch b/MODULE.bazel-remove-override.patch new file mode 100644 index 0000000..0f30777 --- /dev/null +++ b/MODULE.bazel-remove-override.patch @@ -0,0 +1,16 @@ +--- MODULE.bazel ++++ MODULE.bazel +@@ -22,8 +22,8 @@ + # Needed for bazelci and for building distribution tarballs. + # If using an unreleased version of bazel_skylib via git_override, apply + # MODULE.bazel-remove-override.patch to remove the following lines: +-bazel_dep(name = "bazel_skylib_gazelle_plugin", dev_dependency = True) +-local_path_override( +- module_name = "bazel_skylib_gazelle_plugin", +- path = "gazelle", +-) ++# bazel_dep(name = "bazel_skylib_gazelle_plugin", dev_dependency = True) ++# local_path_override( ++# module_name = "bazel_skylib_gazelle_plugin", ++# path = "gazelle", ++# ) diff --git a/README.md b/README.md index ce70bab..4c6764e 100644 --- a/README.md +++ b/README.md @@ -132,3 +132,38 @@ then you probably forgot to load and call `bazel_skylib_workspace()` in your See the [maintaner's guide](docs/maintainers_guide.md) for instructions for cutting a new release. + +## Gazelle Plugin + +`bazel_skylib` ships with a [gazelle](https://github.com/bazelbuild/bazel-gazelle) +plugin to generate `bzl_library` entries in build files. To use this, in your +`WORKSPACE`: + +```starlark +load("@bazel_skylib_gazelle_plugin//:workspace.bzl", "bazel_skylib_gazelle_plugin_workspace") + +bazel_skylib_gazelle_plugin_workspace() + +load("@bazel_skylib_gazelle_plugin//:setup.bzl", "bazel_skylib_gazelle_plugin_setup") + +bazel_skylib_gazelle_plugin_setup() +``` + +You may then include the plugin using code similar to this in your `BUILD.bazel` +file: + +```starlark +load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary") + +gazelle( + name = "gazelle", + gazelle = ":gazelle_bin", +) + +gazelle_binary( + name = "gazelle_bin", + languages = DEFAULT_LANGUAGES + [ + "@bazel_skylib_gazelle_plugin//bzl", + ], +) +``` diff --git a/WORKSPACE b/WORKSPACE index 3876987..1b551a3 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,27 +1,28 @@ workspace(name = "bazel_skylib") +load(":workspace.bzl", "bazel_skylib_workspace") + +bazel_skylib_workspace() + +### INTERNAL ONLY - lines after this are not included in the release packaging. +# Lines below are for tests, documentation generation, and distribution archive +# generation only, and should thus not be included by dependencies on bazel-skylib. + load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") -maybe( - name = "io_bazel_rules_go", - repo_rule = http_archive, - sha256 = "685052b498b6ddfe562ca7a97736741d87916fe536623afb7da2824c0211c369", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip", - "https://github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip", - ], +local_repository( + name = "bazel_skylib_gazelle_plugin", + path = "gazelle", ) -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +load("@bazel_skylib_gazelle_plugin//:workspace.bzl", "bazel_skylib_gazelle_plugin_workspace") -go_rules_dependencies() +bazel_skylib_gazelle_plugin_workspace() -go_register_toolchains(version = "1.17.1") +load("@bazel_skylib_gazelle_plugin//:setup.bzl", "bazel_skylib_gazelle_plugin_setup") -# Below this line is for documentation generation only, -# and should thus not be included by dependencies on -# bazel-skylib. +bazel_skylib_gazelle_plugin_setup() maybe( http_archive, @@ -40,10 +41,10 @@ stardoc_repositories() maybe( http_archive, name = "rules_pkg", - sha256 = "a89e203d3cf264e564fcb96b6e06dd70bc0557356eb48400ce4b5d97c2c3720d", + sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2", urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.5.1/rules_pkg-0.5.1.tar.gz", - "https://github.com/bazelbuild/rules_pkg/releases/download/0.5.1/rules_pkg-0.5.1.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz", + "https://github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz", ], ) @@ -64,22 +65,3 @@ maybe( load("//lib:unittest.bzl", "register_unittest_toolchains") register_unittest_toolchains() - -# Provide a repository hint for Gazelle to inform it that the go package -# github.com/bazelbuild/rules_go is available from io_bazel_rules_go and it -# doesn't need to duplicatively fetch it. -# gazelle:repository go_repository name=io_bazel_rules_go importpath=github.com/bazelbuild/rules_go -http_archive( - name = "bazel_gazelle", - sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz", - "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz", - ], -) -# Another Gazelle repository hint. -# gazelle:repository go_repository name=bazel_gazelle importpath=github.com/bazelbuild/bazel-gazelle/testtools - -load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") - -gazelle_dependencies() diff --git a/distribution/BUILD b/distribution/BUILD index 326cee7..1aa737c 100644 --- a/distribution/BUILD +++ b/distribution/BUILD @@ -1,37 +1,84 @@ +load("distribution.bzl", "remove_internal_only") load("@bazel_skylib//:version.bzl", "version") load("@rules_pkg//:pkg.bzl", "pkg_tar") -load("@rules_pkg//releasing:defs.bzl", "print_rel_notes") +load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") package( default_visibility = ["//visibility:private"], ) -genrule( - name = "distro_module_bazel", - srcs = ["//:MODULE.bazel"], - outs = ["MODULE.bazel"], - cmd = "sed -e '/### INTERNAL ONLY/,$$d' $(location //:MODULE.bazel) >$@", +remove_internal_only( + name = "distro_workspace", + src = "//:WORKSPACE", + out = "WORKSPACE", ) -# Build the artifact to put on the github release page. -pkg_tar( - name = "bazel-skylib-%s" % version, +remove_internal_only( + name = "distro_module_bazel", + src = "//:MODULE.bazel", + out = "MODULE.bazel", +) + +# remove "distribution/" path prefix +pkg_files( + name = "distro-files-without-prefix", srcs = [ "distro_module_bazel", + "distro_workspace", + ], + strip_prefix = strip_prefix.from_pkg(), +) + +pkg_tar( + name = "bazel-skylib", + srcs = [ + "distro-files-without-prefix", "//:distribution", ], + out = "bazel-skylib-%s.tar.gz" % version, extension = "tar.gz", mode = "0644", # Make it owned by root so it does not have the uid of the CI robot. owner = "0.0", - strip_prefix = ".", + strip_prefix = strip_prefix.from_root(), ) -print_rel_notes( - name = "relnotes", - outs = ["relnotes.txt"], - deps_method = "bazel_skylib_workspace", - repo = "bazel-skylib", - setup_file = ":workspace.bzl", - version = version, +# @bazel_skylib_gazelle_plugin//:WORKSPACE refers to bazel-skylib in the parent +# directory. For distribution, use the minimal WORSKPACE.bzlmod instead. +pkg_files( + name = "bazel-skylib-gazelle-plugin-distro_workspace", + srcs = ["@bazel_skylib_gazelle_plugin//:WORKSPACE.bzlmod"], + renames = {"@bazel_skylib_gazelle_plugin//:WORKSPACE.bzlmod": "WORKSPACE"}, +) + +pkg_files( + name = "bazel-skylib-gazelle-plugin-without-external-prefix", + srcs = [ + "@bazel_skylib_gazelle_plugin//:distribution", + "@bazel_skylib_gazelle_plugin//bzl:distribution", + ], + strip_prefix = strip_prefix.from_root(), +) + +pkg_tar( + name = "bazel-skylib-gazelle-plugin", + srcs = [ + "bazel-skylib-gazelle-plugin-distro_workspace", + "bazel-skylib-gazelle-plugin-without-external-prefix", + "//:LICENSE", + ], + out = "bazel-skylib-gazelle-plugin-%s.tar.gz" % version, + extension = "tar.gz", + mode = "0644", + # Make it owned by root so it does not have the uid of the CI robot. + owner = "0.0", +) + +# Build the artifacts to put on the github release page. +filegroup( + name = "distribution", + srcs = [ + "bazel-skylib", + "bazel-skylib-gazelle-plugin", + ], ) diff --git a/distribution/distribution.bzl b/distribution/distribution.bzl new file mode 100644 index 0000000..e1dd14e --- /dev/null +++ b/distribution/distribution.bzl @@ -0,0 +1,32 @@ +# Copyright 2023 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Helper utilities for generating distribution tarballs.""" + +def remove_internal_only(name, src, out, **kwargs): + """Removes '### INTERNAL ONLY' line and all lines below from a file. + + Args: + name: Name of the rule. + src: File to process. + out: Path of the output file. + **kwargs: further keyword arguments. + """ + native.genrule( + name = name, + srcs = [src], + outs = [out], + cmd = "sed -e '/### INTERNAL ONLY/,$$d' $< >$@", + **kwargs + ) diff --git a/docs/maintainers_guide.md b/docs/maintainers_guide.md index 5339b6a..6c06a3f 100644 --- a/docs/maintainers_guide.md +++ b/docs/maintainers_guide.md @@ -72,25 +72,33 @@ Name 1, Name 2, Name 3 (alphabetically from `git log`) -------------------------------------------------------------------------------- -2. Bump `version` in version.bzl *and* MODULE.bazel to the new version. - TODO(#386): add a test to make sure the two versions are in sync. +2. Bump `version` in version.bzl, MODULE.bazel, *and* gazelle/MODULE.bazel to + the new version. + TODO(#386): add a test to make sure all the versions are in sync. 3. Ensure that the commits for steps 1 and 2 have been merged. All further steps must be performed on a single, known-good git commit. -4. `bazel build //distribution:bazel-skylib-$VERSION.tar.gz` -5. Copy the `bazel-skylib-$VERSION.tar.gz` tarball to the mirror (you'll need - Bazel developer gcloud credentials; assuming you are a Bazel developer, you - can obtain them via `gcloud init`): +4. `bazel build //distribution` +5. Copy the `bazel-skylib-$VERSION.tar.gz` and + `bazel-skylib-gazelle-plugin-$VERSION.tar.gz` tarballs to the mirror (you'll + need Bazel developer gcloud credentials; assuming you are a Bazel developer, + you can obtain them via `gcloud init`): -``` -gsutil cp bazel-bin/distribution/bazel-skylib-$VERSION.tar.gz gs://bazel-mirror/github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib-$VERSION.tar.gz -gsutil setmeta -h "Cache-Control: public, max-age=31536000" "gs://bazel-mirror/github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib-$VERSION.tar.gz" +```bash +gsutil cp bazel-bin/distribution/bazel-skylib{,-gazelle-plugin}-$VERSION.tar.gz gs://bazel-mirror/github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/ +gsutil setmeta -h "Cache-Control: public, max-age=31536000" gs://bazel-mirror/github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib{,-gazelle-plugin}-$VERSION.tar.gz ``` -6. Run `sha256sum bazel-bin/distro/bazel-skylib-$VERSION.tar.gz`; you'll need - the checksum for the release notes. +6. Obtain checksums for release notes: + +```bash +sha256sum bazel-bin/distribution/bazel-skylib-$VERSION.tar.gz +sha256sum bazel-bin/distribution/bazel-skylib-gazelle-plugin-$VERSION.tar.gz +```` + 7. Draft a new release with a new tag named $VERSION in github. Attach - `bazel-skylib-$VERSION.tar.gz` to the release. For the release notes, use - the CHANGELOG.md entry plus the following template: + `bazel-skylib-$VERSION.tar.gz` and + `bazel-skylib-gazelle-plugin-$VERSION.tar.gz` to the release. For the + release notes, use the CHANGELOG.md entry plus the following template: -------------------------------------------------------------------------------- @@ -98,20 +106,88 @@ gsutil setmeta -h "Cache-Control: public, max-age=31536000" "gs://bazel-mirror/g ``` load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + http_archive( name = "bazel_skylib", + sha256 = "$SHA256SUM" urls = [ "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib-$VERSION.tar.gz", "https://github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib-$VERSION.tar.gz", ], - sha256 = "$SHA256SUM", ) + load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") + bazel_skylib_workspace() ``` +***Additional WORKSPACE setup for the Gazelle plugin*** + +```starlark +http_archive( + name = "bazel_skylib_gazelle_plugin", + sha256 = "$SHA256SUM_GAZELLE_PLUGIN", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib-gazelle-plugin-$VERSION.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/$VERSION/bazel-skylib-gazelle-plugin-$VERSION.tar.gz", + ], +) + +load("@bazel_skylib_gazelle_plugin//:workspace.bzl", "bazel_skylib_gazelle_plugin_workspace") + +bazel_skylib_gazelle_plugin_workspace() + +load("@bazel_skylib_gazelle_plugin//:setup.bzl", "bazel_skylib_gazelle_plugin_setup") + +bazel_skylib_gazelle_plugin_setup() +``` + **Using the rules** See [the source](https://github.com/bazelbuild/bazel-skylib/tree/$VERSION). --------------------------------------------------------------------------------- \ No newline at end of file +-------------------------------------------------------------------------------- + +8. Obtain [Subresource Integrity](https://w3c.github.io/webappsec-subresource-integrity/#integrity-metadata-description) + format checksums for bzlmod: + +```bash +echo -n sha256-; cat bazel-bin/distribution/bazel-skylib-$VERSION.tar.gz | openssl dgst -sha256 -binary | base64 +echo -n sha256-; cat bazel-bin/distribution/bazel-skylib-gazelle-plugin-$VERSION.tar.gz | openssl dgst -sha256 -binary | base64 +``` + +9. Create a PR at [Bazel Central Registry](https://github.com/bazelbuild/bazel-central-registry) + to update the registry's versions of bazel_skylib and + bazel_skylib_gazelle_plugin. + + Use https://github.com/bazelbuild/bazel-central-registry/pull/403 as the + model; you will need to update `modules/bazel_skylib/metadata.json` and + `modules/bazel_skylib_gazelle_plugin/metadata.json` to list the new version + in `versions`, and create new $VERSION subdirectories for the updated + modules, using the latest existing version subdirectories as the guide. Use + Subresource Integrity checksums obtained above in the new `source.json` + files. + + Ensure that the MODULE.bazel files you add in the new $VERSION + subdirectories exactly match the MODULE.bazel file packaged in + bazel-skylib-$VERSION.tar.gz and bazel-skylib-gazelle-plugin-$VERSION.tar.gz + tarballs - or buildkite checks will fail. + +10. Once the Bazel Central Registry PR is merged, insert in the release + description after the WORKSPACE setup section: + +-------------------------------------------------------------------------------- + +**MODULE.bazel setup** + +```starlark +bazel_dep(name = "bazel_skylib", version = "$VERSION") +``` + +And for the Gazelle plugin: + +```starlark +bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "$VERSION", dev_dependency = True) +``` + +-------------------------------------------------------------------------------- diff --git a/gazelle/BUILD b/gazelle/BUILD new file mode 100644 index 0000000..d1c6daa --- /dev/null +++ b/gazelle/BUILD @@ -0,0 +1,37 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +exports_files(["WORKSPACE.bzlmod"]) + +bzl_library( + name = "setup", + srcs = ["setup.bzl"], + visibility = ["//visibility:public"], + deps = [ + "@bazel_gazelle//:deps", + "@io_bazel_rules_go//go:deps", + ], +) + +bzl_library( + name = "workspace", + srcs = ["workspace.bzl"], + visibility = ["//visibility:public"], + deps = [ + "@bazel_skylib//:workspace", + "@bazel_tools//tools/build_defs/repo:http.bzl", + "@bazel_tools//tools/build_defs/repo:utils.bzl", + ], +) + +# TODO(arostovtsev): exclude everything below from distro tarball +filegroup( + name = "distribution", + srcs = [ + "BUILD", + "MODULE.bazel", + "WORKSPACE.bzlmod", + "setup.bzl", + "workspace.bzl", + ], + visibility = ["//visibility:public"], +) diff --git a/gazelle/MODULE.bazel b/gazelle/MODULE.bazel new file mode 100644 index 0000000..09e426b --- /dev/null +++ b/gazelle/MODULE.bazel @@ -0,0 +1,21 @@ +module( + name = "bazel_skylib_gazelle_plugin", + # Keep in sync with @bazel_skylib//:MODULE.bazel and @bazel_skylib//:version.bzl + version = "1.4.1", + compatibility_level = 1, +) + +# Keep in sync with @bazel_skylib//:MODULE.bazel and @bazel_skylib//:version.bzl +bazel_dep(name = "bazel_skylib", version = "1.4.1") +bazel_dep(name = "gazelle", version = "0.29.0", repo_name = "bazel_gazelle") +bazel_dep(name = "rules_go", version = "0.38.1", repo_name = "io_bazel_rules_go") + +# `rules_go` will register a toolchain for us if the user doesn't do so + +go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps") +go_deps.module( + path = "github.com/bazelbuild/buildtools", + sum = "h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=", + version = "v0.0.0-20220531122519-a43aed7014c8", +) +use_repo(go_deps, "com_github_bazelbuild_buildtools") diff --git a/gazelle/WORKSPACE b/gazelle/WORKSPACE new file mode 100644 index 0000000..cc0217d --- /dev/null +++ b/gazelle/WORKSPACE @@ -0,0 +1,25 @@ +workspace(name = "bazel_skylib_gazelle_plugin") + +local_repository( + name = "bazel_skylib", + path = "..", +) + +load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") + +bazel_skylib_workspace() + +load(":workspace.bzl", "bazel_skylib_gazelle_plugin_workspace") + +bazel_skylib_gazelle_plugin_workspace() + +load(":setup.bzl", "bazel_skylib_gazelle_plugin_setup") + +bazel_skylib_gazelle_plugin_setup() + +# Provide a repository hint for Gazelle to inform it that the go package +# github.com/bazelbuild/rules_go is available from io_bazel_rules_go and it +# doesn't need to duplicatively fetch it. +# gazelle:repository go_repository name=io_bazel_rules_go importpath=github.com/bazelbuild/rules_go +# Another Gazelle repository hint. +# gazelle:repository go_repository name=bazel_gazelle importpath=github.com/bazelbuild/bazel-gazelle/testtools diff --git a/gazelle/WORKSPACE.bzlmod b/gazelle/WORKSPACE.bzlmod new file mode 100644 index 0000000..621db93 --- /dev/null +++ b/gazelle/WORKSPACE.bzlmod @@ -0,0 +1,8 @@ +workspace(name = "bazel_skylib_gazelle_plugin") + +# Provide a repository hint for Gazelle to inform it that the go package +# github.com/bazelbuild/rules_go is available from io_bazel_rules_go and it +# doesn't need to duplicatively fetch it. +# gazelle:repository go_repository name=io_bazel_rules_go importpath=github.com/bazelbuild/rules_go +# Another Gazelle repository hint. +# gazelle:repository go_repository name=bazel_gazelle importpath=github.com/bazelbuild/bazel-gazelle/testtools diff --git a/gazelle/bzl/BUILD b/gazelle/bzl/BUILD index 29caeb0..0b8e6d3 100644 --- a/gazelle/bzl/BUILD +++ b/gazelle/bzl/BUILD @@ -1,5 +1,5 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") # gazelle:exclude testdata @@ -25,9 +25,12 @@ go_test( srcs = ["gazelle_test.go"], data = [ ":gazelle-skylib", - ] + glob([ - "testdata/**", - ]), + ] + glob( + [ + "testdata/**", + ], + allow_empty = True, + ), embed = [":bzl"], deps = [ "@bazel_gazelle//testtools:go_default_library", @@ -51,3 +54,24 @@ gazelle( name = "gazelle", gazelle = ":gazelle-skylib", ) + +# TODO(arostovtsev): exclude everything below from distro tarball + +# The files needed for distribution +# A fake testdata directory is created so that +# the build file has nothing missing, but we +# do not bloat the distribution tarball +filegroup( + name = "distribution", + srcs = glob(["*.go"]) + [ + "BUILD", + ":fake-testdata", + ], + visibility = ["//visibility:public"], +) + +genrule( + name = "fake-testdata", + outs = ["testdata"], + cmd = "touch $@", +) diff --git a/gazelle/bzl/gazelle_test.go b/gazelle/bzl/gazelle_test.go index e4f59e8..1554337 100644 --- a/gazelle/bzl/gazelle_test.go +++ b/gazelle/bzl/gazelle_test.go @@ -29,7 +29,7 @@ import ( var gazellePath = findGazelle() -const testDataPath = "gazelle/bzl/testdata/" +const testDataPath = "bzl/testdata/" // TestGazelleBinary runs a gazelle binary with starlib installed on each // directory in `testdata/*`. Please see `testdata/README.md` for more @@ -135,7 +135,7 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) { } func findGazelle() string { - gazellePath, ok := bazel.FindBinary("gazelle/bzl", "gazelle-skylib") + gazellePath, ok := bazel.FindBinary("bzl", "gazelle-skylib") if !ok { panic("could not find gazelle binary") } diff --git a/gazelle/setup.bzl b/gazelle/setup.bzl new file mode 100644 index 0000000..b70c64c --- /dev/null +++ b/gazelle/setup.bzl @@ -0,0 +1,32 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Dependency registration helpers for the gazelle plugin.""" + +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") + +def bazel_skylib_gazelle_plugin_setup(go_version = "1.18.7", register_go_toolchains = True): + """Set up the dependencies needed by the Gazelle plugin. + + Args: + go_version: The version of Go registered as part of the build as a string + register_go_toolchains: A boolean indicating whether or not to register the Go toolchains. Defaults to `True` + """ + go_rules_dependencies() + + if register_go_toolchains: + go_register_toolchains(version = go_version) + + gazelle_dependencies() diff --git a/gazelle/workspace.bzl b/gazelle/workspace.bzl new file mode 100644 index 0000000..d6fd2f0 --- /dev/null +++ b/gazelle/workspace.bzl @@ -0,0 +1,43 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Dependency registration helpers for repositories which need to load bazel-skylib's gazelle plugin.""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") + +def bazel_skylib_gazelle_plugin_workspace(): + """Loads dependencies required to use skylib's gazelle plugin""" + bazel_skylib_workspace() + + maybe( + http_archive, + name = "io_bazel_rules_go", + sha256 = "dd926a88a564a9246713a9c00b35315f54cbd46b31a26d5d8fb264c07045f05d", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip", + ], + ) + + maybe( + http_archive, + name = "bazel_gazelle", + sha256 = "ecba0f04f96b4960a5b250c8e8eeec42281035970aa8852dda73098274d14a1d", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz", + ], + ) diff --git a/version.bzl b/version.bzl index 70a04d6..c44b714 100644 --- a/version.bzl +++ b/version.bzl @@ -13,4 +13,5 @@ # limitations under the License. """The version of bazel-skylib.""" -version = "1.3.0" +# Keep in sync with MODULE.bazel and @bazel_skylib_gazelle_plugin//:MODULE.bazel +version = "1.4.1"