internal/lsp: prevent initial workspace load from being canceled

In one of the many iterations on CL 212102, the contexts propagated
through the initial workspace load were allowed to be canceled. This
should not be allowed because the initial workspace load has to be
completed.

Change-Id: I6c6273b4e58fb9041af518f329f4766ed5f1f81b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/213641
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
diff --git a/internal/lsp/general.go b/internal/lsp/general.go
index bad9734..b921590 100644
--- a/internal/lsp/general.go
+++ b/internal/lsp/general.go
@@ -17,6 +17,7 @@
 	"golang.org/x/tools/internal/lsp/source"
 	"golang.org/x/tools/internal/span"
 	"golang.org/x/tools/internal/telemetry/log"
+	"golang.org/x/tools/internal/xcontext"
 	errors "golang.org/x/xerrors"
 )
 
@@ -165,11 +166,13 @@
 
 	for _, folder := range folders {
 		uri := span.NewURI(folder.URI)
-		_, snapshot, err := s.addView(ctx, folder.Name, span.NewURI(folder.URI))
+		view, snapshot, err := s.addView(ctx, folder.Name, span.NewURI(folder.URI))
 		if err != nil {
 			viewErrors[uri] = err
 			continue
 		}
+		// Make sure that this does not get canceled.
+		ctx := xcontext.Detach(view.BackgroundContext())
 		go s.diagnoseSnapshot(ctx, snapshot)
 	}
 	if len(viewErrors) > 0 {
diff --git a/internal/lsp/workspace.go b/internal/lsp/workspace.go
index 358c839..a607d9e 100644
--- a/internal/lsp/workspace.go
+++ b/internal/lsp/workspace.go
@@ -10,6 +10,7 @@
 	"golang.org/x/tools/internal/lsp/protocol"
 	"golang.org/x/tools/internal/lsp/source"
 	"golang.org/x/tools/internal/span"
+	"golang.org/x/tools/internal/xcontext"
 	errors "golang.org/x/xerrors"
 )
 
@@ -52,6 +53,8 @@
 		if err != nil {
 			return err
 		}
+		// Make sure that this does not get canceled.
+		ctx := xcontext.Detach(view.BackgroundContext())
 		go s.diagnoseSnapshot(ctx, view.Snapshot())
 	}
 	return nil