Skip to content
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
29 changes: 25 additions & 4 deletions detect/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,17 @@ func (e *Engine) matchTool(tool *kb.ToolDef) brief.Confidence {
}

// exists checks if a file, directory, or glob pattern matches something at the project root.
// A trailing "/" means the pattern must match a directory. Glob patterns without
// a trailing "/" only match regular files so that a NEWS.d/ directory does not
// register as D source.
func (e *Engine) exists(pattern string) bool {
e.filesChecked++

if strings.HasSuffix(pattern, "/") {
info, err := os.Stat(filepath.Join(e.Root, pattern))
if dir, ok := strings.CutSuffix(pattern, "/"); ok {
if kb.HasGlobPattern(dir) {
return e.globMatches(dir, true)
}
info, err := os.Stat(filepath.Join(e.Root, dir))
return err == nil && info.IsDir()
}

Expand All @@ -428,14 +434,29 @@ func (e *Engine) exists(pattern string) bool {
}

if kb.HasGlobPattern(pattern) {
matches, err := filepath.Glob(filepath.Join(e.Root, pattern))
return err == nil && len(matches) > 0
return e.globMatches(pattern, false)
}

_, err := os.Stat(filepath.Join(e.Root, pattern))
return err == nil
}

// globMatches reports whether a root-level glob pattern matches at least one
// entry of the requested kind.
func (e *Engine) globMatches(pattern string, wantDir bool) bool {
matches, err := filepath.Glob(filepath.Join(e.Root, pattern))
if err != nil {
return false
}
for _, m := range matches {
info, err := os.Stat(m)
if err == nil && info.IsDir() == wantDir {
return true
}
}
return false
}

// recursiveGlob handles ** patterns by checking against the cached file extension set.
// Falls back to a bounded walk if the cache isn't populated.
func (e *Engine) recursiveGlob(pattern string) bool {
Expand Down
40 changes: 40 additions & 0 deletions detect/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,46 @@ func TestInvokeDetection(t *testing.T) {
}
}

func TestGlobIgnoresDirectories(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n"), 0o644); err != nil {
t.Fatal(err)
}
for _, d := range []string{"NEWS.d", "scratch.js"} {
if err := os.Mkdir(filepath.Join(dir, d), 0o755); err != nil {
t.Fatal(err)
}
}

r, err := New(loadKB(t), dir).Run()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for _, lang := range r.Languages {
if lang.Name == "D" || lang.Name == "JavaScript" {
t.Errorf("directory with source extension should not trigger %s detection", lang.Name)
}
}
if len(r.Languages) == 0 || r.Languages[0].Name != "Go" {
t.Errorf("expected Go as primary language, got %v", r.Languages)
}
}

func TestDirectoryGlobPattern(t *testing.T) {
dir := t.TempDir()
if err := os.Mkdir(filepath.Join(dir, "Foo.xcodeproj"), 0o755); err != nil {
t.Fatal(err)
}

engine := New(loadKB(t), dir)
if !engine.exists("*.xcodeproj/") {
t.Error("expected *.xcodeproj/ to match Foo.xcodeproj directory")
}
if engine.exists("*.xcodeproj") {
t.Error("*.xcodeproj without trailing slash should not match a directory")
}
}

func assertToolDetected(t *testing.T, r *brief.Report, category, name string) {
t.Helper()
tools, ok := r.Tools[category]
Expand Down
2 changes: 1 addition & 1 deletion knowledge/objc/language.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ docs = "https://developer.apple.com/library/archive/documentation/Cocoa/Conceptu
description = "Object-oriented superset of C for Apple platforms"

[detect]
files = ["*.m", "**/*.m", "*.mm", "**/*.mm", "*.xcodeproj", "*.xcworkspace"]
files = ["*.m", "**/*.m", "*.mm", "**/*.mm", "*.xcodeproj/", "*.xcworkspace/"]
ecosystems = ["objc"]

[taxonomy]
Expand Down
Loading