Skip to content

Commit

Permalink
Do not conflict with patches
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jul 31, 2024
1 parent 24a345a commit 811422f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,11 @@ def _go_repository_impl(ctx):
if generate:
# Build file generation is needed. Populate Gazelle directive at root build file
build_file_name = existing_build_file or build_file_names[0]
ctx.file(
build_file_name,
"\n".join(["# " + d for d in ctx.attr.build_directives] + [_generate_package_info(
importpath = ctx.attr.importpath,
version = ctx.attr.version,
)]),
)
if len(ctx.attr.build_directives) > 0:
ctx.file(
build_file_name,
"\n".join(["# " + d for d in ctx.attr.build_directives]),
)

# Run Gazelle
if gazelle_path == None:
Expand Down Expand Up @@ -363,22 +361,40 @@ def _go_repository_impl(ctx):
patch(ctx)

if generate:
ctx.file("REPO.bazel", """\
# Do not override a REPO.bazel patched in by users. This also provides a
# way for users to opt out of Gazelle-generated package_info.
repo_file = ctx.path("REPO.bazel")
if not repo_file.exists:
ctx.file("REPO.bazel", """\
repo(default_package_metadata = ["//:gazelle_generated_package_info"])
""")

# Modify the top-level build file after patches have been applied as the
# patches may otherwise conflict with our generated content.
build_file = ctx.path(build_file_name)
if build_file.exists:
build_file_content = ctx.read(build_file)
else:
build_file_content = ""
build_file_content += _generate_package_info(
importpath = ctx.attr.importpath,
version = ctx.attr.version,
)
ctx.file(build_file_name, build_file_content)

def _generate_package_info(*, importpath, version):
package_name = importpath
package_url = "https://" + importpath
package_version = version.removeprefix("v")
return """\
return """
load("@rules_license//rules:package_info.bzl", "package_info")
package_info(
name = "gazelle_generated_package_info",
package_name = {package_name},
package_url = {package_url},
package_version = {package_version},
visibility = ["//visibility:private"],
)
""".format(
package_name = repr(package_name),
Expand Down
2 changes: 1 addition & 1 deletion tests/bcr/go_mod/tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _package_info_aspect_impl(_, ctx):
attr = ctx.rule.attr.applicable_licenses
elif hasattr(ctx.rule.attr, "package_metadata"):
attr = ctx.rule.attr.package_metadata
if PackageInfo in attr[0]:
if attr and PackageInfo in attr[0]:
return [attr[0][PackageInfo]]
return []

Expand Down

0 comments on commit 811422f

Please sign in to comment.