github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/cache/session.go (about)

     1  // Copyright 2019 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
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"strconv"
    11  	"strings"
    12  	"sync"
    13  	"sync/atomic"
    14  
    15  	"github.com/april1989/origin-go-tools/internal/event"
    16  	"github.com/april1989/origin-go-tools/internal/gocommand"
    17  	"github.com/april1989/origin-go-tools/internal/imports"
    18  	"github.com/april1989/origin-go-tools/internal/lsp/source"
    19  	"github.com/april1989/origin-go-tools/internal/span"
    20  	"github.com/april1989/origin-go-tools/internal/xcontext"
    21  	errors "golang.org/x/xerrors"
    22  )
    23  
    24  type Session struct {
    25  	cache *Cache
    26  	id    string
    27  
    28  	options source.Options
    29  
    30  	viewMu  sync.Mutex
    31  	views   []*View
    32  	viewMap map[span.URI]*View
    33  
    34  	overlayMu sync.Mutex
    35  	overlays  map[span.URI]*overlay
    36  
    37  	// gocmdRunner guards go command calls from concurrency errors.
    38  	gocmdRunner *gocommand.Runner
    39  }
    40  
    41  type overlay struct {
    42  	session *Session
    43  	uri     span.URI
    44  	text    []byte
    45  	hash    string
    46  	version float64
    47  	kind    source.FileKind
    48  
    49  	// saved is true if a file matches the state on disk,
    50  	// and therefore does not need to be part of the overlay sent to go/packages.
    51  	saved bool
    52  }
    53  
    54  func (o *overlay) Read() ([]byte, error) {
    55  	return o.text, nil
    56  }
    57  
    58  func (o *overlay) FileIdentity() source.FileIdentity {
    59  	return source.FileIdentity{
    60  		URI:  o.uri,
    61  		Hash: o.hash,
    62  		Kind: o.kind,
    63  	}
    64  }
    65  
    66  func (o *overlay) VersionedFileIdentity() source.VersionedFileIdentity {
    67  	return source.VersionedFileIdentity{
    68  		URI:       o.uri,
    69  		SessionID: o.session.id,
    70  		Version:   o.version,
    71  	}
    72  }
    73  
    74  func (o *overlay) Kind() source.FileKind {
    75  	return o.kind
    76  }
    77  
    78  func (o *overlay) URI() span.URI {
    79  	return o.uri
    80  }
    81  
    82  func (o *overlay) Version() float64 {
    83  	return o.version
    84  }
    85  
    86  func (o *overlay) Session() string {
    87  	return o.session.id
    88  }
    89  
    90  func (o *overlay) Saved() bool {
    91  	return o.saved
    92  }
    93  
    94  // closedFile implements LSPFile for a file that the editor hasn't told us about.
    95  type closedFile struct {
    96  	source.FileHandle
    97  }
    98  
    99  func (c *closedFile) VersionedFileIdentity() source.VersionedFileIdentity {
   100  	return source.VersionedFileIdentity{
   101  		URI:       c.FileHandle.URI(),
   102  		SessionID: "",
   103  		Version:   0,
   104  	}
   105  }
   106  
   107  func (c *closedFile) Session() string {
   108  	return ""
   109  }
   110  
   111  func (c *closedFile) Version() float64 {
   112  	return 0
   113  }
   114  
   115  func (s *Session) ID() string     { return s.id }
   116  func (s *Session) String() string { return s.id }
   117  
   118  func (s *Session) Options() source.Options {
   119  	return s.options
   120  }
   121  
   122  func (s *Session) SetOptions(options source.Options) {
   123  	s.options = options
   124  }
   125  
   126  func (s *Session) Shutdown(ctx context.Context) {
   127  	s.viewMu.Lock()
   128  	defer s.viewMu.Unlock()
   129  	for _, view := range s.views {
   130  		view.shutdown(ctx)
   131  	}
   132  	s.views = nil
   133  	s.viewMap = nil
   134  	event.Log(ctx, "Shutdown session", KeyShutdownSession.Of(s))
   135  }
   136  
   137  func (s *Session) Cache() interface{} {
   138  	return s.cache
   139  }
   140  
   141  func (s *Session) NewView(ctx context.Context, name string, folder span.URI, options source.Options) (source.View, source.Snapshot, func(), error) {
   142  	s.viewMu.Lock()
   143  	defer s.viewMu.Unlock()
   144  	view, snapshot, release, err := s.createView(ctx, name, folder, options, 0)
   145  	if err != nil {
   146  		return nil, nil, func() {}, err
   147  	}
   148  	s.views = append(s.views, view)
   149  	// we always need to drop the view map
   150  	s.viewMap = make(map[span.URI]*View)
   151  	return view, snapshot, release, nil
   152  }
   153  
   154  func (s *Session) createView(ctx context.Context, name string, folder span.URI, options source.Options, snapshotID uint64) (*View, *snapshot, func(), error) {
   155  	index := atomic.AddInt64(&viewIndex, 1)
   156  	// We want a true background context and not a detached context here
   157  	// the spans need to be unrelated and no tag values should pollute it.
   158  	baseCtx := event.Detach(xcontext.Detach(ctx))
   159  	backgroundCtx, cancel := context.WithCancel(baseCtx)
   160  
   161  	v := &View{
   162  		session:            s,
   163  		initialized:        make(chan struct{}),
   164  		initializationSema: make(chan struct{}, 1),
   165  		initializeOnce:     &sync.Once{},
   166  		id:                 strconv.FormatInt(index, 10),
   167  		options:            options,
   168  		baseCtx:            baseCtx,
   169  		backgroundCtx:      backgroundCtx,
   170  		cancel:             cancel,
   171  		name:               name,
   172  		folder:             folder,
   173  		root:               folder,
   174  		filesByURI:         make(map[span.URI]*fileBase),
   175  		filesByBase:        make(map[string][]*fileBase),
   176  	}
   177  	v.snapshot = &snapshot{
   178  		id:                snapshotID,
   179  		view:              v,
   180  		generation:        s.cache.store.Generation(generationName(v, 0)),
   181  		packages:          make(map[packageKey]*packageHandle),
   182  		ids:               make(map[span.URI][]packageID),
   183  		metadata:          make(map[packageID]*metadata),
   184  		files:             make(map[span.URI]source.VersionedFileHandle),
   185  		goFiles:           make(map[parseKey]*parseGoHandle),
   186  		importedBy:        make(map[packageID][]packageID),
   187  		actions:           make(map[actionKey]*actionHandle),
   188  		workspacePackages: make(map[packageID]packagePath),
   189  		unloadableFiles:   make(map[span.URI]struct{}),
   190  		parseModHandles:   make(map[span.URI]*parseModHandle),
   191  		modTidyHandles:    make(map[span.URI]*modTidyHandle),
   192  		modUpgradeHandles: make(map[span.URI]*modUpgradeHandle),
   193  		modWhyHandles:     make(map[span.URI]*modWhyHandle),
   194  	}
   195  
   196  	if v.session.cache.options != nil {
   197  		v.session.cache.options(&v.options)
   198  	}
   199  	// Set the module-specific information.
   200  	if err := v.setBuildInformation(ctx, folder, options); err != nil {
   201  		return nil, nil, func() {}, err
   202  	}
   203  
   204  	// We have v.goEnv now.
   205  	v.processEnv = &imports.ProcessEnv{
   206  		GocmdRunner: s.gocmdRunner,
   207  		WorkingDir:  folder.Filename(),
   208  		Env:         v.goEnv,
   209  	}
   210  
   211  	// Set the first snapshot's workspace directories. The view's modURI was
   212  	// set by setBuildInformation.
   213  	var fh source.FileHandle
   214  	if v.modURI != "" {
   215  		fh, _ = s.GetFile(ctx, v.modURI)
   216  	}
   217  	v.snapshot.workspaceDirectories = v.snapshot.findWorkspaceDirectories(ctx, fh)
   218  
   219  	// Initialize the view without blocking.
   220  	initCtx, initCancel := context.WithCancel(xcontext.Detach(ctx))
   221  	v.initCancelFirstAttempt = initCancel
   222  	go func() {
   223  		release := v.snapshot.generation.Acquire(initCtx)
   224  		v.initialize(initCtx, v.snapshot, true)
   225  		release()
   226  	}()
   227  	return v, v.snapshot, v.snapshot.generation.Acquire(ctx), nil
   228  }
   229  
   230  // View returns the view by name.
   231  func (s *Session) View(name string) source.View {
   232  	s.viewMu.Lock()
   233  	defer s.viewMu.Unlock()
   234  	for _, view := range s.views {
   235  		if view.Name() == name {
   236  			return view
   237  		}
   238  	}
   239  	return nil
   240  }
   241  
   242  // ViewOf returns a view corresponding to the given URI.
   243  // If the file is not already associated with a view, pick one using some heuristics.
   244  func (s *Session) ViewOf(uri span.URI) (source.View, error) {
   245  	return s.viewOf(uri)
   246  }
   247  
   248  func (s *Session) viewOf(uri span.URI) (*View, error) {
   249  	s.viewMu.Lock()
   250  	defer s.viewMu.Unlock()
   251  
   252  	// Check if we already know this file.
   253  	if v, found := s.viewMap[uri]; found {
   254  		return v, nil
   255  	}
   256  	// Pick the best view for this file and memoize the result.
   257  	v, err := s.bestView(uri)
   258  	if err != nil {
   259  		return nil, err
   260  	}
   261  	s.viewMap[uri] = v
   262  	return v, nil
   263  }
   264  
   265  func (s *Session) viewsOf(uri span.URI) []*View {
   266  	s.viewMu.Lock()
   267  	defer s.viewMu.Unlock()
   268  
   269  	var views []*View
   270  	for _, view := range s.views {
   271  		if strings.HasPrefix(string(uri), string(view.Folder())) {
   272  			views = append(views, view)
   273  		}
   274  	}
   275  	return views
   276  }
   277  
   278  func (s *Session) Views() []source.View {
   279  	s.viewMu.Lock()
   280  	defer s.viewMu.Unlock()
   281  	result := make([]source.View, len(s.views))
   282  	for i, v := range s.views {
   283  		result[i] = v
   284  	}
   285  	return result
   286  }
   287  
   288  // bestView finds the best view to associate a given URI with.
   289  // viewMu must be held when calling this method.
   290  func (s *Session) bestView(uri span.URI) (*View, error) {
   291  	if len(s.views) == 0 {
   292  		return nil, errors.Errorf("no views in the session")
   293  	}
   294  	// we need to find the best view for this file
   295  	var longest *View
   296  	for _, view := range s.views {
   297  		if longest != nil && len(longest.Folder()) > len(view.Folder()) {
   298  			continue
   299  		}
   300  		if view.contains(uri) {
   301  			longest = view
   302  		}
   303  	}
   304  	if longest != nil {
   305  		return longest, nil
   306  	}
   307  	// Try our best to return a view that knows the file.
   308  	for _, view := range s.views {
   309  		if view.knownFile(uri) {
   310  			return view, nil
   311  		}
   312  	}
   313  	// TODO: are there any more heuristics we can use?
   314  	return s.views[0], nil
   315  }
   316  
   317  func (s *Session) removeView(ctx context.Context, view *View) error {
   318  	s.viewMu.Lock()
   319  	defer s.viewMu.Unlock()
   320  	i, err := s.dropView(ctx, view)
   321  	if err != nil {
   322  		return err
   323  	}
   324  	// delete this view... we don't care about order but we do want to make
   325  	// sure we can garbage collect the view
   326  	s.views[i] = s.views[len(s.views)-1]
   327  	s.views[len(s.views)-1] = nil
   328  	s.views = s.views[:len(s.views)-1]
   329  	return nil
   330  }
   331  
   332  func (s *Session) updateView(ctx context.Context, view *View, options source.Options) (*View, error) {
   333  	s.viewMu.Lock()
   334  	defer s.viewMu.Unlock()
   335  	i, err := s.dropView(ctx, view)
   336  	if err != nil {
   337  		return nil, err
   338  	}
   339  	// Preserve the snapshot ID if we are recreating the view.
   340  	view.snapshotMu.Lock()
   341  	snapshotID := view.snapshot.id
   342  	view.snapshotMu.Unlock()
   343  	v, _, release, err := s.createView(ctx, view.name, view.folder, options, snapshotID)
   344  	release()
   345  	if err != nil {
   346  		// we have dropped the old view, but could not create the new one
   347  		// this should not happen and is very bad, but we still need to clean
   348  		// up the view array if it happens
   349  		s.views[i] = s.views[len(s.views)-1]
   350  		s.views[len(s.views)-1] = nil
   351  		s.views = s.views[:len(s.views)-1]
   352  		return nil, err
   353  	}
   354  	// substitute the new view into the array where the old view was
   355  	s.views[i] = v
   356  	return v, nil
   357  }
   358  
   359  func (s *Session) dropView(ctx context.Context, v *View) (int, error) {
   360  	// we always need to drop the view map
   361  	s.viewMap = make(map[span.URI]*View)
   362  	for i := range s.views {
   363  		if v == s.views[i] {
   364  			// we found the view, drop it and return the index it was found at
   365  			s.views[i] = nil
   366  			v.shutdown(ctx)
   367  			return i, nil
   368  		}
   369  	}
   370  	return -1, errors.Errorf("view %s for %v not found", v.Name(), v.Folder())
   371  }
   372  
   373  func (s *Session) ModifyFiles(ctx context.Context, changes []source.FileModification) error {
   374  	_, releases, _, err := s.DidModifyFiles(ctx, changes)
   375  	for _, release := range releases {
   376  		release()
   377  	}
   378  	return err
   379  }
   380  
   381  func (s *Session) DidModifyFiles(ctx context.Context, changes []source.FileModification) ([]source.Snapshot, []func(), []span.URI, error) {
   382  	views := make(map[*View]map[span.URI]source.VersionedFileHandle)
   383  
   384  	// Keep track of deleted files so that we can clear their diagnostics.
   385  	// A file might be re-created after deletion, so only mark files that
   386  	// have truly been deleted.
   387  	deletions := map[span.URI]struct{}{}
   388  
   389  	overlays, err := s.updateOverlays(ctx, changes)
   390  	if err != nil {
   391  		return nil, nil, nil, err
   392  	}
   393  	var forceReloadMetadata bool
   394  	for _, c := range changes {
   395  		if c.Action == source.InvalidateMetadata {
   396  			forceReloadMetadata = true
   397  		}
   398  
   399  		// Look through all of the session's views, invalidating the file for
   400  		// all of the views to which it is known.
   401  		for _, view := range s.views {
   402  			// Don't propagate changes that are outside of the view's scope
   403  			// or knowledge.
   404  			if !view.relevantChange(c) {
   405  				continue
   406  			}
   407  			// Make sure that the file is added to the view.
   408  			if _, err := view.getFile(c.URI); err != nil {
   409  				return nil, nil, nil, err
   410  			}
   411  			if _, ok := views[view]; !ok {
   412  				views[view] = make(map[span.URI]source.VersionedFileHandle)
   413  			}
   414  			var (
   415  				fh source.VersionedFileHandle
   416  				ok bool
   417  			)
   418  			if fh, ok = overlays[c.URI]; ok {
   419  				views[view][c.URI] = fh
   420  				delete(deletions, c.URI)
   421  			} else {
   422  				fsFile, err := s.cache.getFile(ctx, c.URI)
   423  				if err != nil {
   424  					return nil, nil, nil, err
   425  				}
   426  				fh = &closedFile{fsFile}
   427  				views[view][c.URI] = fh
   428  				if _, err := fh.Read(); err != nil {
   429  					deletions[c.URI] = struct{}{}
   430  				}
   431  			}
   432  			// If the file change is to a go.mod file, and initialization for
   433  			// the view has previously failed, we should attempt to retry.
   434  			// TODO(rstambler): We can use unsaved contents with -modfile, so
   435  			// maybe we should do that and retry on any change?
   436  			if fh.Kind() == source.Mod && (c.OnDisk || c.Action == source.Save) {
   437  				view.maybeReinitialize()
   438  			}
   439  		}
   440  	}
   441  	var snapshots []source.Snapshot
   442  	var releases []func()
   443  	for view, uris := range views {
   444  		snapshot, release := view.invalidateContent(ctx, uris, forceReloadMetadata)
   445  		snapshots = append(snapshots, snapshot)
   446  		releases = append(releases, release)
   447  	}
   448  	var deletionsSlice []span.URI
   449  	for uri := range deletions {
   450  		deletionsSlice = append(deletionsSlice, uri)
   451  	}
   452  	return snapshots, releases, deletionsSlice, nil
   453  }
   454  
   455  func (s *Session) isOpen(uri span.URI) bool {
   456  	s.overlayMu.Lock()
   457  	defer s.overlayMu.Unlock()
   458  
   459  	_, open := s.overlays[uri]
   460  	return open
   461  }
   462  
   463  func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModification) (map[span.URI]*overlay, error) {
   464  	s.overlayMu.Lock()
   465  	defer s.overlayMu.Unlock()
   466  
   467  	for _, c := range changes {
   468  		// Don't update overlays for metadata invalidations.
   469  		if c.Action == source.InvalidateMetadata {
   470  			continue
   471  		}
   472  
   473  		o, ok := s.overlays[c.URI]
   474  
   475  		// If the file is not opened in an overlay and the change is on disk,
   476  		// there's no need to update an overlay. If there is an overlay, we
   477  		// may need to update the overlay's saved value.
   478  		if !ok && c.OnDisk {
   479  			continue
   480  		}
   481  
   482  		// Determine the file kind on open, otherwise, assume it has been cached.
   483  		var kind source.FileKind
   484  		switch c.Action {
   485  		case source.Open:
   486  			kind = source.DetectLanguage(c.LanguageID, c.URI.Filename())
   487  		default:
   488  			if !ok {
   489  				return nil, errors.Errorf("updateOverlays: modifying unopened overlay %v", c.URI)
   490  			}
   491  			kind = o.kind
   492  		}
   493  		if kind == source.UnknownKind {
   494  			return nil, errors.Errorf("updateOverlays: unknown file kind for %s", c.URI)
   495  		}
   496  
   497  		// Closing a file just deletes its overlay.
   498  		if c.Action == source.Close {
   499  			delete(s.overlays, c.URI)
   500  			continue
   501  		}
   502  
   503  		// If the file is on disk, check if its content is the same as in the
   504  		// overlay. Saves and on-disk file changes don't come with the file's
   505  		// content.
   506  		text := c.Text
   507  		if text == nil && (c.Action == source.Save || c.OnDisk) {
   508  			if !ok {
   509  				return nil, fmt.Errorf("no known content for overlay for %s", c.Action)
   510  			}
   511  			text = o.text
   512  		}
   513  		// On-disk changes don't come with versions.
   514  		version := c.Version
   515  		if c.OnDisk {
   516  			version = o.version
   517  		}
   518  		hash := hashContents(text)
   519  		var sameContentOnDisk bool
   520  		switch c.Action {
   521  		case source.Delete:
   522  			// Do nothing. sameContentOnDisk should be false.
   523  		case source.Save:
   524  			// Make sure the version and content (if present) is the same.
   525  			if o.version != version {
   526  				return nil, errors.Errorf("updateOverlays: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
   527  			}
   528  			if c.Text != nil && o.hash != hash {
   529  				return nil, errors.Errorf("updateOverlays: overlay %s changed on save", c.URI)
   530  			}
   531  			sameContentOnDisk = true
   532  		default:
   533  			fh, err := s.cache.getFile(ctx, c.URI)
   534  			if err != nil {
   535  				return nil, err
   536  			}
   537  			_, readErr := fh.Read()
   538  			sameContentOnDisk = (readErr == nil && fh.FileIdentity().Hash == hash)
   539  		}
   540  		o = &overlay{
   541  			session: s,
   542  			uri:     c.URI,
   543  			version: version,
   544  			text:    text,
   545  			kind:    kind,
   546  			hash:    hash,
   547  			saved:   sameContentOnDisk,
   548  		}
   549  		s.overlays[c.URI] = o
   550  	}
   551  
   552  	// Get the overlays for each change while the session's overlay map is
   553  	// locked.
   554  	overlays := make(map[span.URI]*overlay)
   555  	for _, c := range changes {
   556  		if o, ok := s.overlays[c.URI]; ok {
   557  			overlays[c.URI] = o
   558  		}
   559  	}
   560  	return overlays, nil
   561  }
   562  
   563  func (s *Session) GetFile(ctx context.Context, uri span.URI) (source.FileHandle, error) {
   564  	if overlay := s.readOverlay(uri); overlay != nil {
   565  		return overlay, nil
   566  	}
   567  	// Fall back to the cache-level file system.
   568  	return s.cache.getFile(ctx, uri)
   569  }
   570  
   571  func (s *Session) readOverlay(uri span.URI) *overlay {
   572  	s.overlayMu.Lock()
   573  	defer s.overlayMu.Unlock()
   574  
   575  	if overlay, ok := s.overlays[uri]; ok {
   576  		return overlay
   577  	}
   578  	return nil
   579  }
   580  
   581  func (s *Session) Overlays() []source.Overlay {
   582  	s.overlayMu.Lock()
   583  	defer s.overlayMu.Unlock()
   584  
   585  	overlays := make([]source.Overlay, 0, len(s.overlays))
   586  	for _, overlay := range s.overlays {
   587  		overlays = append(overlays, overlay)
   588  	}
   589  	return overlays
   590  }