Skip to content

Commit

Permalink
Handle canonical repository names vs apparent repository names and st…
Browse files Browse the repository at this point in the history
…op disabling bzlmod

We don't actively use bzlmod yet, but we're no longer disabling it.
  • Loading branch information
James Judd committed Jul 25, 2024
1 parent 24c9d2b commit 61ccf28
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 8 deletions.
1 change: 0 additions & 1 deletion .bazelrc_shared
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
common --announce_rc
common --color=yes
common --noenable_bzlmod
# Fix "Failed to fetch blobs because they do not exist remotely" errors
# https://github.com/bazelbuild/bazel/issues/18696#issuecomment-2175561503
common --remote_download_all
Expand Down
6 changes: 6 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
110 changes: 110 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions rules/private/phases/phase_zinc_depscheck.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ def phase_zinc_depscheck(ctx, g):
deps_configuration = ctx.attr.scala[_DepsConfiguration]

deps_checks = {}
labeled_jars = depset(transitive = [dep[_LabeledJars].values for dep in ctx.attr.deps])
labeled_jar_groups = depset(transitive = [dep[_LabeledJars].values for dep in ctx.attr.deps])

worker_inputs, _, worker_input_manifests = ctx.resolve_command(tools = [deps_configuration.worker])
for name in ("direct", "used"):
deps_check = ctx.actions.declare_file("{}/depscheck_{}.success".format(ctx.label.name, name))
deps_args = ctx.actions.args()
deps_args.add(name, format = "--check_%s=true")
deps_args.add_all("--direct", [dep.label for dep in ctx.attr.deps], format_each = "_%s")
deps_args.add_all(labeled_jars, map_each = _depscheck_labeled_group)

# Check the comment on the function we're calling here to understand why
# we're not using map_each
for labeled_jar_group in labeled_jar_groups.to_list():
_add_args_for_depscheck_labeled_group(labeled_jar_group, deps_args)

deps_args.add("--label", ctx.label, format = "_%s")
deps_args.add_all("--used_whitelist", [dep.label for dep in ctx.attr.deps_used_whitelist], format_each = "_%s")
deps_args.add_all("--unused_whitelist", [dep.label for dep in ctx.attr.deps_unused_whitelist], format_each = "_%s")
Expand Down Expand Up @@ -63,5 +69,12 @@ def phase_zinc_depscheck(ctx, g):
toolchain = deps_configuration,
)

def _depscheck_labeled_group(labeled_jars):
return (["--group", "_{}".format(labeled_jars.label)] + [jar.path for jar in labeled_jars.jars.to_list()])
# If you use avoid using map_each, then labels are converted to their apparent repo name rather than
# their canonical repo name. The apparent repo name is the human readable one that we want for use
# with buildozer. See https://bazel.build/rules/lib/builtins/Args.html for more info
#
# Avoiding map_each is why we've got this odd section of add and add_all to create a --group
def _add_args_for_depscheck_labeled_group(labeled_jar_group, deps_args):
deps_args.add("--group")
deps_args.add(labeled_jar_group.label, format = "_%s")
deps_args.add_all(labeled_jar_group.jars)
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,21 @@ object DepsRunner extends WorkerMain[Unit] {
case _ => throw new Exception(s"Unexpected case in DepsRunner")
}
val labelToPaths = groups.toMap
def pathsForLabel(depLabel: String) =
Seq(depLabel, s"@${depLabel}", depLabel.stripPrefix("@")).collect(labelToPaths).flatten
def pathsForLabel(depLabel: String): Seq[String] = {
// A label could have no @ prefix, a single @ prefix, or a double @@ prefix.
// In an ideal world, the label we see here would always match the label in
// the --group, but that's not always the case. So we need to be able to handle
// moving from any of the forms to any of the other forms.
val potentialLabels = if (depLabel.startsWith("@@")) {
Seq(depLabel.stripPrefix("@@"), depLabel.stripPrefix("@"), depLabel)
} else if (depLabel.startsWith("@")) {
Seq(depLabel.stripPrefix("@"), depLabel, s"@${depLabel}")
} else {
Seq(depLabel, s"@${depLabel}", s"@@${depLabel}")
}

potentialLabels.collect(labelToPaths).flatten
}
val usedPaths = Files.readAllLines(namespace.get[File]("used").toPath).asScala.toSet

val remove = if (namespace.getBoolean("check_used") == true) {
Expand Down
6 changes: 6 additions & 0 deletions tests/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
Loading

0 comments on commit 61ccf28

Please sign in to comment.