1 // Copyright 2020 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.
17 "golang.org/x/tools/internal/lsp/protocol"
18 "golang.org/x/tools/internal/span"
19 errors "golang.org/x/xerrors"
22 // FileEvent wraps the protocol.FileEvent so that it can be associated with a
23 // workdir-relative path.
24 type FileEvent struct {
26 ProtocolEvent protocol.FileEvent
29 // RelativeTo is a helper for operations relative to a given directory.
30 type RelativeTo string
32 // AbsPath returns an absolute filesystem path for the workdir-relative path.
33 func (r RelativeTo) AbsPath(path string) string {
34 fp := filepath.FromSlash(path)
35 if filepath.IsAbs(fp) {
38 return filepath.Join(string(r), filepath.FromSlash(path))
41 // RelPath returns a '/'-encoded path relative to the working directory (or an
42 // absolute path if the file is outside of workdir)
43 func (r RelativeTo) RelPath(fp string) string {
45 if rel, err := filepath.Rel(root, fp); err == nil && !strings.HasPrefix(rel, "..") {
46 return filepath.ToSlash(rel)
48 return filepath.ToSlash(fp)
51 func writeTxtar(txt string, rel RelativeTo) error {
52 files := unpackTxt(txt)
53 for name, data := range files {
54 if err := WriteFileData(name, data, rel); err != nil {
55 return errors.Errorf("writing to workdir: %w", err)
61 // WriteFileData writes content to the relative path, replacing the special
62 // token $SANDBOX_WORKDIR with the relative root given by rel.
63 func WriteFileData(path string, content []byte, rel RelativeTo) error {
64 content = bytes.ReplaceAll(content, []byte("$SANDBOX_WORKDIR"), []byte(rel))
65 fp := rel.AbsPath(path)
66 if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil {
67 return errors.Errorf("creating nested directory: %w", err)
69 if err := ioutil.WriteFile(fp, []byte(content), 0644); err != nil {
70 return errors.Errorf("writing %q: %w", path, err)
75 // Workdir is a temporary working directory for tests. It exposes file
76 // operations in terms of relative paths, and fakes file watching by triggering
77 // events on file operations.
82 watchers []func(context.Context, []FileEvent)
85 files map[string]time.Time
88 // NewWorkdir writes the txtar-encoded file data in txt to dir, and returns a
89 // Workir for operating on these files using
90 func NewWorkdir(dir string) *Workdir {
91 return &Workdir{RelativeTo: RelativeTo(dir)}
94 func (w *Workdir) writeInitialFiles(txt string) error {
95 writeTxtar(txt, w.RelativeTo)
96 // Poll to capture the current file state.
97 if _, err := w.pollFiles(); err != nil {
98 return errors.Errorf("polling files: %w", err)
103 // RootURI returns the root URI for this working directory of this scratch
105 func (w *Workdir) RootURI() protocol.DocumentURI {
106 return toURI(string(w.RelativeTo))
109 // AddWatcher registers the given func to be called on any file change.
110 func (w *Workdir) AddWatcher(watcher func(context.Context, []FileEvent)) {
112 w.watchers = append(w.watchers, watcher)
116 // URI returns the URI to a the workdir-relative path.
117 func (w *Workdir) URI(path string) protocol.DocumentURI {
118 return toURI(w.AbsPath(path))
121 // URIToPath converts a uri to a workdir-relative path (or an absolute path,
122 // if the uri is outside of the workdir).
123 func (w *Workdir) URIToPath(uri protocol.DocumentURI) string {
124 fp := uri.SpanURI().Filename()
128 func toURI(fp string) protocol.DocumentURI {
129 return protocol.DocumentURI(span.URIFromPath(fp))
132 // ReadFile reads a text file specified by a workdir-relative path.
133 func (w *Workdir) ReadFile(path string) (string, error) {
134 b, err := ioutil.ReadFile(w.AbsPath(path))
138 return string(b), nil
141 func (w *Workdir) RegexpRange(path, re string) (Pos, Pos, error) {
142 content, err := w.ReadFile(path)
144 return Pos{}, Pos{}, err
146 return regexpRange(content, re)
149 // RegexpSearch searches the file corresponding to path for the first position
151 func (w *Workdir) RegexpSearch(path string, re string) (Pos, error) {
152 content, err := w.ReadFile(path)
156 start, _, err := regexpRange(content, re)
160 // ChangeFilesOnDisk executes the given on-disk file changes in a batch,
161 // simulating the action of changing branches outside of an editor.
162 func (w *Workdir) ChangeFilesOnDisk(ctx context.Context, events []FileEvent) error {
163 for _, e := range events {
164 switch e.ProtocolEvent.Type {
165 case protocol.Deleted:
166 fp := w.AbsPath(e.Path)
167 if err := os.Remove(fp); err != nil {
168 return errors.Errorf("removing %q: %w", e.Path, err)
170 case protocol.Changed, protocol.Created:
171 if _, err := w.writeFile(ctx, e.Path, e.Content); err != nil {
176 w.sendEvents(ctx, events)
180 // RemoveFile removes a workdir-relative file path.
181 func (w *Workdir) RemoveFile(ctx context.Context, path string) error {
182 fp := w.AbsPath(path)
183 if err := os.Remove(fp); err != nil {
184 return errors.Errorf("removing %q: %w", path, err)
186 evts := []FileEvent{{
188 ProtocolEvent: protocol.FileEvent{
190 Type: protocol.Deleted,
193 w.sendEvents(ctx, evts)
197 func (w *Workdir) sendEvents(ctx context.Context, evts []FileEvent) {
202 watchers := make([]func(context.Context, []FileEvent), len(w.watchers))
203 copy(watchers, w.watchers)
205 for _, w := range watchers {
210 // WriteFiles writes the text file content to workdir-relative paths.
211 // It batches notifications rather than sending them consecutively.
212 func (w *Workdir) WriteFiles(ctx context.Context, files map[string]string) error {
214 for filename, content := range files {
215 evt, err := w.writeFile(ctx, filename, content)
219 evts = append(evts, evt)
221 w.sendEvents(ctx, evts)
225 // WriteFile writes text file content to a workdir-relative path.
226 func (w *Workdir) WriteFile(ctx context.Context, path, content string) error {
227 evt, err := w.writeFile(ctx, path, content)
231 w.sendEvents(ctx, []FileEvent{evt})
235 func (w *Workdir) writeFile(ctx context.Context, path, content string) (FileEvent, error) {
236 fp := w.AbsPath(path)
237 _, err := os.Stat(fp)
238 if err != nil && !os.IsNotExist(err) {
239 return FileEvent{}, errors.Errorf("checking if %q exists: %w", path, err)
241 var changeType protocol.FileChangeType
242 if os.IsNotExist(err) {
243 changeType = protocol.Created
245 changeType = protocol.Changed
247 if err := WriteFileData(path, []byte(content), w.RelativeTo); err != nil {
248 return FileEvent{}, err
252 ProtocolEvent: protocol.FileEvent{
259 // ListFiles lists files in the given directory, returning a map of relative
260 // path to modification time.
261 func (w *Workdir) ListFiles(dir string) (map[string]time.Time, error) {
262 files := make(map[string]time.Time)
263 absDir := w.AbsPath(dir)
264 if err := filepath.Walk(absDir, func(fp string, info os.FileInfo, err error) error {
271 path := w.RelPath(fp)
272 files[path] = info.ModTime()
280 // CheckForFileChanges walks the working directory and checks for any files
281 // that have changed since the last poll.
282 func (w *Workdir) CheckForFileChanges(ctx context.Context) error {
283 evts, err := w.pollFiles()
287 w.sendEvents(ctx, evts)
291 // pollFiles updates w.files and calculates FileEvents corresponding to file
292 // state changes since the last poll. It does not call sendEvents.
293 func (w *Workdir) pollFiles() ([]FileEvent, error) {
295 defer w.fileMu.Unlock()
297 files, err := w.ListFiles(".")
302 // Check which files have been added or modified.
303 for path, mtime := range files {
304 oldmtime, ok := w.files[path]
305 delete(w.files, path)
306 var typ protocol.FileChangeType
309 typ = protocol.Created
310 case oldmtime != mtime:
311 typ = protocol.Changed
315 evts = append(evts, FileEvent{
317 ProtocolEvent: protocol.FileEvent{
323 // Any remaining files must have been deleted.
324 for path := range w.files {
325 evts = append(evts, FileEvent{
327 ProtocolEvent: protocol.FileEvent{
329 Type: protocol.Deleted,