Skip to content

Commit 21366dd

Browse files
committed
Go / configure-baseline: account for multiple vendor directories and the CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS setting
Our existing configure-baseline scripts would give the wrong result if a `vendor` directory wasn't at the root of the repository, or if the `CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS` variable was set to `true` indicating the user wants their vendored code scanned. Here I replace the shell scripts that implemented the very simplest behaviour with a small Go program.
1 parent 8b4e060 commit 21366dd

File tree

9 files changed

+98
-18
lines changed

9 files changed

+98
-18
lines changed

go/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ codeql_pkg_files(
4747
"//go/extractor/cli/go-autobuilder",
4848
"//go/extractor/cli/go-bootstrap",
4949
"//go/extractor/cli/go-build-runner",
50+
"//go/extractor/cli/go-configure-baseline",
5051
"//go/extractor/cli/go-extractor",
5152
"//go/extractor/cli/go-gen-dbscheme",
5253
"//go/extractor/cli/go-tokenizer",

go/codeql-tools/baseline-config-empty.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

go/codeql-tools/baseline-config-vendor.json

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@echo off
2-
if exist vendor\modules.txt (
3-
type "%CODEQL_EXTRACTOR_GO_ROOT%\tools\baseline-config-vendor.json"
4-
) else (
5-
type "%CODEQL_EXTRACTOR_GO_ROOT%\tools\baseline-config-empty.json"
6-
)
2+
SETLOCAL EnableDelayedExpansion
3+
4+
type NUL && "%CODEQL_EXTRACTOR_GO_ROOT%/tools/%CODEQL_PLATFORM%/go-configure-baseline.exe"
5+
exit /b %ERRORLEVEL%
6+
7+
ENDLOCAL

go/codeql-tools/configure-baseline.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#!/bin/sh
22

3-
if [ -f vendor/modules.txt ]; then
4-
cat "$CODEQL_EXTRACTOR_GO_ROOT/tools/baseline-config-vendor.json"
5-
else
6-
cat "$CODEQL_EXTRACTOR_GO_ROOT/tools/baseline-config-empty.json"
7-
fi
3+
"$CODEQL_EXTRACTOR_GO_ROOT/tools/$CODEQL_PLATFORM/go-configure-baseline"

go/extractor/cli/go-configure-baseline/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/github/codeql-go/extractor/configurebaseline"
5+
)
6+
7+
func main() {
8+
jsonResult, err := configurebaseline.GetConfigBaselineAsJSON(".")
9+
if err != nil {
10+
panic(err)
11+
} else {
12+
println(string(jsonResult))
13+
}
14+
}

go/extractor/configurebaseline/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package configurebaseline
2+
3+
import (
4+
"encoding/json"
5+
"io/fs"
6+
"os"
7+
"path"
8+
"path/filepath"
9+
)
10+
11+
func fileExists(path string) bool {
12+
stat, err := os.Stat(path)
13+
return err == nil && stat.Mode().IsRegular()
14+
}
15+
16+
func isGolangVendorDirectory(dirPath string) bool {
17+
// Call a directory a Golang vendor directory if it contains a modules.txt file.
18+
return path.Base(dirPath) == "vendor" && fileExists(path.Join(dirPath, "modules.txt"))
19+
}
20+
21+
type PathsIgnoreStruct struct {
22+
PathsIgnore []string `json:"paths-ignore"`
23+
}
24+
25+
func GetConfigBaselineAsJSON(rootDir string) ([]byte, error) {
26+
vendorDirs := make([]string, 0)
27+
28+
// If CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS is "true":
29+
if os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true" {
30+
// The user wants vendor directories scanned; emit an empty report.
31+
} else {
32+
filepath.WalkDir(rootDir, func(dirPath string, d fs.DirEntry, err error) error {
33+
if err != nil {
34+
// Mask any unreadable paths.
35+
return nil
36+
}
37+
if isGolangVendorDirectory(dirPath) {
38+
vendorDirs = append(vendorDirs, path.Join(dirPath, "**"))
39+
return filepath.SkipDir
40+
} else {
41+
return nil
42+
}
43+
})
44+
}
45+
46+
outputStruct := PathsIgnoreStruct{PathsIgnore: vendorDirs}
47+
return json.Marshal(outputStruct)
48+
}

0 commit comments

Comments
 (0)