Function: bazel-show-consuming-target

bazel-show-consuming-target is an interactive and natively compiled function defined in bazel.el.

Signature

(bazel-show-consuming-target)

Documentation

Find the definition of the rule target consuming the current file.

The current buffer must visit a file, and the file must be in a Bazel repository. Use ‘xref-show-definitions-function’ to display the rule target definition. Right now, perform a best-effort attempt for finding the consuming rule target by a textual search in the BUILD file.

Key Bindings

This command is not in any keymaps.

Aliases

bazel-show-consuming-rule (obsolete since 2026-04-16)

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel-show-consuming-target ()
  "Find the definition of the rule target consuming the current file.
The current buffer must visit a file, and the file must be in a
Bazel repository.  Use ‘xref-show-definitions-function’ to display
the rule target definition.  Right now, perform a best-effort attempt
for finding the consuming rule target by a textual search in the BUILD
file."
  (declare (ftype (function () null))
           (interactive-only t))
  (interactive)
  (let* ((source-file (or buffer-file-name
                          (user-error "Buffer doesn’t visit a file")))
         (root (or (bazel--repository-root source-file)
                   (user-error "File is not in a Bazel repository")))
         (directory (or (bazel--package-directory source-file root)
                        (user-error "File is not in a Bazel package")))
         (package (or (bazel--package-name directory root)
                      (user-error "File is not in a Bazel package")))
         (build-file (or (bazel--locate-build-file directory)
                         (user-error "No BUILD file found")))
         (relative-file (file-relative-name source-file directory))
         (case-fold-file (file-name-case-insensitive-p source-file))
         (target (or (bazel--consuming-target build-file relative-file
                                              case-fold-file nil)
                     (user-error "No rule target for file %s found"
                                 relative-file)))
         ;; We press ‘xref-find-definitions’ into service for finding and
         ;; showing the rule target definition.  For that to work, our Xref
         ;; backend must be found unconditionally.
         (xref-backend-functions (list (lambda () 'bazel-mode))))
    (xref-find-definitions
     ;; Create a target identifier similar to what
     ;; ‘xref-backend-identifier-at-point’ returns.
     (propertize (bazel--canonical nil package target)
                 'bazel-mode-workspace root)))
  nil)