1
0
mirror of https://github.com/golang/go synced 2024-11-05 22:36:10 -07:00
go/internal/lsp/fake/workdir.go
Rob Findley c20a87c16a internal/lsp/fake: split up and rename the Workspace type
The Workspace type has accumulated too much additional functionality of
late: managing the Env, GOPATH, and GOPROXY in addition to the working
directory. Additionally, the name 'Workspace' can easily be confused
with 'workspaceFolder' in the LSP spec, and they're not quite
equivalent.

Split off a Proxy type to be responsible for the fake module proxy, and
a Workdir type to be responsible for working with the temporary
directory. Rename what remains of 'Workspace' to a more appropriate name
for such a collection of resources: Sandbox.

This is mostly just moving things around, with one significant change in
functionality: previously our three temporary directories (workdir,
gopath, and goproxy) were in separate toplevel directories below
$TMPDIR. Now they are all below a new sandbox temp directory, so that
they are correlated in the filesystem and can be cleaned up with one
call to os.RemoveAll.

Change-Id: I1e160a31ae22f0132355117df941fe65822900eb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230758
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-05-06 18:17:57 +00:00

271 lines
6.9 KiB
Go

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fake
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
)
// FileEvent wraps the protocol.FileEvent so that it can be associated with a
// workdir-relative path.
type FileEvent struct {
Path string
ProtocolEvent protocol.FileEvent
}
// Workdir is a temporary working directory for tests. It exposes file
// operations in terms of relative paths, and fakes file watching by triggering
// events on file operations.
type Workdir struct {
workdir string
watcherMu sync.Mutex
watchers []func(context.Context, []FileEvent)
fileMu sync.Mutex
files map[string]time.Time
}
// NewWorkdir writes the txtar-encoded file data in txt to dir, and returns a
// Workir for operating on these files using
func NewWorkdir(dir, txt string) (*Workdir, error) {
w := &Workdir{workdir: dir}
files := unpackTxt(txt)
for name, data := range files {
if err := w.writeFileData(name, string(data)); err != nil {
return nil, fmt.Errorf("writing to workdir: %w", err)
}
}
// Poll to capture the current file state.
if _, err := w.pollFiles(); err != nil {
return nil, fmt.Errorf("polling files: %w", err)
}
return w, nil
}
// RootURI returns the root URI for this working directory of this scratch
// environment.
func (w *Workdir) RootURI() protocol.DocumentURI {
return toURI(w.workdir)
}
// AddWatcher registers the given func to be called on any file change.
func (w *Workdir) AddWatcher(watcher func(context.Context, []FileEvent)) {
w.watcherMu.Lock()
w.watchers = append(w.watchers, watcher)
w.watcherMu.Unlock()
}
// filePath returns an absolute filesystem path for the workdir-relative path.
func (w *Workdir) filePath(path string) string {
fp := filepath.FromSlash(path)
if filepath.IsAbs(fp) {
return fp
}
return filepath.Join(w.workdir, filepath.FromSlash(path))
}
// URI returns the URI to a the workdir-relative path.
func (w *Workdir) URI(path string) protocol.DocumentURI {
return toURI(w.filePath(path))
}
// URIToPath converts a uri to a workdir-relative path (or an absolute path,
// if the uri is outside of the workdir).
func (w *Workdir) URIToPath(uri protocol.DocumentURI) string {
fp := uri.SpanURI().Filename()
return w.relPath(fp)
}
// relPath returns a '/'-encoded path relative to the working directory (or an
// absolute path if the file is outside of workdir)
func (w *Workdir) relPath(fp string) string {
root := w.RootURI().SpanURI().Filename()
if rel, err := filepath.Rel(root, fp); err == nil && !strings.HasPrefix(rel, "..") {
return filepath.ToSlash(rel)
}
return filepath.ToSlash(fp)
}
func toURI(fp string) protocol.DocumentURI {
return protocol.DocumentURI(span.URIFromPath(fp))
}
// ReadFile reads a text file specified by a workdir-relative path.
func (w *Workdir) ReadFile(path string) (string, error) {
b, err := ioutil.ReadFile(w.filePath(path))
if err != nil {
return "", err
}
return string(b), nil
}
// RegexpSearch searches the file corresponding to path for the first position
// matching re.
func (w *Workdir) RegexpSearch(path string, re string) (Pos, error) {
content, err := w.ReadFile(path)
if err != nil {
return Pos{}, err
}
start, _, err := regexpRange(content, re)
return start, err
}
// RemoveFile removes a workdir-relative file path.
func (w *Workdir) RemoveFile(ctx context.Context, path string) error {
fp := w.filePath(path)
if err := os.Remove(fp); err != nil {
return fmt.Errorf("removing %q: %w", path, err)
}
evts := []FileEvent{{
Path: path,
ProtocolEvent: protocol.FileEvent{
URI: w.URI(path),
Type: protocol.Deleted,
},
}}
w.sendEvents(ctx, evts)
return nil
}
func (w *Workdir) sendEvents(ctx context.Context, evts []FileEvent) {
if len(evts) == 0 {
return
}
w.watcherMu.Lock()
watchers := make([]func(context.Context, []FileEvent), len(w.watchers))
copy(watchers, w.watchers)
w.watcherMu.Unlock()
for _, w := range watchers {
go w(ctx, evts)
}
}
// WriteFile writes text file content to a workdir-relative path.
func (w *Workdir) WriteFile(ctx context.Context, path, content string) error {
fp := w.filePath(path)
_, err := os.Stat(fp)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("checking if %q exists: %w", path, err)
}
var changeType protocol.FileChangeType
if os.IsNotExist(err) {
changeType = protocol.Created
} else {
changeType = protocol.Changed
}
if err := w.writeFileData(path, content); err != nil {
return err
}
evts := []FileEvent{{
Path: path,
ProtocolEvent: protocol.FileEvent{
URI: w.URI(path),
Type: changeType,
},
}}
w.sendEvents(ctx, evts)
return nil
}
func (w *Workdir) writeFileData(path string, content string) error {
fp := w.filePath(path)
if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil {
return fmt.Errorf("creating nested directory: %w", err)
}
if err := ioutil.WriteFile(fp, []byte(content), 0644); err != nil {
return fmt.Errorf("writing %q: %w", path, err)
}
return nil
}
// ListFiles lists files in the given directory, returning a map of relative
// path to modification time.
func (w *Workdir) ListFiles(dir string) (map[string]time.Time, error) {
files := make(map[string]time.Time)
absDir := w.filePath(dir)
if err := filepath.Walk(absDir, func(fp string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
path := w.relPath(fp)
files[path] = info.ModTime()
return nil
}); err != nil {
return nil, err
}
return files, nil
}
// CheckForFileChanges walks the working directory and checks for any files
// that have changed since the last poll.
func (w *Workdir) CheckForFileChanges(ctx context.Context) error {
evts, err := w.pollFiles()
if err != nil {
return err
}
w.sendEvents(ctx, evts)
return nil
}
// pollFiles updates w.files and calculates FileEvents corresponding to file
// state changes since the last poll. It does not call sendEvents.
func (w *Workdir) pollFiles() ([]FileEvent, error) {
w.fileMu.Lock()
defer w.fileMu.Unlock()
files, err := w.ListFiles(".")
if err != nil {
return nil, err
}
var evts []FileEvent
// Check which files have been added or modified.
for path, mtime := range files {
oldmtime, ok := w.files[path]
delete(w.files, path)
var typ protocol.FileChangeType
switch {
case !ok:
typ = protocol.Created
case oldmtime != mtime:
typ = protocol.Changed
default:
continue
}
evts = append(evts, FileEvent{
Path: path,
ProtocolEvent: protocol.FileEvent{
URI: w.URI(path),
Type: typ,
},
})
}
// Any remaining files must have been deleted.
for path := range w.files {
evts = append(evts, FileEvent{
Path: path,
ProtocolEvent: protocol.FileEvent{
URI: w.URI(path),
Type: protocol.Deleted,
},
})
}
w.files = files
return evts, nil
}