@@ -19,7 +19,7 @@ visibility and integration capabilities.
1919load (
2020 "@rules_license//rules:gather_licenses_info.bzl" ,
2121 "gather_licenses_info" ,
22- "write_licenses_info " ,
22+ "licenses_info_to_json " ,
2323)
2424load (
2525 "@rules_license//rules_gathering:gather_metadata.bzl" ,
@@ -31,15 +31,39 @@ load(
3131 "TransitiveLicensesInfo" ,
3232)
3333
34+ def _safe_write_licenses_info (ctx , deps , json_out ):
35+ """Writes license info as JSON, skipping targets without license data.
36+
37+ Some targets yield TransitiveLicensesInfo without target_under_license
38+ (when no licenses are found). The upstream write_licenses_info does not
39+ guard against that, so we skip such entries here.
40+ """
41+ licenses_json = []
42+ licenses_files = []
43+ for dep in deps :
44+ if TransitiveLicensesInfo in dep :
45+ transitive_licenses_info = dep [TransitiveLicensesInfo ]
46+ if not hasattr (transitive_licenses_info , "target_under_license" ):
47+ continue
48+ lic_info , lic_files = licenses_info_to_json (transitive_licenses_info )
49+ licenses_json .extend (lic_info )
50+ licenses_files .extend (lic_files )
51+
52+ ctx .actions .write (
53+ output = json_out ,
54+ content = "[\n %s\n ]\n " % ",\n " .join (licenses_json ),
55+ )
56+ return licenses_files
57+
3458def _license_report_impl (ctx ):
3559 """Implementation for license_report rule.
3660
3761 Collects license information from dependencies and writes it as JSON.
3862 Uses the official rules_license gather_licenses_info aspect.
3963 """
4064
41- # Use the official write_licenses_info function from rules_license
42- write_licenses_info (ctx , ctx .attr .deps , ctx .outputs .out )
65+ # Use a safe writer to avoid failures when no licenses are present
66+ _safe_write_licenses_info (ctx , ctx .attr .deps , ctx .outputs .out )
4367
4468 return [DefaultInfo (files = depset ([ctx .outputs .out ]))]
4569
@@ -281,7 +305,7 @@ def _enhanced_license_report_impl(ctx):
281305
282306 # Create standard JSON report using the licenses_deps with gather_licenses_info aspect
283307 json_file = ctx .actions .declare_file (ctx .label .name + ".json" )
284- write_licenses_info (ctx , ctx .attr .licenses_deps , json_file )
308+ _safe_write_licenses_info (ctx , ctx .attr .licenses_deps , json_file )
285309
286310 # Create enhanced metadata report using the metadata_deps with gather_metadata_info aspect
287311 metadata_file = ctx .actions .declare_file (ctx .label .name + "_metadata.json" )
0 commit comments