Skip to content

Use bazel query to provide completion on target names - #79

Open
blais wants to merge 13 commits into
bazel-contrib:masterfrom
blais:master
Open

Use bazel query to provide completion on target names#79
blais wants to merge 13 commits into
bazel-contrib:masterfrom
blais:master

Conversation

@blais

@blais blais commented Jun 10, 2020

Copy link
Copy Markdown
Contributor

This CL enhances the build commands by automatically inferring the name of the target from the current buffer. If multiple targets match, completion is provided with the list.

This also changes the commands to run from the root of the workspace with a cd before the command name. There's an option to turn that behavior on. I think this is the most natural way to work with bazel (cwd at root of workspace).

If the current buffer is

  • A file, bazel query is used to figure out the name of the target that the file is a src of.
  • A directory (dired mode), the list of targets in the package is provided with default :all
  • A BUILD file, the targets in the package as well.

Note that I did not add unit tests. I did check locally on all three files that it works. This PR for your consideration. I'll be running this on my box for a while. Happy to move to a branch if you prefer to work that way (let me know0.

Some further improvements are possible for the future:

  • Adding an option to disable this might be useful, as it can be a little slow.
  • When computing the completion list over a file, it may be a good idea to actually fetch it for the enclosing package, so that if the user wants to edit it there's completion from packages nearby.
  • If a single target matches the file, maybe the command should just run. Using a prefix argument to allow the user to edit the target before running, perhaps.

@blais
blais requested a review from laurentlb as a code owner June 10, 2020 04:04
@googlebot

Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot

Copy link
Copy Markdown

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@blais

blais commented Jun 10, 2020

Copy link
Copy Markdown
Contributor Author

@googlebot I signed it!

Comment thread lisp/bazel-build.el Outdated
Comment thread lisp/bazel-build.el Outdated
Comment thread lisp/bazel-build.el Outdated
Comment thread lisp/bazel-util.el
@phst

phst commented Jul 18, 2020

Copy link
Copy Markdown
Collaborator

This CL enhances the build commands by automatically inferring the name of the target from the current buffer. If multiple targets match, completion is provided with the list.

This also changes the commands to run from the root of the workspace with a cd before the command name.

Would you mind splitting up these two changes into separate PRs? They seem pretty unrelated to each other.

Comment thread lisp/bazel-build.el

(defun bazel-build--read-target (prompt &optional filename)
"Read a target name for the given or current file or dired directory name."
;; Bazel query invocation can be slow, issue a message.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think especially for large repositories or when the Bazel server needs to be restarted, this approach can be too slow for an interactive command. I think there should at least be a customization option to switch between the precise-but-slow bazel query method and a faster-but-imprecise method like the one used before.

Comment thread lisp/bazel-build.el
Comment thread lisp/bazel-build.el
(defvar bazel-query-args '("query" "--noblock_for_lock")
"Bazel query subcommand and arguments.")

(defvar bazel-run-commands-from-root nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest making this a customization option and instead of a (non-extensible) Boolean provide choices such as workspace-root and default-directory.

Comment thread lisp/bazel-build.el
Comment thread lisp/bazel-build.el
(if (/= status 0)
(with-temp-buffer
(insert-file-contents stderr-file)
(error "Error running command: %s" (buffer-string)))))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the entire standard error should be part of the error message, since it can be arbitrarily long.
I'd also suggest singaling a specific error symbol, so that callers can catch and handle the error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What summarization would you like to see?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, looking at this "holistically", I'd probably just return nil here and handle that accordingly in the completion prompt. This isn't a critical condition in the context of completion, as the user can always enter the target manually, so we souldn't fail hard here or pop up a warning.

Comment thread lisp/bazel-build.el Outdated
Comment thread lisp/bazel-build.el Outdated
Comment thread lisp/bazel-build.el
(let* ((default-directory dirname)
(results (split-string
(bazel-query "kind('.*rule', ':*')")))
(package (car (split-string (car results) ":"))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if there are no rules in the current package

Comment thread lisp/bazel-build.el
Comment thread lisp/bazel-build.el Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants