From 86b02b36c48ed149b585df5c5b5c2aa04484145d Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Fri, 18 Mar 2022 14:01:38 -0400 Subject: [PATCH] internal/lsp/cache: check for nil WorkFile.Go Fixes golang/vscode-go#2121 Change-Id: Icf44c8bb4079c0bcba83a9512f939260bbb5b6de Reviewed-on: https://go-review.googlesource.com/c/tools/+/393856 Trust: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Hyang-Ah Hana Kim --- internal/lsp/cache/workspace.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/lsp/cache/workspace.go b/internal/lsp/cache/workspace.go index 7a75b21567..5d62d66913 100644 --- a/internal/lsp/cache/workspace.go +++ b/internal/lsp/cache/workspace.go @@ -505,10 +505,13 @@ func parseGoWork(ctx context.Context, root, uri span.URI, contents []byte, fs so if err != nil { return nil, nil, err } - if workFile.Go.Version != "" { - if err := modFile.AddGoStmt(workFile.Go.Version); err != nil { - return nil, nil, err - } + + // Require a go directive, per the spec. + if workFile.Go == nil || workFile.Go.Version == "" { + return nil, nil, fmt.Errorf("go.work has missing or incomplete go directive") + } + if err := modFile.AddGoStmt(workFile.Go.Version); err != nil { + return nil, nil, err } return modFile, modFiles, nil