diff --git a/internal/common.bzl b/internal/common.bzl index 912940ef4..d8ae4c224 100644 --- a/internal/common.bzl +++ b/internal/common.bzl @@ -36,3 +36,10 @@ def executable_extension(ctx): if ctx.os.name.startswith("windows"): extension = ".exe" return extension + +def watch(ctx, path): + # Versions of Bazel that have ctx.watch may no longer explicitly watch + # labels on which ctx.path is called and/or labels in attributes. Do so + # explicitly here, duplicate watches are no-ops. + if hasattr(ctx, "watch"): + ctx.watch(path) diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl index 48a9d1490..758f7b8bd 100644 --- a/internal/go_repository.bzl +++ b/internal/go_repository.bzl @@ -13,7 +13,7 @@ # limitations under the License. load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch", "read_user_netrc", "use_netrc") -load("//internal:common.bzl", "env_execute", "executable_extension") +load("//internal:common.bzl", "env_execute", "executable_extension", "watch") load("//internal:go_repository_cache.bzl", "read_cache_env") _DOC = """ @@ -319,6 +319,10 @@ def _go_repository_impl(ctx): repo_config = ctx.path(Label("@@" + extension_repo_prefix + "bazel_gazelle_go_repository_config//:WORKSPACE")) else: repo_config = ctx.path(ctx.attr.build_config) + + watch(ctx, gazelle_path) + watch(ctx, repo_config) + cmd = [ gazelle_path, "-go_repository_mode", @@ -389,6 +393,7 @@ def _generate_package_info(*, importpath, version): # TODO: Consider adding support for custom remotes. package_url = "https://{}".format(importpath) if version else None package_version = version.removeprefix("v") if version else None + # See specification: # https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#golang # scheme:type/namespace/name@version?qualifiers#subpath diff --git a/internal/go_repository_cache.bzl b/internal/go_repository_cache.bzl index 88caf74bb..40161c9da 100644 --- a/internal/go_repository_cache.bzl +++ b/internal/go_repository_cache.bzl @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//internal:common.bzl", "executable_extension") +load("//internal:common.bzl", "executable_extension", "watch") # Change to trigger cache invalidation: 1 @@ -96,6 +96,7 @@ go_repository_cache = repository_rule( ) def read_go_env(ctx, go_tool, var): + watch(ctx, go_tool) res = ctx.execute([go_tool, "env", var]) if res.return_code: fail("failed to read go environment: " + res.stderr) diff --git a/internal/go_repository_config.bzl b/internal/go_repository_config.bzl index 5592e8b21..2efd1eef1 100644 --- a/internal/go_repository_config.bzl +++ b/internal/go_repository_config.bzl @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//internal:common.bzl", "env_execute", "executable_extension") +load("//internal:common.bzl", "env_execute", "executable_extension", "watch") load("//internal:go_repository_cache.bzl", "read_cache_env") def _go_repository_config_impl(ctx): @@ -25,15 +25,17 @@ def _go_repository_config_impl(ctx): config_path = ctx.path(ctx.attr.config) if config_path: - env = read_cache_env(ctx, str(ctx.path(Label("@bazel_gazelle_go_repository_cache//:go.env")))) - generate_repo_config = str(ctx.path(Label("@bazel_gazelle_go_repository_tools//:bin/generate_repo_config{}".format(executable_extension(ctx))))) + watch(ctx, config_path) + env = read_cache_env(ctx, ctx.path(Label("@bazel_gazelle_go_repository_cache//:go.env"))) + generate_repo_config = ctx.path(Label("@bazel_gazelle_go_repository_tools//:bin/generate_repo_config{}".format(executable_extension(ctx)))) + watch(ctx, generate_repo_config) list_repos_args = [ "-config_source=" + str(config_path), "-config_dest=" + str(ctx.path("WORKSPACE")), ] result = env_execute( ctx, - [generate_repo_config] + list_repos_args, + [str(generate_repo_config)] + list_repos_args, environment = env, ) if result.return_code: @@ -47,7 +49,7 @@ def _go_repository_config_impl(ctx): config_label = str(ctx.attr.config) macro_label_prefix = config_label[:config_label.find("//")] macro_label_str = macro_label_prefix + "//:" + f - ctx.path(Label(macro_label_str)) + watch(ctx, ctx.path(Label(macro_label_str))) else: ctx.file( diff --git a/internal/go_repository_tools.bzl b/internal/go_repository_tools.bzl index 9f2d98c78..8478ff399 100644 --- a/internal/go_repository_tools.bzl +++ b/internal/go_repository_tools.bzl @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//internal:common.bzl", "env_execute", "executable_extension") +load("//internal:common.bzl", "env_execute", "executable_extension", "watch") load("//internal:go_repository_cache.bzl", "read_cache_env") load("//internal:go_repository_tools_srcs.bzl", "GO_REPOSITORY_TOOLS_SRCS") @@ -38,10 +38,14 @@ exports_files(["ROOT"]) """ def _go_repository_tools_impl(ctx): + for src in ctx.attr._go_repository_tools_srcs: + watch(ctx, src) + # Create a link to the gazelle repo. This will be our GOPATH. env = read_cache_env(ctx, str(ctx.path(ctx.attr.go_cache))) extension = executable_extension(ctx) go_tool = env["GOROOT"] + "/bin/go" + extension + watch(ctx, go_tool) ctx.symlink( ctx.path(Label("//:WORKSPACE")).dirname, @@ -70,6 +74,7 @@ def _go_repository_tools_impl(ctx): # rule each time. Compiling the script is relatively slow. # Don't try this on Windows: bazel does not set up symbolic links. if "windows" not in ctx.os.name: + watch(ctx, ctx.attr._list_repository_tools_srcs) result = env_execute( ctx, [