Skip to content

Commit e2d4e9e

Browse files
authored
Snapshot: don't fail on unparseable symbols. (#118)
* Snapshot: don't fail on unparseable symbols. Previously, the `scip snapshot` command failed if there was a symbol that wasn't parseable. This was unhelpful when trying to debug why navigation was not working as expected (which is the main situation where I use `scip snapshot`). Unparseable symbols are not invalid, they just don't work with cross-repo navigation. To fail fast like before, there's a new `scip snapshot --strict` flag that fails if there's a parser error. * Enable strict by default and explain how to disable strict
1 parent c0b47ff commit e2d4e9e

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

bindings/go/scip/symbol_formatter.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
// Excluding parts of the symbol can be helpful for testing purposes. For example, snapshot tests may hardcode
99
// the package version number so it's easier to read the snapshot tests if the version is excluded.
1010
type SymbolFormatter struct {
11+
OnError func(err error) error
1112
IncludeScheme func(scheme string) bool
1213
IncludePackageManager func(manager string) bool
1314
IncludePackageName func(name string) bool
@@ -17,6 +18,17 @@ type SymbolFormatter struct {
1718

1819
// VerboseSymbolFormatter formats all parts of the symbol.
1920
var VerboseSymbolFormatter = SymbolFormatter{
21+
OnError: func(err error) error { return err },
22+
IncludeScheme: func(_ string) bool { return true },
23+
IncludePackageManager: func(_ string) bool { return true },
24+
IncludePackageName: func(_ string) bool { return true },
25+
IncludePackageVersion: func(_ string) bool { return true },
26+
IncludeDescriptor: func(_ string) bool { return true },
27+
}
28+
29+
// Same as VerboseSymbolFormatter but silently ignores errors.
30+
var LenientVerboseSymbolFormatter = SymbolFormatter{
31+
OnError: func(_ error) error { return nil },
2032
IncludeScheme: func(_ string) bool { return true },
2133
IncludePackageManager: func(_ string) bool { return true },
2234
IncludePackageName: func(_ string) bool { return true },
@@ -26,6 +38,7 @@ var VerboseSymbolFormatter = SymbolFormatter{
2638

2739
// DescriptorOnlyFormatter formats only the descriptor part of the symbol.
2840
var DescriptorOnlyFormatter = SymbolFormatter{
41+
OnError: func(err error) error { return err },
2942
IncludeScheme: func(scheme string) bool { return scheme == "local" },
3043
IncludePackageManager: func(_ string) bool { return false },
3144
IncludePackageName: func(_ string) bool { return false },

bindings/go/scip/testutil/format.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ func FormatSnapshots(
2424
if err != nil {
2525
return nil, err
2626
}
27+
28+
var documentErrors error
2729
for _, document := range index.Documents {
2830
snapshot, err := FormatSnapshot(document, index, commentSyntax, symbolFormatter)
31+
err = symbolFormatter.OnError(err)
2932
if err != nil {
30-
return nil, err
33+
documentErrors = errors.CombineErrors(
34+
documentErrors,
35+
errors.Wrap(err, document.RelativePath),
36+
)
3137
}
3238
sourceFile := scip.NewSourceFile(
3339
filepath.Join(projectRoot.Path, document.RelativePath),
@@ -36,6 +42,9 @@ func FormatSnapshots(
3642
)
3743
result = append(result, sourceFile)
3844
}
45+
if documentErrors != nil {
46+
return nil, documentErrors
47+
}
3948
return result, nil
4049
}
4150

@@ -68,7 +77,7 @@ func FormatSnapshot(
6877
formatted, err := formatter.Format(symbol)
6978
if err != nil {
7079
formattingError = errors.CombineErrors(formattingError, errors.Wrapf(err, symbol))
71-
return err.Error()
80+
return symbol
7281
}
7382
return formatted
7483
}

cmd/snapshot.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99

1010
"github.com/sourcegraph/scip/bindings/go/scip"
1111
"github.com/sourcegraph/scip/bindings/go/scip/testutil"
12+
"github.com/sourcegraph/sourcegraph/lib/errors"
1213
)
1314

1415
type snapshotFlags struct {
1516
from string
1617
output string
18+
strict bool
1719
}
1820

1921
func snapshotCommand() cli.Command {
@@ -31,7 +33,13 @@ and symbol information.`,
3133
Name: "to",
3234
Usage: "Path to output directory for snapshot files",
3335
Destination: &snapshotFlags.output,
34-
Value: "scip-snapshot",
36+
Value: "scip-snapshot",
37+
},
38+
&cli.BoolFlag{
39+
Name: "strict",
40+
Usage: "If true, fail fast on errors",
41+
Destination: &snapshotFlags.strict,
42+
Value: true,
3543
},
3644
},
3745
Action: func(c *cli.Context) error {
@@ -52,7 +60,14 @@ func snapshotMain(flags snapshotFlags) error {
5260
if err != nil {
5361
return err
5462
}
55-
snapshots, err := testutil.FormatSnapshots(index, "//", scip.VerboseSymbolFormatter)
63+
symbolFormatter := scip.LenientVerboseSymbolFormatter
64+
if flags.strict {
65+
symbolFormatter = scip.VerboseSymbolFormatter
66+
symbolFormatter.OnError = func(err error) error {
67+
return errors.Wrap(err, "use --strict=false to ignore this error")
68+
}
69+
}
70+
snapshots, err := testutil.FormatSnapshots(index, "//", symbolFormatter)
5671
if err != nil {
5772
return err
5873
}

0 commit comments

Comments
 (0)