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

Tackle the issue of XML files filtered as binaries in search results #910

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 10 additions & 5 deletions index/shard_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,16 @@ func (b *ShardBuilder) addSymbols(symbols []*zoekt.Symbol) {
}

func DetermineLanguageIfUnknown(doc *Document) {
if doc.Language == "" {
if doc.Language != "" {
return
}

if doc.SkipReason != "" {
// If this document has been skipped, it's likely very large, or it's a non-code file like binary.
// In this case, we just guess the language based on file name to avoid examining the contents.
// Note: passing nil content is allowed by the go-enry contract (the underlying library we use here).
doc.Language = languages.GetLanguage(doc.Name, nil)
} else {
doc.Language = languages.GetLanguage(doc.Name, doc.Content)
}
}
Expand All @@ -407,16 +416,12 @@ func (b *ShardBuilder) Add(doc Document) error {

if idx := bytes.IndexByte(doc.Content, 0); idx >= 0 {
doc.SkipReason = fmt.Sprintf("binary content at byte offset %d", idx)
doc.Language = "binary"
}

if doc.SkipReason != "" {
doc.Content = []byte(notIndexedMarker + doc.SkipReason)
doc.Symbols = nil
doc.SymbolsMetaData = nil
if doc.Language == "" {
doc.Language = "skipped"
}
}

DetermineLanguageIfUnknown(&doc)
Expand Down
46 changes: 46 additions & 0 deletions index/shard_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,49 @@ func TestShardName(t *testing.T) {
})
}
}

func TestDetermineLanguageIfUnknown(t *testing.T) {
tests := []struct {
name string
doc Document
wantLang string
skipContent bool
}{
{
name: "already has language",
doc: Document{
Name: "test.java",
Language: "Go",
Content: []byte("package main"),
},
wantLang: "Go",
},
{
name: "skipped file",
doc: Document{
Name: "large.js",
SkipReason: "too large",
Content: []byte(notIndexedMarker + "too large"),
},
wantLang: "JavaScript",
},
{
name: "skipped file with unknown extension",
doc: Document{
Name: "deadb33f",
SkipReason: "binary",
Content: []byte(notIndexedMarker + "binary"),
},
wantLang: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
DetermineLanguageIfUnknown(&tt.doc)
if tt.doc.Language != tt.wantLang {
t.Errorf("DetermineLanguageIfUnknown() got language = %v, want %v", tt.doc.Language, tt.wantLang)
}
})
}
}
Loading