Skip to content

Commit 774eb0d

Browse files
committed
Go: merge with incoming path transformer when setting GOPATH
1 parent bdb3fe4 commit 774eb0d

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

go/extractor/cli/go-autobuilder/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/github/codeql-go/extractor/autobuilder"
1414
"github.com/github/codeql-go/extractor/diagnostics"
1515
"github.com/github/codeql-go/extractor/project"
16+
"github.com/github/codeql-go/extractor/srcarchive"
1617
"github.com/github/codeql-go/extractor/toolchain"
1718
"github.com/github/codeql-go/extractor/util"
1819
)
@@ -283,7 +284,7 @@ func createPathTransformerFile(newdir string) *os.File {
283284
}
284285

285286
// Writes the path transformer file
286-
func writePathTransformerFile(pt *os.File, realSrc, root, newdir string) {
287+
func writePathTransformerFile(pt *os.File, realSrc, newdir string) {
287288
_, err := pt.WriteString("#" + realSrc + "\n" + newdir + "//\n")
288289
if err != nil {
289290
log.Fatalf("Unable to write path transformer file: %s.", err.Error())
@@ -501,10 +502,6 @@ func installDependenciesAndBuild() {
501502

502503
srcdir := getSourceDir()
503504

504-
// we set `CODEQL_PATH_TRANSFORMER` ourselves in some cases, so blank it out first for consistency
505-
os.Setenv("CODEQL_PATH_TRANSFORMER", "")
506-
os.Setenv("SEMMLE_PATH_TRANSFORMER", "")
507-
508505
// determine how to install dependencies and whether a GOPATH needs to be set up before
509506
// extraction
510507
workspaces := project.GetWorkspaceInfo(true)
@@ -535,7 +532,21 @@ func installDependenciesAndBuild() {
535532
pt := createPathTransformerFile(paths.newdir)
536533
defer os.Remove(pt.Name())
537534

538-
writePathTransformerFile(pt, paths.realSrc, paths.root, paths.newdir)
535+
// We're about to create out own path transformer, so that paths containing the
536+
// temporary GOPATH point to the right location. However, if there was already an
537+
// incoming path transformer, the right location will be what _it_ wanted to transform
538+
// paths to.
539+
existingPathTransformer, err := srcarchive.LoadProjectLayoutFromEnv()
540+
if err != nil {
541+
log.Fatalf("Unable to load path transformer: %s.\n", err.Error())
542+
}
543+
var realSrc string
544+
if existingPathTransformer == nil {
545+
realSrc = paths.realSrc
546+
} else {
547+
realSrc = existingPathTransformer.To
548+
}
549+
writePathTransformerFile(pt, realSrc, paths.newdir)
539550
setGopath(paths.root)
540551
}
541552
}

go/extractor/srcarchive/projectlayout.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"os"
88
"strings"
9+
10+
"github.com/github/codeql-go/extractor/util"
911
)
1012

1113
// ProjectLayout describes a very simple project layout rewriting paths starting
@@ -16,7 +18,7 @@ import (
1618
// # to
1719
// from//
1820
type ProjectLayout struct {
19-
from, to string
21+
From, To string
2022
}
2123

2224
// normaliseSlashes adds an initial slash to `path` if there isn't one, and trims
@@ -28,6 +30,25 @@ func normaliseSlashes(path string) string {
2830
return strings.TrimSuffix(path, "/")
2931
}
3032

33+
// LoadProjectLayoutFromEnv loads a project layout from the file referenced by the
34+
// {CODEQL,SEMMLE}_PATH_TRANSFORMER environment variable. If neither env var is set, returns nil. If
35+
// the file cannot be read or does not have the right format, it returns an error.
36+
func LoadProjectLayoutFromEnv() (*ProjectLayout, error) {
37+
pt := util.Getenv("CODEQL_PATH_TRANSFORMER", "SEMMLE_PATH_TRANSFORMER")
38+
if pt == "" {
39+
return nil, nil
40+
}
41+
ptf, err := os.Open(pt)
42+
if err != nil {
43+
return nil, err
44+
}
45+
pathTransformer, err = LoadProjectLayout(ptf)
46+
if err != nil {
47+
return nil, err
48+
}
49+
return pathTransformer, nil
50+
}
51+
3152
// LoadProjectLayout loads a project layout from the given file, returning an error
3253
// if the file does not have the right format
3354
func LoadProjectLayout(file *os.File) (*ProjectLayout, error) {
@@ -41,7 +62,7 @@ func LoadProjectLayout(file *os.File) (*ProjectLayout, error) {
4162
if !strings.HasPrefix(line, "#") {
4263
return nil, fmt.Errorf("first line of project layout should start with #, but got %s", line)
4364
}
44-
res.to = normaliseSlashes(strings.TrimSpace(strings.TrimPrefix(line, "#")))
65+
res.To = normaliseSlashes(strings.TrimSpace(strings.TrimPrefix(line, "#")))
4566

4667
if !scanner.Scan() {
4768
return nil, errors.New("empty section in project-layout file")
@@ -57,7 +78,7 @@ func LoadProjectLayout(file *os.File) (*ProjectLayout, error) {
5778
if strings.HasPrefix(line, "-") || strings.Contains(line, "*") || strings.Contains(line, "//") {
5879
return nil, errors.New("unsupported project-layout feature")
5980
}
60-
res.from = normaliseSlashes(line)
81+
res.From = normaliseSlashes(line)
6182

6283
for scanner.Scan() {
6384
if strings.TrimSpace(scanner.Text()) != "" {
@@ -71,11 +92,11 @@ func LoadProjectLayout(file *os.File) (*ProjectLayout, error) {
7192
// transformString transforms `str` as specified by the project layout: if it starts with the `from`
7293
// prefix, that prefix is relaced by `to`; otherwise the string is returned unchanged
7394
func (p *ProjectLayout) transformString(str string) string {
74-
if str == p.from {
75-
return p.to
95+
if str == p.From {
96+
return p.To
7697
}
77-
if strings.HasPrefix(str, p.from+"/") {
78-
return p.to + "/" + str[len(p.from)+1:]
98+
if strings.HasPrefix(str, p.From+"/") {
99+
return p.To + "/" + str[len(p.From)+1:]
79100
}
80101
return str
81102
}

go/extractor/srcarchive/srcarchive.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10-
11-
"github.com/github/codeql-go/extractor/util"
1210
)
1311

1412
var pathTransformer *ProjectLayout
1513

1614
func init() {
17-
pt := util.Getenv("CODEQL_PATH_TRANSFORMER", "SEMMLE_PATH_TRANSFORMER")
18-
if pt != "" {
19-
ptf, err := os.Open(pt)
20-
if err != nil {
21-
log.Fatalf("Unable to open path transformer %s: %s.\n", pt, err.Error())
22-
}
23-
pathTransformer, err = LoadProjectLayout(ptf)
24-
if err != nil {
25-
log.Fatalf("Unable to initialize path transformer: %s.\n", err.Error())
26-
}
15+
pt, err := LoadProjectLayoutFromEnv()
16+
if err == nil {
17+
pathTransformer = pt
18+
} else {
19+
log.Fatalf("Unable to load path transformer: %s.\n", err.Error())
2720
}
2821
}
2922

0 commit comments

Comments
 (0)