Skip to content

Commit

Permalink
Track whether a repo is pinned or unpinned by explicit attr
Browse files Browse the repository at this point in the history
Doing this by performing string interpretation on the repo name is a bit
brittle. And it works less well under Bzlmod where the unpinned repo
name is going to look something like
`rules_jvm_external~~maven~unpinned_maven`.

After this change, the lockfile pinning script, `@maven//:pin` will use the local
machine's Coursier cache (`~/Library/Caches/Coursier` on MacOS) even under
Bzlmod. A populated cache makes version resolutions substantially faster to obtain.
  • Loading branch information
plobsing committed Jul 24, 2024
1 parent 806ac7f commit 614f27e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions private/extensions/maven.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def _maven_impl(mctx):
# created from the maven_install.json file in the coursier_fetch
# invocation after this.
name = "unpinned_" + name if repo.get("lock_file") else name,
pinned_repo_name = name if repo.get("lock_file") else None,
user_provided_name = name,
repositories = repo.get("repositories"),
artifacts = artifacts_json,
Expand Down
8 changes: 6 additions & 2 deletions private/rules/coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _is_directory(repository_ctx, path):
return repository_ctx.which("test") and repository_ctx.execute(["test", "-d", path]).return_code == 0

def _is_unpinned(repository_ctx):
return repository_ctx.attr.name.startswith("unpinned_")
return repository_ctx.attr.pinned_repo_name != ""

def _shell_quote(s):
# Lifted from
Expand Down Expand Up @@ -1240,7 +1240,7 @@ def _coursier_fetch_impl(repository_ctx):
# This repository rule can be either in the pinned or unpinned state, depending on when
# the user invokes artifact pinning. Normalize the repository name here.
if _is_unpinned(repository_ctx):
repository_name = repository_ctx.name[len("unpinned_"):]
repository_name = repository_ctx.attr.pinned_repo_name
outdated_build_file_content = ""
else:
repository_name = repository_ctx.name
Expand Down Expand Up @@ -1442,6 +1442,10 @@ coursier_fetch = repository_rule(
),
"ignore_empty_files": attr.bool(default = False, doc = "Treat jars that are empty as if they were not found."),
"additional_coursier_options": attr.string_list(doc = "Additional options that will be passed to coursier."),
"pinned_repo_name": attr.string(
doc = "Name of the corresponding pinned repo for this repo. Presence implies that this is an unpinned repo.",
mandatory = False,
),
},
environ = [
"JAVA_HOME",
Expand Down
1 change: 1 addition & 0 deletions private/rules/maven_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def maven_install(
# created from the maven_install.json file in the coursier_fetch
# invocation after this.
name = name if maven_install_json == None else "unpinned_" + name,
pinned_coursier_fetch = None if maven_install_json == None else name,
repositories = repositories_json_strings,
artifacts = artifacts_json_strings,
fail_on_missing_checksum = fail_on_missing_checksum,
Expand Down

0 comments on commit 614f27e

Please sign in to comment.