@@ -394,7 +394,7 @@ def _make_xrefs_for_arg_attr(
394394 # This allows referencing an arg as e.g `funcname.argname`
395395 anchor_id ,
396396 # This allows referencing an arg as simply `argname`
397- arg_name
397+ arg_name ,
398398 ],
399399 )
400400
@@ -503,7 +503,22 @@ def run(self) -> list[docutils_nodes.Node]:
503503 self .env .ref_context ["bzl:object_id_stack" ] = []
504504 self .env .ref_context ["bzl:doc_id_stack" ] = []
505505
506- _ , _ , basename = file_label .partition (":" )
506+ package_label , _ , basename = file_label .partition (":" )
507+
508+ # Transform //foo/bar:BUILD.bazel into "bar"
509+ # This allows referencing "bar" as itself
510+ extra_alt_names = []
511+ if basename in ("BUILD.bazel" , "BUILD" ):
512+ # Allow xref //foo
513+ extra_alt_names .append (package_label )
514+ basename = os .path .basename (package_label )
515+ # Handle //:BUILD.bazel
516+ if not basename :
517+ # There isn't a convention for referring to the root package
518+ # besides `//:`, which is already the file_label. So just
519+ # use some obvious value
520+ basename = "__ROOT_BAZEL_PACKAGE__"
521+
507522 index_description = f"File { label } "
508523 absolute_label = repo + label
509524 self .env .get_domain ("bzl" ).add_object (
@@ -527,7 +542,8 @@ def run(self) -> list[docutils_nodes.Node]:
527542 file_label ,
528543 # Allow xref bar.bzl
529544 basename ,
530- ],
545+ ]
546+ + extra_alt_names ,
531547 )
532548 index_node = addnodes .index (
533549 entries = [
0 commit comments