Skip to content

Commit

Permalink
feat: filter build-constraint files
Browse files Browse the repository at this point in the history
  • Loading branch information
x1unix committed Jul 16, 2024
1 parent a57c5c6 commit 82167ea
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
26 changes: 23 additions & 3 deletions internal/pkgindex/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/x1unix/go-playground/pkg/monaco"
)

const (
resultsPreallocSize = 300
queueInitSize = 300

// resultsInitSize value based on a number of packages in Go 1.22
resultsInitSize = 180
)

var buildContraintRegex = regexp.MustCompile(`(?m)^([\S]+)_(freebsd|darwin|plan9|windows|netbsd|arm|386|loong64|mips|ppc|riscv|s390x)`)

type scanResult struct {
hasPkg bool
item monaco.CompletionItem
Expand Down Expand Up @@ -60,7 +66,7 @@ func (s *GoRootScanner) start() ([]monaco.CompletionItem, error) {
return nil, fmt.Errorf("cannot open Go SDK directory: %w", err)
}

q := newQueue(resultsPreallocSize)
q := newQueue(queueInitSize)
for _, entry := range entries {
if !entry.Type().IsDir() {
continue
Expand All @@ -75,7 +81,7 @@ func (s *GoRootScanner) start() ([]monaco.CompletionItem, error) {
q.add(pkgName)
}

results := make([]monaco.CompletionItem, 0, resultsPreallocSize)
results := make([]monaco.CompletionItem, 0, resultsInitSize)
for q.occupied() {
pkgName, ok := q.pop()
if !ok {
Expand Down Expand Up @@ -169,5 +175,19 @@ func shouldIgnoreFileName(fname string) bool {
return true
}

if isBuildConstraintFile(fname) {
return true
}

return strings.HasSuffix(fname, "_test.go")
}

// isBuildConstraintFile checks whether file name contains Go build constraint.
//
// Linux and "_unix.go" files are allowed to align with GoDoc behavior.
// Wasm-target files are intentionally allowed to support `syscall/js` package.
//
// For example: `foo_amd64.go` or `bar_openbsd_ppc64.go`.
func isBuildConstraintFile(fname string) bool {
return buildContraintRegex.MatchString(fname)
}
42 changes: 42 additions & 0 deletions internal/pkgindex/scanner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pkgindex

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestShouldIgnoreFileName(t *testing.T) {
ignoredFiles := []string{
"internal",
"testdata",
"foo_test.go",
"bar_windows.go",
"foo_darwin.go",
"foo_openbsd_arm64.go",
"foo_netbsd_riscv64.go",
"mmap_unix_test.go",
}

allowFiles := []string{
"foo.go",
"foo_bar.go",
"foo_js.go",
"foo_wasm.go",
"foo_js_wasm.go",
"env_unix.go",
"exec_linux.go",
}

t.Run("ignore", func(t *testing.T) {
for _, n := range ignoredFiles {
require.True(t, shouldIgnoreFileName(n), n)
}
})

t.Run("allow", func(t *testing.T) {
for _, n := range allowFiles {
require.False(t, shouldIgnoreFileName(n), n)
}
})
}

0 comments on commit 82167ea

Please sign in to comment.