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

ctags: allow binary to be anything with validation #652

Merged
merged 1 commit into from
Oct 5, 2023
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
4 changes: 4 additions & 0 deletions build/ctags.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func ctagsAddSymbolsParserMap(todo []*zoekt.Document, languageMap ctags.Language
parser := parserMap[parserKind]
if parser == nil {
parser = parserMap[ctags.UniversalCTags]
if parser == nil {
// this happens if CTagsMustSucceed is not true and we didn't find universal-ctags
continue
}
}

es, err := parser.Parse(doc.Name, doc.Content)
Expand Down
46 changes: 35 additions & 11 deletions ctags/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package ctags

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -119,19 +121,41 @@ func (lp *lockedParser) close() {

// NewParser creates a parser that is implemented by the given
// universal-ctags binary. The parser is safe for concurrent use.
func NewParser(bin string) (Parser, error) {
if strings.Contains(bin, "universal-ctags") || strings.Contains(bin, "scip-ctags") {
opts := goctags.Options{
Bin: bin,
func NewParser(parserType CTagsParserType, bin string) (Parser, error) {
if err := checkBinary(parserType, bin); err != nil {
return nil, err
Comment on lines +124 to +126
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this code path run often? ie. would this spawn a ton of processes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once per index job, so acceptable. IE before this change we would do one long running subprocess to universal-ctags (and communicate over stdio), now before we start the long lived process we run this first. Long lived == time it takes to index a repo.

}

opts := goctags.Options{
Bin: bin,
}
if debug {
opts.Info = log.New(os.Stderr, "CTAGS INF: ", log.LstdFlags)
opts.Debug = log.New(os.Stderr, "CTAGS DBG: ", log.LstdFlags)
}
return &lockedParser{
opts: opts,
}, nil
}

// checkBinary does checks on bin to ensure we can correctly use the binary
// for symbols. It is more user friendly to fail early in this case.
func checkBinary(typ CTagsParserType, bin string) error {
switch typ {
case UniversalCTags:
helpOutput, err := exec.Command(bin, "--help").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to check if %s is universal-ctags: %w\n--help output:\n%s", bin, err, string(helpOutput))
}
if debug {
opts.Info = log.New(os.Stderr, "CTAGS INF: ", log.LstdFlags)
opts.Debug = log.New(os.Stderr, "CTAGS DBG: ", log.LstdFlags)
if !bytes.Contains(helpOutput, []byte("+interactive")) {
return fmt.Errorf("ctags binary is not universal-ctags or is not compiled with +interactive feature: bin=%s", bin)
}

case ScipCTags:
if !strings.Contains(bin, "scip-ctags") {
return fmt.Errorf("only supports scip-ctags, not %s", bin)
}
return &lockedParser{
opts: opts,
}, nil
}

return nil, fmt.Errorf("only supports universal-ctags, not %s", bin)
return nil
}
2 changes: 1 addition & 1 deletion ctags/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestJSON(t *testing.T) {
t.Skip(err)
}

p, err := NewParser("universal-ctags")
p, err := NewParser(UniversalCTags, "universal-ctags")
if err != nil {
t.Fatal("newProcess", err)
}
Expand Down
2 changes: 1 addition & 1 deletion ctags/parser_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewParserMap(bins ParserBinMap, cTagsMustSucceed bool) (ParserMap, error) {
for _, parserType := range []CTagsParserType{UniversalCTags, ScipCTags} {
bin := bins[parserType]
if bin != "" {
parser, err := NewParser(bin)
parser, err := NewParser(parserType, bin)

if err != nil && cTagsMustSucceed {
return nil, fmt.Errorf("ctags.NewParserMap: %v", err)
Expand Down