From 4f2bbad67dfb6142acc4fa7c31ad74fe5f61e03e Mon Sep 17 00:00:00 2001 From: Joe Kralicky Date: Sat, 21 Sep 2024 13:38:08 -0400 Subject: [PATCH] Skip packages with the 'ignore' build tag when searching for generated code --- pkg/lsp/golang.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/lsp/golang.go b/pkg/lsp/golang.go index ae520ac..9dcf5e7 100644 --- a/pkg/lsp/golang.go +++ b/pkg/lsp/golang.go @@ -10,6 +10,7 @@ import ( "io" "io/fs" "log/slog" + "maps" "net/url" "os" "path" @@ -246,6 +247,25 @@ func (s *GoLanguageDriver) SynthesizeFromGoSource(importName string, res GoModul if err != nil { return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err) } + if len(packages) > 1 { + maps.DeleteFunc(packages, func(k string, v *goast.Package) bool { + filesIgnored := 0 + for _, f := range v.Files { + if len(f.Comments) == 0 { + continue + } + // only check the first comment group + for _, comment := range f.Comments[0].List { + if strings.HasPrefix(comment.Text, "//go:build ignore") || + strings.HasPrefix(comment.Text, "// +build ignore") { + filesIgnored++ + break + } + } + } + return filesIgnored == len(v.Files) + }) + } if len(packages) != 1 { return nil, fmt.Errorf("wrong number of packages found: %d", len(packages)) }