Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: sync commits from Aspect-internal silo #772

Merged
merged 15 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions .github/workflows/validate_changed_files.sh

This file was deleted.

33 changes: 0 additions & 33 deletions .github/workflows/validation.yml

This file was deleted.

1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ http_archive(
"//:patches/bazelbuild_bazel-gazelle_aspect-cli.patch",
"//:patches/bazelbuild_bazel-gazelle_aspect-walk-subdir.patch",
"//:patches/bazelbuild_bazel-gazelle_aspect-gitignore.patch",
"//:patches/bazelbuild_bazel-gazelle_aspect-fs-direntry.patch",
],
sha256 = "872f1532567cdc53dc8e9f4681cd45021cd6787e2bde8a022bcec24a5867ce4c",
# Ensure this version always matches the go.mod version.
Expand Down
2 changes: 1 addition & 1 deletion cmd/aspect/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func HandleVersionFlags(streams ioutils.Streams, args []string, bzl bazel.Bazel)
if err != nil {
aspecterrors.HandleError(err)
}
fmt.Fprintf(streams.Stdout, version)
fmt.Fprint(streams.Stdout, version)
os.Exit(0)
}
}
Expand Down
13 changes: 11 additions & 2 deletions gazelle/common/git/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package git

import (
"bufio"
"fmt"
"io"
"io/fs"
"os"
"path"
"strings"
Expand Down Expand Up @@ -36,15 +38,22 @@ func collectIgnoreFiles(c *config.Config, rel string) {
}
c.Exts[lastConfiguredExt] = rel

ents := c.Exts[common.ASPECT_DIR_ENTRIES].(map[string]fs.DirEntry)
if _, hasIgnore := ents[".gitignore"]; !hasIgnore {
return
}

// Find and add .gitignore files from this directory
ignoreFilePath := path.Join(c.RepoRoot, rel, ".gitignore")
ignoreReader, ignoreErr := os.Open(ignoreFilePath)
if ignoreErr == nil {
BazelLog.Tracef("Add ignore file %s/.gitignore", rel)
defer ignoreReader.Close()
addIgnore(c, rel, ignoreReader)
} else if !os.IsNotExist(ignoreErr) {
BazelLog.Errorf("Failed to open %s/.gitignore: %v", rel, ignoreErr)
} else {
msg := fmt.Sprintf("Failed to open %s/.gitignore: %v", rel, ignoreErr)
BazelLog.Error(msg)
fmt.Printf("%s\n", msg)
}
}

Expand Down
11 changes: 3 additions & 8 deletions gazelle/common/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ import (
var regexCache = make(map[string]*regexp.Regexp)
var regexMutex sync.Mutex

func ParseRegex(regexStr string) (*regexp.Regexp, error) {
func ParseRegex(regexStr string) *regexp.Regexp {
regexMutex.Lock()
defer regexMutex.Unlock()

if regexCache[regexStr] == nil {
re, err := regexp.Compile(regexStr)
if err != nil {
return nil, err
}

regexCache[regexStr] = re
regexCache[regexStr] = regexp.MustCompile(regexStr)
}

return regexCache[regexStr], nil
return regexCache[regexStr]
}
21 changes: 20 additions & 1 deletion gazelle/common/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,26 @@ type LabelSet struct {
}

func LabelComparator(a, b interface{}) int {
return utils.StringComparator(a.(label.Label).String(), b.(label.Label).String())
al := a.(label.Label)
bl := b.(label.Label)

if al.Relative && !bl.Relative {
return -1
} else if !al.Relative && bl.Relative {
return +1
}

c := utils.StringComparator(al.Repo, bl.Repo)
if c != 0 {
return c
}

c = utils.StringComparator(al.Pkg, bl.Pkg)
if c != 0 {
return c
}

return utils.StringComparator(al.Name, bl.Name)
}

func NewLabelSet(from label.Label) *LabelSet {
Expand Down
2 changes: 2 additions & 0 deletions gazelle/common/treesitter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ go_library(
"filters.go",
"parser.go",
"queries.go",
"query.go",
"traversal.go",
],
importpath = "aspect.build/cli/gazelle/common/treesitter",
visibility = ["//visibility:public"],
deps = [
"//gazelle/common",
"//gazelle/common/treesitter/grammars/json",
"//gazelle/common/treesitter/grammars/kotlin",
"//gazelle/common/treesitter/grammars/starlark",
Expand Down
9 changes: 4 additions & 5 deletions gazelle/common/treesitter/filters.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package treesitter

import (
"regexp"

common "aspect.build/cli/gazelle/common"
sitter "github.com/smacker/go-tree-sitter"
)

Expand All @@ -17,7 +16,7 @@ import (
// Predicates implemented here:
// - eq?
// - match?
func matchesAllPredicates(q *sitter.Query, m *sitter.QueryMatch, qc *sitter.QueryCursor, input []byte) bool {
func matchesAllPredicates(q *sitterQuery, m *sitter.QueryMatch, qc *sitter.QueryCursor, input []byte) bool {
qm := &sitter.QueryMatch{
ID: m.ID,
PatternIndex: m.PatternIndex,
Expand Down Expand Up @@ -79,15 +78,15 @@ func matchesAllPredicates(q *sitter.Query, m *sitter.QueryMatch, qc *sitter.Quer
isPositive := operator == "match?"

expectedCaptureName := q.CaptureNameForId(steps[1].ValueId)
regex := regexp.MustCompile(q.StringValueForId(steps[2].ValueId))
regex := common.ParseRegex(q.StringValueForId(steps[2].ValueId))

for _, c := range m.Captures {
captureName := q.CaptureNameForId(c.Index)
if expectedCaptureName != captureName {
continue
}

if regex.Match([]byte(c.Node.Content(input))) != isPositive {
if regex.MatchString(c.Node.Content(input)) != isPositive {
return false
}
}
Expand Down
28 changes: 14 additions & 14 deletions gazelle/common/treesitter/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
var ErrorsQuery = `(ERROR) @error`

// A cache of parsed queries per language
var queryCache = make(map[LanguageGrammar]map[string]*sitter.Query)
var queryCache = make(map[LanguageGrammar]map[string]*sitterQuery)
var queryMutex sync.Mutex

func parseQuery(lang LanguageGrammar, queryStr string) *sitter.Query {
func parseQuery(lang LanguageGrammar, queryStr string) *sitterQuery {
queryMutex.Lock()
defer queryMutex.Unlock()

if queryCache[lang] == nil {
queryCache[lang] = make(map[string]*sitter.Query)
queryCache[lang] = make(map[string]*sitterQuery)
}
if queryCache[lang][queryStr] == nil {
queryCache[lang][queryStr] = mustNewQuery(lang, queryStr)
Expand All @@ -39,7 +39,7 @@ func (tree TreeAst) QueryStrings(query, returnVar string) []string {

// Execute the query.
qc := sitter.NewQueryCursor()
qc.Exec(sitterQuery, rootNode)
qc.Exec(sitterQuery.q, rootNode)

// Collect string from the query results.
for {
Expand Down Expand Up @@ -82,7 +82,7 @@ func (tree TreeAst) Query(query string) <-chan ASTQueryResult {
// Execute the query.
go func() {
qc := sitter.NewQueryCursor()
qc.Exec(q, rootNode)
qc.Exec(q.q, rootNode)

for {
m, ok := qc.NextMatch()
Expand All @@ -104,7 +104,7 @@ func (tree TreeAst) Query(query string) <-chan ASTQueryResult {
return out
}

func (tree TreeAst) mapQueryMatchCaptures(m *sitter.QueryMatch, q *sitter.Query) map[string]string {
func (tree TreeAst) mapQueryMatchCaptures(m *sitter.QueryMatch, q *sitterQuery) map[string]string {
captures := make(map[string]string, len(m.Captures))
for _, c := range m.Captures {
name := q.CaptureNameForId(c.Index)
Expand All @@ -120,11 +120,11 @@ func (tree TreeAst) mapQueryMatchCaptures(m *sitter.QueryMatch, q *sitter.Query)

// Find and read the `from` QueryCapture from a QueryMatch.
// Filter matches based on captures value using "equals-{name}" vars.
func fetchQueryMatch(query *sitter.Query, name string, m *sitter.QueryMatch, sourceCode []byte) *sitter.QueryCapture {
func fetchQueryMatch(query *sitterQuery, name string, m *sitter.QueryMatch, sourceCode []byte) *sitter.QueryCapture {
var result *sitter.QueryCapture

for ci, c := range m.Captures {
cn := query.CaptureNameForId(uint32(ci))
for _, c := range m.Captures {
cn := query.CaptureNameForId(c.Index)

// Filters where a capture must equal a specific value.
if strings.HasPrefix(cn, "equals-") {
Expand All @@ -145,10 +145,10 @@ func fetchQueryMatch(query *sitter.Query, name string, m *sitter.QueryMatch, sou
return result
}

func mustNewQuery(lang LanguageGrammar, queryStr string) *sitter.Query {
treeQ, err := sitter.NewQuery([]byte(queryStr), toSitterLanguage(lang))
func mustNewTreeQuery(lang LanguageGrammar, query string) *sitter.Query {
treeQ, err := sitter.NewQuery([]byte(query), toSitterLanguage(lang))
if err != nil {
BazelLog.Fatalf("Failed to create query for %q: %v", queryStr, err)
BazelLog.Fatalf("Failed to create query for %q: %v", query, err)
}
return treeQ
}
Expand All @@ -166,7 +166,7 @@ func (tree TreeAst) QueryErrors() []error {

// Execute the import query
qc := sitter.NewQueryCursor()
qc.Exec(query, node)
qc.Exec(query.q, node)

// Collect import statements from the query results
for {
Expand Down Expand Up @@ -201,7 +201,7 @@ func (tree TreeAst) QueryErrors() []error {
msg := pre + line
arw := strings.Repeat(" ", len(pre)+colI) + "^"

errors = append(errors, fmt.Errorf(msg+"\n"+arw))
errors = append(errors, fmt.Errorf("%s\n%s", msg, arw))
}
}

Expand Down
53 changes: 53 additions & 0 deletions gazelle/common/treesitter/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package treesitter

import sitter "github.com/smacker/go-tree-sitter"

// Basic wrapper around sitter.Query to cache tree-sitter cgo calls.
type sitterQuery struct {
q *sitter.Query

// Pre-computed and cached query data
stringValues []string
captureNames []string
predicatePatterns [][][]sitter.QueryPredicateStep
}

func mustNewQuery(lang LanguageGrammar, query string) *sitterQuery {
q := mustNewTreeQuery(lang, query)

captureNames := make([]string, q.CaptureCount())
for i := uint32(0); i < q.CaptureCount(); i++ {
captureNames[i] = q.CaptureNameForId(i)
}

stringValues := make([]string, q.StringCount())
for i := uint32(0); i < q.StringCount(); i++ {
stringValues[i] = q.StringValueForId(i)
}

predicatePatterns := make([][][]sitter.QueryPredicateStep, q.PatternCount())
for i := uint32(0); i < q.PatternCount(); i++ {
predicatePatterns[i] = q.PredicatesForPattern(i)
}

return &sitterQuery{
q: q,
stringValues: stringValues,
captureNames: captureNames,
predicatePatterns: predicatePatterns,
}
}

// Cached query data accessors mirroring the tree-sitter Query signatures.

func (q *sitterQuery) StringValueForId(id uint32) string {
return q.stringValues[id]
}

func (q *sitterQuery) CaptureNameForId(id uint32) string {
return q.captureNames[id]
}

func (q *sitterQuery) PredicatesForPattern(patternIndex uint32) [][]sitter.QueryPredicateStep {
return q.predicatePatterns[patternIndex]
}
1 change: 1 addition & 0 deletions gazelle/common/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type GazelleWalkFunc func(path string) error

// Must align with patched bazel-gazelle
const ASPECT_WALKSUBDIR = "__aspect:walksubdir"
const ASPECT_DIR_ENTRIES = "__aspect:direntries"

// Read any configuration regarding walk options.
func ReadWalkConfig(c *config.Config, rel string, f *rule.File) bool {
Expand Down
2 changes: 1 addition & 1 deletion gazelle/js/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (c *JsGazelleConfig) GetNpmPackageGenerationMode() NpmPackageMode {

// Set the pnpm-workspace.yaml file path.
func (c *JsGazelleConfig) SetPnpmLockfile(pnpmLockPath string) {
c.pnpmLockPath = pnpmLockPath
c.pnpmLockPath = path.Clean(pnpmLockPath)
}
func (c *JsGazelleConfig) PnpmLockfile() string {
return c.pnpmLockPath
Expand Down
Loading