Skip to content

Commit 7424a4d

Browse files
committed
internal/lsp/cache: fix check for excluded paths in locateTemplateFiles
The pathExcludedByFilter helper function expects path to be relative to the workspace folder. This is a confusing API, and led to us passing absolute paths when checking for excluded template files. Fix it, add a regtest, and add a TODO to clean up the API. Fixes golang/go#50431 Change-Id: Iedcf0dd9710e541c9fb8c296d9856a13ef3fbcb6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/378398 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Ian Cottrell <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 4e31bde commit 7424a4d

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

gopls/internal/regtest/template/template_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ Hello {{}} <-- missing body
4343
})
4444
}
4545

46+
func TestTemplatesObserveDirectoryFilters(t *testing.T) {
47+
const files = `
48+
-- go.mod --
49+
module mod.com
50+
51+
go 1.12
52+
-- a/a.tmpl --
53+
A {{}} <-- missing body
54+
-- b/b.tmpl --
55+
B {{}} <-- missing body
56+
`
57+
58+
WithOptions(
59+
EditorConfig{
60+
Settings: map[string]interface{}{
61+
"templateExtensions": []string{"tmpl"},
62+
},
63+
DirectoryFilters: []string{"-b"},
64+
},
65+
).Run(t, files, func(t *testing.T, env *Env) {
66+
env.Await(
67+
OnceMet(env.DiagnosticAtRegexp("a/a.tmpl", "()A")),
68+
NoDiagnostics("b/b.tmpl"),
69+
)
70+
})
71+
}
72+
4673
func TestTemplatesFromLangID(t *testing.T) {
4774
const files = `
4875
-- go.mod --

internal/lsp/cache/view.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func (s *snapshot) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Optio
357357
return s.view.importsState.runProcessEnvFunc(ctx, s, fn)
358358
}
359359

360-
// separated out from its sole use in locateTemplatFiles for testability
360+
// separated out from its sole use in locateTemplateFiles for testability
361361
func fileHasExtension(path string, suffixes []string) bool {
362362
ext := filepath.Ext(path)
363363
if ext != "" && ext[0] == '.' {
@@ -376,15 +376,25 @@ func (s *snapshot) locateTemplateFiles(ctx context.Context) {
376376
return
377377
}
378378
suffixes := s.view.Options().TemplateExtensions
379+
380+
// The workspace root may have been expanded to a module, but we should apply
381+
// directory filters based on the configured workspace folder.
382+
//
383+
// TODO(rfindley): we should be more principled about paths outside of the
384+
// workspace folder: do we even consider them? Do we support absolute
385+
// exclusions? Relative exclusions starting with ..?
379386
dir := s.workspace.root.Filename()
387+
relativeTo := s.view.folder.Filename()
388+
380389
searched := 0
381390
// Change to WalkDir when we move up to 1.16
382391
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
383392
if err != nil {
384393
return err
385394
}
386-
if fileHasExtension(path, suffixes) && !pathExcludedByFilter(path, dir, s.view.gomodcache, s.view.options) &&
387-
!fi.IsDir() {
395+
relpath := strings.TrimPrefix(path, relativeTo)
396+
excluded := pathExcludedByFilter(relpath, dir, s.view.gomodcache, s.view.options)
397+
if fileHasExtension(path, suffixes) && !excluded && !fi.IsDir() {
388398
k := span.URIFromPath(path)
389399
fh, err := s.GetVersionedFile(ctx, k)
390400
if err != nil {
@@ -1114,6 +1124,12 @@ func pathExcludedByFilterFunc(root, gomodcache string, opts *source.Options) fun
11141124
}
11151125
}
11161126

1127+
// pathExcludedByFilter reports whether the path (relative to the workspace
1128+
// folder) should be excluded by the configured directory filters.
1129+
//
1130+
// TODO(rfindley): passing root and gomodcache here makes it confusing whether
1131+
// path should be absolute or relative, and has already caused at least one
1132+
// bug.
11171133
func pathExcludedByFilter(path, root, gomodcache string, opts *source.Options) bool {
11181134
path = strings.TrimPrefix(filepath.ToSlash(path), "/")
11191135
gomodcache = strings.TrimPrefix(filepath.ToSlash(strings.TrimPrefix(gomodcache, root)), "/")

0 commit comments

Comments
 (0)