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 <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/internal/lsp/cache/workspace.go b/internal/lsp/cache/workspace.go
index 7a75b21..5d62d66 100644
--- a/internal/lsp/cache/workspace.go
+++ b/internal/lsp/cache/workspace.go
@@ -505,10 +505,13 @@
 	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