Skip to content

Commit

Permalink
Change fix to ensure that we return a valid license info.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrindel committed Oct 2, 2024
1 parent 0d6cc0b commit ed5bb25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
13 changes: 4 additions & 9 deletions rules/gather_licenses_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def _strip_null_repo(label):
The is to make str(label) compatible between bazel 5.x and 6.x
"""
s = str(label)
if s.startswith("@//"):
if s.startswith('@//'):
return s[1:]
elif s.startswith("@@//"):
elif s.startswith('@@//'):
return s[2:]
return s

Expand Down Expand Up @@ -150,7 +150,7 @@ def write_licenses_info(ctx, deps, json_out):
licenses_files = []
for dep in deps:
if TransitiveLicensesInfo in dep:
transitive_licenses_info = dep[TransitiveLicensesInfo]
transitive_licenses_info = dep[TransitiveLicensesInfo]
lic_info, _ = licenses_info_to_json(transitive_licenses_info)
licenses_json.extend(lic_info)
for info in transitive_licenses_info.licenses.to_list():
Expand Down Expand Up @@ -253,7 +253,6 @@ def licenses_info_to_json(licenses_info):
label = _strip_null_repo(license.label),
used_by = ",\n ".join(sorted(['"%s"' % x for x in used_by[str(license.label)]])),
))

# Additionally return all File references so that other rules invoking
# this method can load license text file contents from external repos
# using runfiles
Expand All @@ -267,12 +266,8 @@ def licenses_info_to_json(licenses_info):
licenses = ",\n ".join(sorted(['"%s"' % _strip_null_repo(x) for x in dep_licenses])),
))

top_level_target = ""
if hasattr(licenses_info, "target_under_license"):
top_level_target = licenses_info.target_under_license

return [main_template.format(
top_level_target = _strip_null_repo(top_level_target),
top_level_target = _strip_null_repo(licenses_info.target_under_license),
dependencies = ",".join(all_deps),
licenses = ",".join(all_licenses),
)], all_license_text_files
14 changes: 12 additions & 2 deletions rules/licenses_core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ def gather_metadata_info_common(target, ctx, provider_factory, metadata_provider
# fully resolved. If exec is in the bin_dir path, then the current
# configuration is probably cfg = exec.
if "-exec-" in ctx.bin_dir.path:
return [provider_factory(deps = depset(), licenses = depset(), traces = [])]
return [provider_factory(
target_under_license = target.label,
deps = depset(),
licenses = depset(),
traces = [],
)]

# Now gather transitive collection of providers from the targets
# this target depends upon.
Expand All @@ -164,7 +169,12 @@ def gather_metadata_info_common(target, ctx, provider_factory, metadata_provider
_get_transitive_metadata(ctx, trans_licenses, trans_other_metadata, trans_package_info, trans_deps, traces, provider_factory, filter_func)

if not licenses and not trans_licenses:
return [provider_factory(deps = depset(), licenses = depset(), traces = [])]
return [provider_factory(
target_under_license = target.label,
deps = depset(),
licenses = depset(),
traces = [],
)]

# If this is the target, start the sequence of traces.
if ctx.attr._trace[TraceInfo].trace and ctx.attr._trace[TraceInfo].trace in str(ctx.label):
Expand Down

0 comments on commit ed5bb25

Please sign in to comment.