Skip to content

Commit d62dfc4

Browse files
authored
Add --project-root option to override the root inside scip files (#142)
This option can be useful if the scip file is produced inside a docker container, and the project root in the file points to a non-existent location.
1 parent b687c6a commit d62dfc4

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

bindings/go/scip/testutil/format.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testutil
22

33
import (
4+
"log"
45
"net/url"
56
"os"
67
"path/filepath"
@@ -18,25 +19,36 @@ func FormatSnapshots(
1819
index *scip.Index,
1920
commentSyntax string,
2021
symbolFormatter scip.SymbolFormatter,
22+
customProjectRoot string,
2123
) ([]*scip.SourceFile, error) {
2224
var result []*scip.SourceFile
23-
projectRoot, err := url.Parse(index.Metadata.ProjectRoot)
25+
projectRootUrl, err := url.Parse(index.Metadata.ProjectRoot)
2426
if err != nil {
2527
return nil, err
2628
}
2729

30+
localSourcesRoot := projectRootUrl.Path
31+
if customProjectRoot != "" {
32+
localSourcesRoot = customProjectRoot
33+
} else if _, err := os.Stat(localSourcesRoot); errors.Is(err, os.ErrNotExist) {
34+
cwd, _ := os.Getwd()
35+
log.Printf("Project root [%s] doesn't exist, using current working directory [%s] instead. "+
36+
"To override this behaviour, use --root=<folder> option directly", projectRootUrl.Path, cwd)
37+
localSourcesRoot = cwd
38+
}
39+
2840
var documentErrors error
2941
for _, document := range index.Documents {
30-
snapshot, err := FormatSnapshot(document, index, commentSyntax, symbolFormatter)
42+
sourceFilePath := filepath.Join(localSourcesRoot, document.RelativePath)
43+
snapshot, err := FormatSnapshot(document, index, commentSyntax, symbolFormatter, sourceFilePath)
3144
err = symbolFormatter.OnError(err)
3245
if err != nil {
3346
documentErrors = errors.CombineErrors(
3447
documentErrors,
3548
errors.Wrap(err, document.RelativePath),
3649
)
3750
}
38-
sourceFile := scip.NewSourceFile(
39-
filepath.Join(projectRoot.Path, document.RelativePath),
51+
sourceFile := scip.NewSourceFile(sourceFilePath,
4052
document.RelativePath,
4153
snapshot,
4254
)
@@ -55,16 +67,10 @@ func FormatSnapshot(
5567
index *scip.Index,
5668
commentSyntax string,
5769
formatter scip.SymbolFormatter,
70+
sourceFilePath string,
5871
) (string, error) {
5972
b := strings.Builder{}
60-
uri, err := url.Parse(filepath.Join(index.Metadata.ProjectRoot, document.RelativePath))
61-
if err != nil {
62-
return "", err
63-
}
64-
if uri.Scheme != "file" {
65-
return "", errors.New("expected url scheme 'file', obtained " + uri.Scheme)
66-
}
67-
data, err := os.ReadFile(uri.Path)
73+
data, err := os.ReadFile(sourceFilePath)
6874
if err != nil {
6975
return "", err
7076
}

cmd/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestSCIPSnapshots(t *testing.T) {
9191
require.Nil(t, err)
9292
symbolFormatter := scip.DescriptorOnlyFormatter
9393
symbolFormatter.IncludePackageName = func(name string) bool { return name != testName }
94-
snapshots, err := testutil.FormatSnapshots(index, "#", symbolFormatter)
94+
snapshots, err := testutil.FormatSnapshots(index, "#", symbolFormatter, "")
9595
require.Nil(t, err)
9696
if debugSnapshotAbspaths != nil && *debugSnapshotAbspaths {
9797
inputDirAbsPath, err := filepath.Abs(inputDirectory)

cmd/snapshot.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

1515
type snapshotFlags struct {
16-
from string
17-
output string
18-
strict bool
19-
commentSyntax string
16+
from string
17+
output string
18+
customProjectRoot string
19+
strict bool
20+
commentSyntax string
2021
}
2122

2223
func snapshotCommand() cli.Command {
@@ -36,6 +37,12 @@ and symbol information.`,
3637
Destination: &snapshotFlags.output,
3738
Value: "scip-snapshot",
3839
},
40+
&cli.StringFlag{
41+
Name: "project-root",
42+
Usage: "Override project root in the SCIP file. " +
43+
"For example, this can be helpful when the SCIP index was created inside a Docker image or created on another computer",
44+
Destination: &snapshotFlags.customProjectRoot,
45+
},
3946
&cli.BoolFlag{
4047
Name: "strict",
4148
Usage: "If true, fail fast on errors",
@@ -74,7 +81,7 @@ func snapshotMain(flags snapshotFlags) error {
7481
return errors.Wrap(err, "use --strict=false to ignore this error")
7582
}
7683
}
77-
snapshots, err := testutil.FormatSnapshots(index, flags.commentSyntax, symbolFormatter)
84+
snapshots, err := testutil.FormatSnapshots(index, flags.commentSyntax, symbolFormatter, flags.customProjectRoot)
7885
if err != nil {
7986
return err
8087
}

0 commit comments

Comments
 (0)