From 476eae6729f947792df87fd066420f89b9a96272 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Fri, 13 Dec 2024 19:45:28 +0530 Subject: [PATCH] fix: Propagate errors when creating repro SCIP index --- cmd/scip/lint.go | 6 +++--- cmd/scip/tests/reprolang/bindings/go/repro/ast.go | 6 ++++-- cmd/scip/tests/reprolang/bindings/go/repro/indexer.go | 9 ++++++++- cmd/scip/tests/reprolang/bindings/go/repro/namer.go | 9 ++++++--- .../tests/snapshots/input/global-workspace/hello.repro | 1 + .../tests/snapshots/input/local-document/local2.repro | 4 ++-- .../snapshots/output/global-cross-repo/reference.repro | 2 +- .../tests/snapshots/output/global-workspace/hello.repro | 4 ++++ .../tests/snapshots/output/local-document/local2.repro | 8 +++++--- 9 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 cmd/scip/tests/snapshots/input/global-workspace/hello.repro create mode 100755 cmd/scip/tests/snapshots/output/global-workspace/hello.repro diff --git a/cmd/scip/lint.go b/cmd/scip/lint.go index 8c0a0ff0..05dee6cf 100644 --- a/cmd/scip/lint.go +++ b/cmd/scip/lint.go @@ -1,7 +1,7 @@ package main import ( - "errors" + stderrors "errors" "fmt" "sort" "strings" @@ -21,7 +21,7 @@ func lintCommand() cli.Command { Action: func(c *cli.Context) error { indexPath := c.Args().Get(0) if indexPath == "" { - return errors.New("missing argument for path to SCIP index") + return stderrors.New("missing argument for path to SCIP index") } return lintMain(indexPath) }, @@ -34,7 +34,7 @@ func lintMain(indexPath string) error { if err != nil { return err } - return errors.Join(lintMainPure(scipIndex).data...) + return stderrors.Join(lintMainPure(scipIndex).data...) } func lintMainPure(scipIndex *scip.Index) errorSet { diff --git a/cmd/scip/tests/reprolang/bindings/go/repro/ast.go b/cmd/scip/tests/reprolang/bindings/go/repro/ast.go index 372841fb..d77bb813 100644 --- a/cmd/scip/tests/reprolang/bindings/go/repro/ast.go +++ b/cmd/scip/tests/reprolang/bindings/go/repro/ast.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/cockroachdb/errors" sitter "github.com/smacker/go-tree-sitter" "github.com/sourcegraph/scip/bindings/go/scip" @@ -77,16 +78,17 @@ func NewRangePositionFromNode(node *sitter.Node) *scip.Range { } } -func (i *identifier) resolveSymbol(localScope *reproScope, context *reproContext) { +func (i *identifier) resolveSymbol(localScope *reproScope, context *reproContext) error { scope := context.globalScope if i.isLocalSymbol() { scope = localScope } symbol, ok := scope.names[i.value] if !ok { - symbol = "local ERROR_UNRESOLVED_SYMBOL" + return errors.Newf("could not resolve local symbol %q", i.value) } i.symbol = symbol + return nil } func (i *identifier) isLocalSymbol() bool { diff --git a/cmd/scip/tests/reprolang/bindings/go/repro/indexer.go b/cmd/scip/tests/reprolang/bindings/go/repro/indexer.go index 6fd8ea58..279fb56e 100644 --- a/cmd/scip/tests/reprolang/bindings/go/repro/indexer.go +++ b/cmd/scip/tests/reprolang/bindings/go/repro/indexer.go @@ -3,6 +3,7 @@ package repro import ( "context" + "github.com/cockroachdb/errors" "github.com/sourcegraph/scip/bindings/go/scip" ) @@ -69,8 +70,14 @@ func Index( } // Phase 3: resolve names for references + var allErrs error for _, file := range reproSources { - file.resolveReferences(ctx) + if err := file.resolveReferences(ctx); err != nil { + allErrs = errors.CombineErrors(allErrs, errors.Wrapf(err, "file %q", file.Source.AbsolutePath)) + } + } + if allErrs != nil { + return nil, allErrs } // Phase 4: emit SCIP diff --git a/cmd/scip/tests/reprolang/bindings/go/repro/namer.go b/cmd/scip/tests/reprolang/bindings/go/repro/namer.go index 6173ca8d..c466f371 100644 --- a/cmd/scip/tests/reprolang/bindings/go/repro/namer.go +++ b/cmd/scip/tests/reprolang/bindings/go/repro/namer.go @@ -3,6 +3,7 @@ package repro import ( "fmt" + "github.com/cockroachdb/errors" "github.com/sourcegraph/scip/bindings/go/scip" ) @@ -58,13 +59,14 @@ func (s *reproSourceFile) enterDefinitions(context *reproContext) { } // resolveReferences updates the .symbol field for all names of reference identifiers. -func (s *reproSourceFile) resolveReferences(context *reproContext) { +func (s *reproSourceFile) resolveReferences(context *reproContext) error { + var err error resolveIdents := func(rel relationships) { for _, ident := range rel.identifiers() { if ident == nil { continue } - ident.resolveSymbol(s.localScope, context) + err = errors.CombineErrors(err, ident.resolveSymbol(s.localScope, context)) } } for _, def := range s.definitions { @@ -74,8 +76,9 @@ func (s *reproSourceFile) resolveReferences(context *reproContext) { resolveIdents(rel.relations) } for _, ref := range s.references { - ref.name.resolveSymbol(s.localScope, context) + err = errors.CombineErrors(err, ref.name.resolveSymbol(s.localScope, context)) } + return err } // newGlobalSymbol returns an SCIP symbol for the given definition. diff --git a/cmd/scip/tests/snapshots/input/global-workspace/hello.repro b/cmd/scip/tests/snapshots/input/global-workspace/hello.repro new file mode 100644 index 00000000..846c831d --- /dev/null +++ b/cmd/scip/tests/snapshots/input/global-workspace/hello.repro @@ -0,0 +1 @@ +definition hello(). \ No newline at end of file diff --git a/cmd/scip/tests/snapshots/input/local-document/local2.repro b/cmd/scip/tests/snapshots/input/local-document/local2.repro index 19537a70..076a5f20 100644 --- a/cmd/scip/tests/snapshots/input/local-document/local2.repro +++ b/cmd/scip/tests/snapshots/input/local-document/local2.repro @@ -1,2 +1,2 @@ -reference localExample - +definition localExample +reference localExample \ No newline at end of file diff --git a/cmd/scip/tests/snapshots/output/global-cross-repo/reference.repro b/cmd/scip/tests/snapshots/output/global-cross-repo/reference.repro index a36de0fe..042bf987 100755 --- a/cmd/scip/tests/snapshots/output/global-cross-repo/reference.repro +++ b/cmd/scip/tests/snapshots/output/global-cross-repo/reference.repro @@ -1,6 +1,6 @@ # Reference a global symbol from another workspace. reference global global-workspace hello.repro/hello(). -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference local ERROR_UNRESOLVED_SYMBOL +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference global-workspace hello.repro/hello(). reference global duplicates duplicate.repro/readFileSync. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference duplicates duplicate.repro/readFileSync. diff --git a/cmd/scip/tests/snapshots/output/global-workspace/hello.repro b/cmd/scip/tests/snapshots/output/global-workspace/hello.repro new file mode 100755 index 00000000..f79a2591 --- /dev/null +++ b/cmd/scip/tests/snapshots/output/global-workspace/hello.repro @@ -0,0 +1,4 @@ + definition hello(). +# ^^^^^^^^ definition hello.repro/hello(). +# documentation +# > signature of hello(). diff --git a/cmd/scip/tests/snapshots/output/local-document/local2.repro b/cmd/scip/tests/snapshots/output/local-document/local2.repro index 0f4ed3c4..b7a32a32 100755 --- a/cmd/scip/tests/snapshots/output/local-document/local2.repro +++ b/cmd/scip/tests/snapshots/output/local-document/local2.repro @@ -1,4 +1,6 @@ + definition localExample +# ^^^^^^^^^^^^ definition local Example +# documentation +# > signature of localExample reference localExample -# ^^^^^^^^^^^^ reference local ERROR_UNRESOLVED_SYMBOL - - +# ^^^^^^^^^^^^ reference local Example