Skip to content

Commit

Permalink
perf(cli): pre-validate aspect-configure glob patterns (#7135)
Browse files Browse the repository at this point in the history
See bmatcuk/doublestar#92 - each time a glob
is executed it gets validated if it does not successfully match a path.
This now validates it once up front and then avoids re-validating on
every (non-matched) glob invocation.

---

### Changes are visible to end-users: yes

- Searched for relevant documentation and updated as needed: yes
- Breaking change (forces users to change their own code or config): no
- Suggested release notes appear below: yes

`aspect configure` glob patterns are now validated once up front instead
of per evaluation.

### Test plan

- Covered by existing test cases

GitOrigin-RevId: 82135c3f6342ea0893d5701b6bb4cfc451e6fe0b
  • Loading branch information
jbedard committed Oct 24, 2024
1 parent a45ede5 commit c2ceebe
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions gazelle/js/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ func (c *JsGazelleConfig) PnpmLockfile() string {
// a given package. Adding an ignored dependency to a package also makes it
// ignored on a subpackage.
func (c *JsGazelleConfig) AddIgnoredImport(impGlob string) {
if !doublestar.ValidatePattern(impGlob) {
fmt.Println("Invalid js ignore import glob: ", impGlob)
return
}

c.ignoreDependencies = append(c.ignoreDependencies, impGlob)
}

Expand All @@ -324,14 +329,7 @@ func (c *JsGazelleConfig) IsImportIgnored(impt string) bool {
config := c
for config != nil {
for _, glob := range config.ignoreDependencies {
m, e := doublestar.Match(glob, impt)

if e != nil {
fmt.Println("Ignore import glob error: ", e)
return false
}

if m {
if doublestar.MatchUnvalidated(glob, impt) {
return true
}
}
Expand All @@ -343,20 +341,19 @@ func (c *JsGazelleConfig) IsImportIgnored(impt string) bool {
}

func (c *JsGazelleConfig) AddResolve(imprt string, label *label.Label) {
if !doublestar.ValidatePattern(imprt) {
fmt.Println("Invalid js resolve glob: ", imprt)
return
}

c.resolves.Put(imprt, label)
}

func (c *JsGazelleConfig) GetResolution(imprt string) *label.Label {
config := c
for config != nil {
for _, glob := range config.resolves.Keys() {
m, e := doublestar.Match(glob.(string), imprt)
if e != nil {
fmt.Println("Resolve import glob error: ", e)
return nil
}

if m {
if doublestar.MatchUnvalidated(glob.(string), imprt) {
resolveLabel, _ := config.resolves.Get(glob)
return resolveLabel.(*label.Label)
}
Expand Down Expand Up @@ -451,13 +448,8 @@ func (c *JsGazelleConfig) GetFileSourceTarget(filePath, rootDir string) *TargetG

for _, globTmpl := range sources {
glob := path.Clean(strings.Replace(globTmpl, rootDirVar, rootDir, 1))
m, e := doublestar.Match(glob, filePath)
if e != nil {
log.Fatalf("Target (%s) glob error: %v", target.name, e)
os.Exit(1)
}

if m {
if doublestar.MatchUnvalidated(glob, filePath) {
return target
}
}
Expand Down Expand Up @@ -497,6 +489,13 @@ func (c *JsGazelleConfig) addTargetGlob(targetName, glob string, isTestOnly bool
log.Fatalf("Custom %s target %s:%s can not override %s target", targetWord, c.rel, targetName, overrideWord)
os.Exit(1)
}

if !doublestar.ValidatePattern(glob) {
log.Fatalf("Invalid target (%s) glob: %v", target.name, glob)
os.Exit(1)
return
}

target.customSources = append(target.customSources, glob)
return
}
Expand Down

0 comments on commit c2ceebe

Please sign in to comment.