Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
feat: Add ability to disable features via flags (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries authored Nov 24, 2021
1 parent 4a008c8 commit 1cfd97b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
26 changes: 16 additions & 10 deletions cmd/lsif-go/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ var app = kingpin.New(
).Version(version + ", protocol version " + protocol.Version)

var (
outFile string
projectRoot string
moduleRoot string
repositoryRoot string
repositoryRemote string
moduleVersion string
verbosity int
noOutput bool
noAnimation bool
depBatchSize int
outFile string
projectRoot string
moduleRoot string
repositoryRoot string
repositoryRemote string
moduleVersion string
verbosity int
noOutput bool
noAnimation bool
depBatchSize int
enableApiDocs bool
enableImplementations bool
)

func init() {
Expand All @@ -51,6 +53,10 @@ func init() {
app.Flag("no-animation", "Do not animate output.").Default("false").BoolVar(&noAnimation)

app.Flag("dep-batch-size", "How many dependencies to load at once to limit memory usage (e.g. 100). 0 means load all at once.").Default("0").IntVar(&depBatchSize)

// Feature flags
app.Flag("enable-api-docs", "Enable Sourcegraph API Doc generation").Default("true").BoolVar(&enableApiDocs)
app.Flag("enable-implementations", "Enable textDocument/implementation generation").Default("true").BoolVar(&enableImplementations)
}

func parseArgs(args []string) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/lsif-go/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/sourcegraph/sourcegraph/lib/codeintel/lsif/protocol/writer"
)

func writeIndex(repositoryRoot, repositoryRemote, projectRoot, moduleName, moduleVersion string, dependencies map[string]gomod.GoModule, projectDependencies []string, outFile string, outputOptions output.Options, depBatchSize int) error {
func writeIndex(repositoryRoot, repositoryRemote, projectRoot, moduleName, moduleVersion string, dependencies map[string]gomod.GoModule, projectDependencies []string, outFile string, outputOptions output.Options, generationOptions indexer.GenerationOptions) error {
start := time.Now()

out, err := os.Create(outFile)
Expand Down Expand Up @@ -44,7 +44,7 @@ func writeIndex(repositoryRoot, repositoryRemote, projectRoot, moduleName, modul
writer.NewJSONWriter(out),
packageDataCache,
outputOptions,
depBatchSize,
generationOptions,
)

if err := indexer.Index(); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion cmd/lsif-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/sourcegraph/lsif-go/internal/git"
"github.com/sourcegraph/lsif-go/internal/gomod"
"github.com/sourcegraph/lsif-go/internal/indexer"
"github.com/sourcegraph/lsif-go/internal/output"
)

Expand Down Expand Up @@ -65,6 +66,11 @@ func mainErr() (err error) {
return fmt.Errorf("failed to list project dependencies: %v", err)
}

generationOptions := indexer.NewGenerationOptions()
generationOptions.EnableApiDocs = enableApiDocs
generationOptions.EnableImplementations = enableImplementations
generationOptions.DepBatchSize = depBatchSize

if err := writeIndex(
repositoryRoot,
repositoryRemote,
Expand All @@ -75,7 +81,7 @@ func mainErr() (err error) {
projectDependencies,
outFile,
outputOptions,
depBatchSize,
generationOptions,
); err != nil {
return fmt.Errorf("failed to index: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/indexer/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (e emittedDocumentationResults) addAll(other emittedDocumentationResults) m

// indexDocumentation indexes all packages in the project.
func (i *Indexer) indexDocumentation() error {
if !i.generationOptions.EnableApiDocs {
return nil
}

var (
d = &docsIndexer{i: i}
mu sync.Mutex
Expand Down
6 changes: 5 additions & 1 deletion internal/indexer/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func (rel *implRelation) linkInterfaceToReceivers(idx int, interfaceMethods []st
// NOTE: if indexImplementations becomes multi-threaded then we would need to update
// Indexer.ensureImplementationMoniker to ensure that it uses appropriate locking.
func (i *Indexer) indexImplementations() error {
if !i.generationOptions.EnableImplementations {
return nil
}

var implErr error

output.WithProgress("Indexing implementations", func() {
Expand Down Expand Up @@ -392,7 +396,7 @@ func (i *Indexer) extractInterfacesAndConcreteTypes(pkgNames []string) (interfac
for ix, pkgName := range pkgNames {
pkgBatch = append(pkgBatch, pkgName)

if i.depBatchSize != 0 && ix%i.depBatchSize == 0 {
if i.generationOptions.DepBatchSize != 0 && ix%i.generationOptions.DepBatchSize == 0 {
err := batch(pkgBatch)
runtime.GC() // Prevent a garbage pile
if err != nil {
Expand Down
20 changes: 17 additions & 3 deletions internal/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ type importMonikerReference struct {
}
type setVal interface{}

type GenerationOptions struct {
EnableApiDocs bool
EnableImplementations bool
DepBatchSize int
}

func NewGenerationOptions() GenerationOptions {
return GenerationOptions{
EnableApiDocs: true,
EnableImplementations: true,
DepBatchSize: 0,
}
}

type Indexer struct {
repositoryRoot string // path to repository
repositoryRemote string // import path inferred by git remote
Expand Down Expand Up @@ -79,7 +93,7 @@ type Indexer struct {

importMonikerChannel chan importMonikerReference

depBatchSize int
generationOptions GenerationOptions
}

func New(
Expand All @@ -94,7 +108,7 @@ func New(
jsonWriter writer.JSONWriter,
packageDataCache *PackageDataCache,
outputOptions output.Options,
depBatchSize int,
generationOptions GenerationOptions,
) *Indexer {
return &Indexer{
repositoryRoot: repositoryRoot,
Expand Down Expand Up @@ -124,7 +138,7 @@ func New(
packageDataCache: packageDataCache,
stripedMutex: newStripedMutex(),
importMonikerChannel: make(chan importMonikerReference, 512),
depBatchSize: depBatchSize,
generationOptions: generationOptions,
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestIndexer(t *testing.T) {
w,
NewPackageDataCache(),
output.Options{},
0,
NewGenerationOptions(),
)

if err := indexer.Index(); err != nil {
Expand Down Expand Up @@ -651,7 +651,7 @@ func TestIndexer_documentation(t *testing.T) {
writer.NewJSONWriter(&buf),
NewPackageDataCache(),
output.Options{},
0,
NewGenerationOptions(),
)
if err := indexer.Index(); err != nil {
t.Fatalf("unexpected error indexing testdata: %s", err.Error())
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestIndexer_shouldVisitPackage(t *testing.T) {
w,
NewPackageDataCache(),
output.Options{},
0,
NewGenerationOptions(),
)

if err := indexer.loadPackages(false); err != nil {
Expand Down

0 comments on commit 1cfd97b

Please sign in to comment.