1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package cache implements the caching layer for gopls.
6package cache
7
8import (
9	"context"
10	"encoding/json"
11	"fmt"
12	"io"
13	"io/ioutil"
14	"os"
15	"path"
16	"path/filepath"
17	"reflect"
18	"regexp"
19	"sort"
20	"strings"
21	"sync"
22
23	"golang.org/x/mod/modfile"
24	"golang.org/x/mod/semver"
25	exec "golang.org/x/sys/execabs"
26	"golang.org/x/tools/go/packages"
27	"golang.org/x/tools/internal/event"
28	"golang.org/x/tools/internal/gocommand"
29	"golang.org/x/tools/internal/imports"
30	"golang.org/x/tools/internal/lsp/protocol"
31	"golang.org/x/tools/internal/lsp/source"
32	"golang.org/x/tools/internal/memoize"
33	"golang.org/x/tools/internal/span"
34	"golang.org/x/tools/internal/xcontext"
35	errors "golang.org/x/xerrors"
36)
37
38type View struct {
39	session *Session
40	id      string
41
42	optionsMu sync.Mutex
43	options   *source.Options
44
45	// mu protects most mutable state of the view.
46	mu sync.Mutex
47
48	// baseCtx is the context handed to NewView. This is the parent of all
49	// background contexts created for this view.
50	baseCtx context.Context
51
52	// cancel is called when all action being performed by the current view
53	// should be stopped.
54	cancel context.CancelFunc
55
56	// name is the user visible name of this view.
57	name string
58
59	// folder is the folder with which this view was constructed.
60	folder span.URI
61
62	importsState *importsState
63
64	// moduleUpgrades tracks known upgrades for module paths.
65	moduleUpgrades map[string]string
66
67	// keep track of files by uri and by basename, a single file may be mapped
68	// to multiple uris, and the same basename may map to multiple files
69	filesByURI  map[span.URI]*fileBase
70	filesByBase map[string][]*fileBase
71
72	// initCancelFirstAttempt can be used to terminate the view's first
73	// attempt at initialization.
74	initCancelFirstAttempt context.CancelFunc
75
76	snapshotMu sync.Mutex
77	snapshot   *snapshot
78
79	// initialWorkspaceLoad is closed when the first workspace initialization has
80	// completed. If we failed to load, we only retry if the go.mod file changes,
81	// to avoid too many go/packages calls.
82	initialWorkspaceLoad chan struct{}
83
84	// initializationSema is used limit concurrent initialization of snapshots in
85	// the view. We use a channel instead of a mutex to avoid blocking when a
86	// context is canceled.
87	initializationSema chan struct{}
88
89	// rootURI is the rootURI directory of this view. If we are in GOPATH mode, this
90	// is just the folder. If we are in module mode, this is the module rootURI.
91	rootURI span.URI
92
93	// workspaceInformation tracks various details about this view's
94	// environment variables, go version, and use of modules.
95	workspaceInformation
96
97	// tempWorkspace is a temporary directory dedicated to holding the latest
98	// version of the workspace go.mod file. (TODO: also go.sum file)
99	tempWorkspace span.URI
100}
101
102type workspaceInformation struct {
103	// The Go version in use: X in Go 1.X.
104	goversion int
105
106	// hasGopackagesDriver is true if the user has a value set for the
107	// GOPACKAGESDRIVER environment variable or a gopackagesdriver binary on
108	// their machine.
109	hasGopackagesDriver bool
110
111	// `go env` variables that need to be tracked by gopls.
112	environmentVariables
113
114	// userGo111Module is the user's value of GO111MODULE.
115	userGo111Module go111module
116
117	// The value of GO111MODULE we want to run with.
118	effectiveGo111Module string
119
120	// goEnv is the `go env` output collected when a view is created.
121	// It includes the values of the environment variables above.
122	goEnv map[string]string
123}
124
125type go111module int
126
127const (
128	off = go111module(iota)
129	auto
130	on
131)
132
133type environmentVariables struct {
134	gocache, gopath, goroot, goprivate, gomodcache, go111module string
135}
136
137type workspaceMode int
138
139const (
140	moduleMode workspaceMode = 1 << iota
141
142	// tempModfile indicates whether or not the -modfile flag should be used.
143	tempModfile
144
145	// usesWorkspaceModule indicates support for the experimental workspace module
146	// feature.
147	usesWorkspaceModule
148)
149
150type builtinPackageHandle struct {
151	handle *memoize.Handle
152}
153
154// fileBase holds the common functionality for all files.
155// It is intended to be embedded in the file implementations
156type fileBase struct {
157	uris  []span.URI
158	fname string
159
160	view *View
161}
162
163func (f *fileBase) URI() span.URI {
164	return f.uris[0]
165}
166
167func (f *fileBase) filename() string {
168	return f.fname
169}
170
171func (f *fileBase) addURI(uri span.URI) int {
172	f.uris = append(f.uris, uri)
173	return len(f.uris)
174}
175
176func (v *View) ID() string { return v.id }
177
178// tempModFile creates a temporary go.mod file based on the contents of the
179// given go.mod file. It is the caller's responsibility to clean up the files
180// when they are done using them.
181func tempModFile(modFh source.FileHandle, gosum []byte) (tmpURI span.URI, cleanup func(), err error) {
182	filenameHash := hashContents([]byte(modFh.URI().Filename()))
183	tmpMod, err := ioutil.TempFile("", fmt.Sprintf("go.%s.*.mod", filenameHash))
184	if err != nil {
185		return "", nil, err
186	}
187	defer tmpMod.Close()
188
189	tmpURI = span.URIFromPath(tmpMod.Name())
190	tmpSumName := sumFilename(tmpURI)
191
192	content, err := modFh.Read()
193	if err != nil {
194		return "", nil, err
195	}
196
197	if _, err := tmpMod.Write(content); err != nil {
198		return "", nil, err
199	}
200
201	cleanup = func() {
202		_ = os.Remove(tmpSumName)
203		_ = os.Remove(tmpURI.Filename())
204	}
205
206	// Be careful to clean up if we return an error from this function.
207	defer func() {
208		if err != nil {
209			cleanup()
210			cleanup = nil
211		}
212	}()
213
214	// Create an analogous go.sum, if one exists.
215	if gosum != nil {
216		if err := ioutil.WriteFile(tmpSumName, gosum, 0655); err != nil {
217			return "", cleanup, err
218		}
219	}
220
221	return tmpURI, cleanup, nil
222}
223
224// Name returns the user visible name of this view.
225func (v *View) Name() string {
226	return v.name
227}
228
229// Folder returns the folder at the base of this view.
230func (v *View) Folder() span.URI {
231	return v.folder
232}
233
234func (v *View) TempWorkspace() span.URI {
235	return v.tempWorkspace
236}
237
238func (v *View) Options() *source.Options {
239	v.optionsMu.Lock()
240	defer v.optionsMu.Unlock()
241	return v.options
242}
243
244func minorOptionsChange(a, b *source.Options) bool {
245	// Check if any of the settings that modify our understanding of files have been changed
246	if !reflect.DeepEqual(a.Env, b.Env) {
247		return false
248	}
249	if !reflect.DeepEqual(a.DirectoryFilters, b.DirectoryFilters) {
250		return false
251	}
252	if a.MemoryMode != b.MemoryMode {
253		return false
254	}
255	aBuildFlags := make([]string, len(a.BuildFlags))
256	bBuildFlags := make([]string, len(b.BuildFlags))
257	copy(aBuildFlags, a.BuildFlags)
258	copy(bBuildFlags, b.BuildFlags)
259	sort.Strings(aBuildFlags)
260	sort.Strings(bBuildFlags)
261	// the rest of the options are benign
262	return reflect.DeepEqual(aBuildFlags, bBuildFlags)
263}
264
265func (v *View) SetOptions(ctx context.Context, options *source.Options) (source.View, error) {
266	// no need to rebuild the view if the options were not materially changed
267	v.optionsMu.Lock()
268	if minorOptionsChange(v.options, options) {
269		v.options = options
270		v.optionsMu.Unlock()
271		return v, nil
272	}
273	v.optionsMu.Unlock()
274	newView, err := v.session.updateView(ctx, v, options)
275	return newView, err
276}
277
278func (v *View) Rebuild(ctx context.Context) (source.Snapshot, func(), error) {
279	newView, err := v.session.updateView(ctx, v, v.Options())
280	if err != nil {
281		return nil, func() {}, err
282	}
283	snapshot, release := newView.Snapshot(ctx)
284	return snapshot, release, nil
285}
286
287func (s *snapshot) WriteEnv(ctx context.Context, w io.Writer) error {
288	s.view.optionsMu.Lock()
289	env := s.view.options.EnvSlice()
290	buildFlags := append([]string{}, s.view.options.BuildFlags...)
291	s.view.optionsMu.Unlock()
292
293	fullEnv := make(map[string]string)
294	for k, v := range s.view.goEnv {
295		fullEnv[k] = v
296	}
297	for _, v := range env {
298		s := strings.SplitN(v, "=", 2)
299		if len(s) != 2 {
300			continue
301		}
302		if _, ok := fullEnv[s[0]]; ok {
303			fullEnv[s[0]] = s[1]
304		}
305	}
306	goVersion, err := s.view.session.gocmdRunner.Run(ctx, gocommand.Invocation{
307		Verb:       "version",
308		Env:        env,
309		WorkingDir: s.view.rootURI.Filename(),
310	})
311	if err != nil {
312		return err
313	}
314	fmt.Fprintf(w, `go env for %v
315(root %s)
316(go version %s)
317(valid build configuration = %v)
318(build flags: %v)
319`,
320		s.view.folder.Filename(),
321		s.view.rootURI.Filename(),
322		strings.TrimRight(goVersion.String(), "\n"),
323		s.ValidBuildConfiguration(),
324		buildFlags)
325	for k, v := range fullEnv {
326		fmt.Fprintf(w, "%s=%s\n", k, v)
327	}
328	return nil
329}
330
331func (s *snapshot) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error) error {
332	return s.view.importsState.runProcessEnvFunc(ctx, s, fn)
333}
334
335func (s *snapshot) locateTemplateFiles(ctx context.Context) {
336	if !s.view.Options().ExperimentalTemplateSupport {
337		return
338	}
339	dir := s.workspace.root.Filename()
340	searched := 0
341	// Change to WalkDir when we move up to 1.16
342	err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
343		if err != nil {
344			return err
345		}
346		if strings.HasSuffix(filepath.Ext(path), "tmpl") && !pathExcludedByFilter(path, dir, s.view.gomodcache, s.view.options) &&
347			!fi.IsDir() {
348			k := span.URIFromPath(path)
349			fh, err := s.GetVersionedFile(ctx, k)
350			if err != nil {
351				return nil
352			}
353			s.files[k] = fh
354		}
355		searched++
356		if fileLimit > 0 && searched > fileLimit {
357			return errExhausted
358		}
359		return nil
360	})
361	if err != nil {
362		event.Error(ctx, "searching for template files failed", err)
363	}
364}
365
366func (v *View) contains(uri span.URI) bool {
367	inRoot := source.InDir(v.rootURI.Filename(), uri.Filename())
368	inFolder := source.InDir(v.folder.Filename(), uri.Filename())
369	if !inRoot && !inFolder {
370		return false
371	}
372	// Filters are applied relative to the workspace folder.
373	if inFolder {
374		return !pathExcludedByFilter(strings.TrimPrefix(uri.Filename(), v.folder.Filename()), v.rootURI.Filename(), v.gomodcache, v.Options())
375	}
376	return true
377}
378
379func (v *View) mapFile(uri span.URI, f *fileBase) {
380	v.filesByURI[uri] = f
381	if f.addURI(uri) == 1 {
382		basename := basename(f.filename())
383		v.filesByBase[basename] = append(v.filesByBase[basename], f)
384	}
385}
386
387func basename(filename string) string {
388	return strings.ToLower(filepath.Base(filename))
389}
390
391func (v *View) relevantChange(c source.FileModification) bool {
392	// If the file is known to the view, the change is relevant.
393	if v.knownFile(c.URI) {
394		return true
395	}
396	// The gopls.mod may not be "known" because we first access it through the
397	// session. As a result, treat changes to the view's gopls.mod file as
398	// always relevant, even if they are only on-disk changes.
399	// TODO(rstambler): Make sure the gopls.mod is always known to the view.
400	if c.URI == goplsModURI(v.rootURI) {
401		return true
402	}
403	// If the file is not known to the view, and the change is only on-disk,
404	// we should not invalidate the snapshot. This is necessary because Emacs
405	// sends didChangeWatchedFiles events for temp files.
406	if c.OnDisk && (c.Action == source.Change || c.Action == source.Delete) {
407		return false
408	}
409	return v.contains(c.URI)
410}
411
412func (v *View) knownFile(uri span.URI) bool {
413	v.mu.Lock()
414	defer v.mu.Unlock()
415
416	f, err := v.findFile(uri)
417	return f != nil && err == nil
418}
419
420// getFile returns a file for the given URI.
421func (v *View) getFile(uri span.URI) *fileBase {
422	v.mu.Lock()
423	defer v.mu.Unlock()
424
425	f, _ := v.findFile(uri)
426	if f != nil {
427		return f
428	}
429	f = &fileBase{
430		view:  v,
431		fname: uri.Filename(),
432	}
433	v.mapFile(uri, f)
434	return f
435}
436
437// findFile checks the cache for any file matching the given uri.
438//
439// An error is only returned for an irreparable failure, for example, if the
440// filename in question does not exist.
441func (v *View) findFile(uri span.URI) (*fileBase, error) {
442	if f := v.filesByURI[uri]; f != nil {
443		// a perfect match
444		return f, nil
445	}
446	// no exact match stored, time to do some real work
447	// check for any files with the same basename
448	fname := uri.Filename()
449	basename := basename(fname)
450	if candidates := v.filesByBase[basename]; candidates != nil {
451		pathStat, err := os.Stat(fname)
452		if os.IsNotExist(err) {
453			return nil, err
454		}
455		if err != nil {
456			return nil, nil // the file may exist, return without an error
457		}
458		for _, c := range candidates {
459			if cStat, err := os.Stat(c.filename()); err == nil {
460				if os.SameFile(pathStat, cStat) {
461					// same file, map it
462					v.mapFile(uri, c)
463					return c, nil
464				}
465			}
466		}
467	}
468	// no file with a matching name was found, it wasn't in our cache
469	return nil, nil
470}
471
472func (v *View) Shutdown(ctx context.Context) {
473	v.session.removeView(ctx, v)
474}
475
476// TODO(rFindley): probably some of this should also be one in View.Shutdown
477// above?
478func (v *View) shutdown(ctx context.Context) {
479	// Cancel the initial workspace load if it is still running.
480	v.initCancelFirstAttempt()
481
482	v.mu.Lock()
483	if v.cancel != nil {
484		v.cancel()
485		v.cancel = nil
486	}
487	v.mu.Unlock()
488	v.snapshotMu.Lock()
489	go v.snapshot.generation.Destroy()
490	v.snapshotMu.Unlock()
491	v.importsState.destroy()
492}
493
494func (v *View) Session() *Session {
495	return v.session
496}
497
498func (s *snapshot) IgnoredFile(uri span.URI) bool {
499	filename := uri.Filename()
500	var prefixes []string
501	if len(s.workspace.getActiveModFiles()) == 0 {
502		for _, entry := range filepath.SplitList(s.view.gopath) {
503			prefixes = append(prefixes, filepath.Join(entry, "src"))
504		}
505	} else {
506		prefixes = append(prefixes, s.view.gomodcache)
507		for m := range s.workspace.getActiveModFiles() {
508			prefixes = append(prefixes, dirURI(m).Filename())
509		}
510	}
511	for _, prefix := range prefixes {
512		if strings.HasPrefix(filename, prefix) {
513			return checkIgnored(filename[len(prefix):])
514		}
515	}
516	return false
517}
518
519// checkIgnored implements go list's exclusion rules. go help list:
520// 		Directory and file names that begin with "." or "_" are ignored
521// 		by the go tool, as are directories named "testdata".
522func checkIgnored(suffix string) bool {
523	for _, component := range strings.Split(suffix, string(filepath.Separator)) {
524		if len(component) == 0 {
525			continue
526		}
527		if component[0] == '.' || component[0] == '_' || component == "testdata" {
528			return true
529		}
530	}
531	return false
532}
533
534func (v *View) Snapshot(ctx context.Context) (source.Snapshot, func()) {
535	return v.getSnapshot(ctx)
536}
537
538func (v *View) getSnapshot(ctx context.Context) (*snapshot, func()) {
539	v.snapshotMu.Lock()
540	defer v.snapshotMu.Unlock()
541	return v.snapshot, v.snapshot.generation.Acquire(ctx)
542}
543
544func (s *snapshot) initialize(ctx context.Context, firstAttempt bool) {
545	select {
546	case <-ctx.Done():
547		return
548	case s.view.initializationSema <- struct{}{}:
549	}
550
551	defer func() {
552		<-s.view.initializationSema
553	}()
554
555	if s.initializeOnce == nil {
556		return
557	}
558	s.initializeOnce.Do(func() {
559		s.loadWorkspace(ctx, firstAttempt)
560		s.collectAllKnownSubdirs(ctx)
561	})
562}
563
564func (s *snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) {
565	defer func() {
566		s.initializeOnce = nil
567		if firstAttempt {
568			close(s.view.initialWorkspaceLoad)
569		}
570	}()
571
572	// If we have multiple modules, we need to load them by paths.
573	var scopes []interface{}
574	var modDiagnostics []*source.Diagnostic
575	addError := func(uri span.URI, err error) {
576		modDiagnostics = append(modDiagnostics, &source.Diagnostic{
577			URI:      uri,
578			Severity: protocol.SeverityError,
579			Source:   source.ListError,
580			Message:  err.Error(),
581		})
582	}
583	s.locateTemplateFiles(ctx)
584	if len(s.workspace.getActiveModFiles()) > 0 {
585		for modURI := range s.workspace.getActiveModFiles() {
586			fh, err := s.GetFile(ctx, modURI)
587			if err != nil {
588				addError(modURI, err)
589				continue
590			}
591			parsed, err := s.ParseMod(ctx, fh)
592			if err != nil {
593				addError(modURI, err)
594				continue
595			}
596			if parsed.File == nil || parsed.File.Module == nil {
597				addError(modURI, fmt.Errorf("no module path for %s", modURI))
598				continue
599			}
600			path := parsed.File.Module.Mod.Path
601			scopes = append(scopes, moduleLoadScope(path))
602		}
603	} else {
604		scopes = append(scopes, viewLoadScope("LOAD_VIEW"))
605	}
606	var err error
607	if len(scopes) > 0 {
608		err = s.load(ctx, firstAttempt, append(scopes, packagePath("builtin"))...)
609	}
610	if ctx.Err() != nil {
611		return
612	}
613
614	var criticalErr *source.CriticalError
615	if err != nil {
616		event.Error(ctx, "initial workspace load failed", err)
617		extractedDiags, _ := s.extractGoCommandErrors(ctx, err.Error())
618		criticalErr = &source.CriticalError{
619			MainError: err,
620			DiagList:  append(modDiagnostics, extractedDiags...),
621		}
622	} else if len(modDiagnostics) == 1 {
623		criticalErr = &source.CriticalError{
624			MainError: fmt.Errorf(modDiagnostics[0].Message),
625			DiagList:  modDiagnostics,
626		}
627	} else if len(modDiagnostics) > 1 {
628		criticalErr = &source.CriticalError{
629			MainError: fmt.Errorf("error loading module names"),
630			DiagList:  modDiagnostics,
631		}
632	}
633
634	// Lock the snapshot when setting the initialized error.
635	s.mu.Lock()
636	defer s.mu.Unlock()
637	s.initializedErr = criticalErr
638}
639
640// invalidateContent invalidates the content of a Go file,
641// including any position and type information that depends on it.
642func (v *View) invalidateContent(ctx context.Context, changes map[span.URI]*fileChange, forceReloadMetadata bool) (*snapshot, func()) {
643	// Detach the context so that content invalidation cannot be canceled.
644	ctx = xcontext.Detach(ctx)
645
646	// Cancel all still-running previous requests, since they would be
647	// operating on stale data.
648	v.snapshot.cancel()
649
650	// Do not clone a snapshot until its view has finished initializing.
651	v.snapshot.AwaitInitialized(ctx)
652
653	// This should be the only time we hold the view's snapshot lock for any period of time.
654	v.snapshotMu.Lock()
655	defer v.snapshotMu.Unlock()
656
657	oldSnapshot := v.snapshot
658
659	var workspaceChanged bool
660	v.snapshot, workspaceChanged = oldSnapshot.clone(ctx, v.baseCtx, changes, forceReloadMetadata)
661	if workspaceChanged && v.tempWorkspace != "" {
662		snap := v.snapshot
663		release := snap.generation.Acquire(ctx)
664		go func() {
665			defer release()
666			wsdir, err := snap.getWorkspaceDir(ctx)
667			if err != nil {
668				event.Error(ctx, "getting workspace dir", err)
669			}
670			if err := copyWorkspace(v.tempWorkspace, wsdir); err != nil {
671				event.Error(ctx, "copying workspace dir", err)
672			}
673		}()
674	}
675	go oldSnapshot.generation.Destroy()
676
677	return v.snapshot, v.snapshot.generation.Acquire(ctx)
678}
679
680func copyWorkspace(dst span.URI, src span.URI) error {
681	for _, name := range []string{"go.mod", "go.sum"} {
682		srcname := filepath.Join(src.Filename(), name)
683		srcf, err := os.Open(srcname)
684		if err != nil {
685			return errors.Errorf("opening snapshot %s: %w", name, err)
686		}
687		defer srcf.Close()
688		dstname := filepath.Join(dst.Filename(), name)
689		dstf, err := os.Create(dstname)
690		if err != nil {
691			return errors.Errorf("truncating view %s: %w", name, err)
692		}
693		defer dstf.Close()
694		if _, err := io.Copy(dstf, srcf); err != nil {
695			return errors.Errorf("copying %s: %w", name, err)
696		}
697	}
698	return nil
699}
700
701func (s *Session) getWorkspaceInformation(ctx context.Context, folder span.URI, options *source.Options) (*workspaceInformation, error) {
702	if err := checkPathCase(folder.Filename()); err != nil {
703		return nil, errors.Errorf("invalid workspace configuration: %w", err)
704	}
705	var err error
706	inv := gocommand.Invocation{
707		WorkingDir: folder.Filename(),
708		Env:        options.EnvSlice(),
709	}
710	goversion, err := gocommand.GoVersion(ctx, inv, s.gocmdRunner)
711	if err != nil {
712		return nil, err
713	}
714
715	go111module := os.Getenv("GO111MODULE")
716	if v, ok := options.Env["GO111MODULE"]; ok {
717		go111module = v
718	}
719	// Make sure to get the `go env` before continuing with initialization.
720	envVars, env, err := s.getGoEnv(ctx, folder.Filename(), goversion, go111module, options.EnvSlice())
721	if err != nil {
722		return nil, err
723	}
724	// If using 1.16, change the default back to auto. The primary effect of
725	// GO111MODULE=on is to break GOPATH, which we aren't too interested in.
726	if goversion >= 16 && go111module == "" {
727		go111module = "auto"
728	}
729	// The value of GOPACKAGESDRIVER is not returned through the go command.
730	gopackagesdriver := os.Getenv("GOPACKAGESDRIVER")
731	for _, s := range env {
732		split := strings.SplitN(s, "=", 2)
733		if split[0] == "GOPACKAGESDRIVER" {
734			gopackagesdriver = split[1]
735		}
736	}
737
738	// A user may also have a gopackagesdriver binary on their machine, which
739	// works the same way as setting GOPACKAGESDRIVER.
740	tool, _ := exec.LookPath("gopackagesdriver")
741	hasGopackagesDriver := gopackagesdriver != "off" && (gopackagesdriver != "" || tool != "")
742
743	return &workspaceInformation{
744		hasGopackagesDriver:  hasGopackagesDriver,
745		effectiveGo111Module: go111module,
746		userGo111Module:      go111moduleForVersion(go111module, goversion),
747		goversion:            goversion,
748		environmentVariables: envVars,
749		goEnv:                env,
750	}, nil
751}
752
753func go111moduleForVersion(go111module string, goversion int) go111module {
754	// Off by default until Go 1.12.
755	if go111module == "off" || (goversion < 12 && go111module == "") {
756		return off
757	}
758	// On by default as of Go 1.16.
759	if go111module == "on" || (goversion >= 16 && go111module == "") {
760		return on
761	}
762	return auto
763}
764
765// findWorkspaceRoot searches for the best workspace root according to the
766// following heuristics:
767//   - First, look for a parent directory containing a gopls.mod file
768//     (experimental only).
769//   - Then, a parent directory containing a go.mod file.
770//   - Then, a child directory containing a go.mod file, if there is exactly
771//     one (non-experimental only).
772// Otherwise, it returns folder.
773// TODO (rFindley): move this to workspace.go
774// TODO (rFindley): simplify this once workspace modules are enabled by default.
775func findWorkspaceRoot(ctx context.Context, folder span.URI, fs source.FileSource, excludePath func(string) bool, experimental bool) (span.URI, error) {
776	patterns := []string{"go.mod"}
777	if experimental {
778		patterns = []string{"gopls.mod", "go.mod"}
779	}
780	for _, basename := range patterns {
781		dir, err := findRootPattern(ctx, folder, basename, fs)
782		if err != nil {
783			return "", errors.Errorf("finding %s: %w", basename, err)
784		}
785		if dir != "" {
786			return dir, nil
787		}
788	}
789
790	// The experimental workspace can handle nested modules at this point...
791	if experimental {
792		return folder, nil
793	}
794
795	// ...else we should check if there's exactly one nested module.
796	all, err := findModules(folder, excludePath, 2)
797	if err == errExhausted {
798		// Fall-back behavior: if we don't find any modules after searching 10000
799		// files, assume there are none.
800		event.Log(ctx, fmt.Sprintf("stopped searching for modules after %d files", fileLimit))
801		return folder, nil
802	}
803	if err != nil {
804		return "", err
805	}
806	if len(all) == 1 {
807		// range to access first element.
808		for uri := range all {
809			return dirURI(uri), nil
810		}
811	}
812	return folder, nil
813}
814
815func findRootPattern(ctx context.Context, folder span.URI, basename string, fs source.FileSource) (span.URI, error) {
816	dir := folder.Filename()
817	for dir != "" {
818		target := filepath.Join(dir, basename)
819		exists, err := fileExists(ctx, span.URIFromPath(target), fs)
820		if err != nil {
821			return "", err
822		}
823		if exists {
824			return span.URIFromPath(dir), nil
825		}
826		next, _ := filepath.Split(dir)
827		if next == dir {
828			break
829		}
830		dir = next
831	}
832	return "", nil
833}
834
835// OS-specific path case check, for case-insensitive filesystems.
836var checkPathCase = defaultCheckPathCase
837
838func defaultCheckPathCase(path string) error {
839	return nil
840}
841
842func validBuildConfiguration(folder span.URI, ws *workspaceInformation, modFiles map[span.URI]struct{}) bool {
843	// Since we only really understand the `go` command, if the user has a
844	// different GOPACKAGESDRIVER, assume that their configuration is valid.
845	if ws.hasGopackagesDriver {
846		return true
847	}
848	// Check if the user is working within a module or if we have found
849	// multiple modules in the workspace.
850	if len(modFiles) > 0 {
851		return true
852	}
853	// The user may have a multiple directories in their GOPATH.
854	// Check if the workspace is within any of them.
855	for _, gp := range filepath.SplitList(ws.gopath) {
856		if source.InDir(filepath.Join(gp, "src"), folder.Filename()) {
857			return true
858		}
859	}
860	return false
861}
862
863// getGoEnv gets the view's various GO* values.
864func (s *Session) getGoEnv(ctx context.Context, folder string, goversion int, go111module string, configEnv []string) (environmentVariables, map[string]string, error) {
865	envVars := environmentVariables{}
866	vars := map[string]*string{
867		"GOCACHE":     &envVars.gocache,
868		"GOPATH":      &envVars.gopath,
869		"GOROOT":      &envVars.goroot,
870		"GOPRIVATE":   &envVars.goprivate,
871		"GOMODCACHE":  &envVars.gomodcache,
872		"GO111MODULE": &envVars.go111module,
873	}
874
875	// We can save ~200 ms by requesting only the variables we care about.
876	args := append([]string{"-json"}, imports.RequiredGoEnvVars...)
877	for k := range vars {
878		args = append(args, k)
879	}
880
881	inv := gocommand.Invocation{
882		Verb:       "env",
883		Args:       args,
884		Env:        configEnv,
885		WorkingDir: folder,
886	}
887	// Don't go through runGoCommand, as we don't need a temporary -modfile to
888	// run `go env`.
889	stdout, err := s.gocmdRunner.Run(ctx, inv)
890	if err != nil {
891		return environmentVariables{}, nil, err
892	}
893	env := make(map[string]string)
894	if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
895		return environmentVariables{}, nil, err
896	}
897
898	for key, ptr := range vars {
899		*ptr = env[key]
900	}
901
902	// Old versions of Go don't have GOMODCACHE, so emulate it.
903	if envVars.gomodcache == "" && envVars.gopath != "" {
904		envVars.gomodcache = filepath.Join(filepath.SplitList(envVars.gopath)[0], "pkg/mod")
905	}
906	// GO111MODULE does not appear in `go env` output until Go 1.13.
907	if goversion < 13 {
908		envVars.go111module = go111module
909	}
910	return envVars, env, err
911}
912
913func (v *View) IsGoPrivatePath(target string) bool {
914	return globsMatchPath(v.goprivate, target)
915}
916
917func (v *View) ModuleUpgrades() map[string]string {
918	v.mu.Lock()
919	defer v.mu.Unlock()
920
921	upgrades := map[string]string{}
922	for mod, ver := range v.moduleUpgrades {
923		upgrades[mod] = ver
924	}
925	return upgrades
926}
927
928func (v *View) RegisterModuleUpgrades(upgrades map[string]string) {
929	v.mu.Lock()
930	defer v.mu.Unlock()
931
932	for mod, ver := range upgrades {
933		v.moduleUpgrades[mod] = ver
934	}
935}
936
937// Copied from
938// https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/str/path.go;l=58;drc=2910c5b4a01a573ebc97744890a07c1a3122c67a
939func globsMatchPath(globs, target string) bool {
940	for globs != "" {
941		// Extract next non-empty glob in comma-separated list.
942		var glob string
943		if i := strings.Index(globs, ","); i >= 0 {
944			glob, globs = globs[:i], globs[i+1:]
945		} else {
946			glob, globs = globs, ""
947		}
948		if glob == "" {
949			continue
950		}
951
952		// A glob with N+1 path elements (N slashes) needs to be matched
953		// against the first N+1 path elements of target,
954		// which end just before the N+1'th slash.
955		n := strings.Count(glob, "/")
956		prefix := target
957		// Walk target, counting slashes, truncating at the N+1'th slash.
958		for i := 0; i < len(target); i++ {
959			if target[i] == '/' {
960				if n == 0 {
961					prefix = target[:i]
962					break
963				}
964				n--
965			}
966		}
967		if n > 0 {
968			// Not enough prefix elements.
969			continue
970		}
971		matched, _ := path.Match(glob, prefix)
972		if matched {
973			return true
974		}
975	}
976	return false
977}
978
979var modFlagRegexp = regexp.MustCompile(`-mod[ =](\w+)`)
980
981// TODO(rstambler): Consolidate modURI and modContent back into a FileHandle
982// after we have a version of the workspace go.mod file on disk. Getting a
983// FileHandle from the cache for temporary files is problematic, since we
984// cannot delete it.
985func (s *snapshot) vendorEnabled(ctx context.Context, modURI span.URI, modContent []byte) (bool, error) {
986	if s.workspaceMode()&moduleMode == 0 {
987		return false, nil
988	}
989	matches := modFlagRegexp.FindStringSubmatch(s.view.goEnv["GOFLAGS"])
990	var modFlag string
991	if len(matches) != 0 {
992		modFlag = matches[1]
993	}
994	if modFlag != "" {
995		// Don't override an explicit '-mod=vendor' argument.
996		// We do want to override '-mod=readonly': it would break various module code lenses,
997		// and on 1.16 we know -modfile is available, so we won't mess with go.mod anyway.
998		return modFlag == "vendor", nil
999	}
1000
1001	modFile, err := modfile.Parse(modURI.Filename(), modContent, nil)
1002	if err != nil {
1003		return false, err
1004	}
1005	if fi, err := os.Stat(filepath.Join(s.view.rootURI.Filename(), "vendor")); err != nil || !fi.IsDir() {
1006		return false, nil
1007	}
1008	vendorEnabled := modFile.Go != nil && modFile.Go.Version != "" && semver.Compare("v"+modFile.Go.Version, "v1.14") >= 0
1009	return vendorEnabled, nil
1010}
1011
1012func (v *View) allFilesExcluded(pkg *packages.Package) bool {
1013	opts := v.Options()
1014	folder := filepath.ToSlash(v.folder.Filename())
1015	for _, f := range pkg.GoFiles {
1016		f = filepath.ToSlash(f)
1017		if !strings.HasPrefix(f, folder) {
1018			return false
1019		}
1020		if !pathExcludedByFilter(strings.TrimPrefix(f, folder), v.rootURI.Filename(), v.gomodcache, opts) {
1021			return false
1022		}
1023	}
1024	return true
1025}
1026
1027func pathExcludedByFilterFunc(root, gomodcache string, opts *source.Options) func(string) bool {
1028	return func(path string) bool {
1029		return pathExcludedByFilter(path, root, gomodcache, opts)
1030	}
1031}
1032
1033func pathExcludedByFilter(path, root, gomodcache string, opts *source.Options) bool {
1034	path = strings.TrimPrefix(filepath.ToSlash(path), "/")
1035	gomodcache = strings.TrimPrefix(filepath.ToSlash(strings.TrimPrefix(gomodcache, root)), "/")
1036
1037	excluded := false
1038	filters := opts.DirectoryFilters
1039	if gomodcache != "" {
1040		filters = append(filters, "-"+gomodcache)
1041	}
1042	for _, filter := range filters {
1043		op, prefix := filter[0], filter[1:]
1044		// Non-empty prefixes have to be precise directory matches.
1045		if prefix != "" {
1046			prefix = prefix + "/"
1047			path = path + "/"
1048		}
1049		if !strings.HasPrefix(path, prefix) {
1050			continue
1051		}
1052		excluded = op == '-'
1053	}
1054	return excluded
1055}
1056