Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: add "if err != nil" snippet completion for Test/Benchmark Functions #43310

marwan-at-work opened this issue Dec 21, 2020 · 1 comment


Copy link

@marwan-at-work marwan-at-work commented Dec 21, 2020

One of my favorite features of Gopls is that it can auto-complete my error returns by writing a contextual if-statement block like so:

if err != nil {
  return <zero types>, ${1:err}

It accomplishes the above by doing the following:

  1. Checks if the enclosing function's signature returns an error
  2. Checks if the cursor's previous statement assigns an error.
  3. Offers an auto-complete snippet that writes out the entire block above.

The main logic of this feature lives here:

I propose that we update this function to include the following use case (that I find myself running into a lot):

func TestSomeLogic(t *testing.T) {
  _, err := os.Open("foo")

If the client asked for completion at the previous cursor, we should offer the following completion:

if err != nil {


Writing if err != nil { t.Fatal(err) } is quite common.

Quickly searching for the statement in popular repos I found that in the x/tools repository, this exact block appears about 393 times:

Screen Shot 2020-12-21 at 4 35 59 PM

While, in the moby/moby repository, it appears 749 times:

Screen Shot 2020-12-21 at 4 36 16 PM

Therefore, having this auto-completion will make Go developer's write tests more quickly when they need it.

In the future, more clever UX can be sketched out that will handle other common scenarios such as Fatalf but I believe as an initial implementation, t.Fatal(err) is a really good start and perhaps good enough.

Copy link

@gopherbot gopherbot commented Dec 21, 2020

Change mentions this issue: lsp/source/completion: Add snippet completion for t.Fatal errs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.