Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created June 12, 2025 23:40
Show Gist options
  • Save fzakaria/ac72b00238d019b3082260c312253c44 to your computer and use it in GitHub Desktop.
Save fzakaria/ac72b00238d019b3082260c312253c44 to your computer and use it in GitHub Desktop.
Compile jar to source jar mapping
ClassPathInfo = provider(
"Provider for classpath information",
fields = {
"compile_jars": "A json fragment containing the mapping of the current jar to its source jar",
},
)
def _classpath_aspect_impl(target, ctx):
java_info = target[JavaInfo]
compile_jars = depset(
direct = [struct(source_jar = java_info.source_jars[0].path, class_jar = java_info.java_outputs[0].class_jar.path)],
transitive = [dep[ClassPathInfo].compile_jars for dep in ctx.rule.attr.deps],
)
json_content = (
ctx.actions.args()
.set_param_file_format("multiline") # Don't do any flags-y stuff; just write lines.
.add_all(compile_jars, map_each = json.encode)
)
json_file = ctx.actions.declare_file("{}.jsonl".format(target.label.name))
ctx.actions.write(json_file, json_content)
return [
OutputGroupInfo(
compile_jars = depset([json_file]),
),
ClassPathInfo(compile_jars = compile_jars),
]
classpath_aspect = aspect(
implementation = _classpath_aspect_impl,
# attr_aspects is a list of rule attributes along
# which the aspect propagates.
attr_aspects = ["deps"],
required_providers = [JavaInfo]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment