Skip to content

Commit

Permalink
Fix staleness issues with recent Bazel versions
Browse files Browse the repository at this point in the history
`ctx.path` and label attributes may no longer implicitly watch their contents in Bazel 7.4 and higher, so we need to explicitly watch them.
  • Loading branch information
fmeum committed Jan 7, 2025
1 parent 23a2816 commit 737ce9c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions internal/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 6 additions & 1 deletion internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/go_repository_cache.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions internal/go_repository_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion internal/go_repository_tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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,
Expand Down Expand Up @@ -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,
[
Expand Down

0 comments on commit 737ce9c

Please sign in to comment.