-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17227 from smowton/smowton/fix/baseline-vs-nonroo…
…t-vendor-dirs Go / configure-baseline: account for multiple vendor directories and the `CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS` setting
- Loading branch information
Showing
20 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
@echo off | ||
if exist vendor\modules.txt ( | ||
type "%CODEQL_EXTRACTOR_GO_ROOT%\tools\baseline-config-vendor.json" | ||
) else ( | ||
type "%CODEQL_EXTRACTOR_GO_ROOT%\tools\baseline-config-empty.json" | ||
) | ||
|
||
type NUL && "%CODEQL_EXTRACTOR_GO_ROOT%/tools/%CODEQL_PLATFORM%/go-configure-baseline.exe" | ||
exit /b %ERRORLEVEL% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
#!/bin/sh | ||
|
||
if [ -f vendor/modules.txt ]; then | ||
cat "$CODEQL_EXTRACTOR_GO_ROOT/tools/baseline-config-vendor.json" | ||
else | ||
cat "$CODEQL_EXTRACTOR_GO_ROOT/tools/baseline-config-empty.json" | ||
fi | ||
"$CODEQL_EXTRACTOR_GO_ROOT/tools/$CODEQL_PLATFORM/go-configure-baseline" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
go/extractor/cli/go-configure-baseline/go-configure-baseline.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/github/codeql-go/extractor/configurebaseline" | ||
) | ||
|
||
func main() { | ||
jsonResult, err := configurebaseline.GetConfigBaselineAsJSON(".") | ||
if err != nil { | ||
panic(err) | ||
} else { | ||
fmt.Println(string(jsonResult)) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package configurebaseline | ||
|
||
import ( | ||
"encoding/json" | ||
"io/fs" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/github/codeql-go/extractor/util" | ||
) | ||
|
||
func fileExists(path string) bool { | ||
stat, err := os.Stat(path) | ||
return err == nil && stat.Mode().IsRegular() | ||
} | ||
|
||
// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor` | ||
// and contains a `modules.txt` file. | ||
func isGolangVendorDirectory(dirPath string) bool { | ||
return filepath.Base(dirPath) == "vendor" && fileExists(filepath.Join(dirPath, "modules.txt")) | ||
} | ||
|
||
type BaselineConfig struct { | ||
PathsIgnore []string `json:"paths-ignore"` | ||
} | ||
|
||
func GetConfigBaselineAsJSON(rootDir string) ([]byte, error) { | ||
vendorDirs := make([]string, 0) | ||
|
||
if util.IsVendorDirExtractionEnabled() { | ||
// The user wants vendor directories scanned; emit an empty report. | ||
} else { | ||
filepath.WalkDir(rootDir, func(dirPath string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
// Ignore any unreadable paths -- if this script can't see it, very likely | ||
// it will not be extracted either. | ||
return nil | ||
} | ||
if isGolangVendorDirectory(dirPath) { | ||
// Note that CodeQL expects a forward-slash-separated path, even on Windows. | ||
vendorDirs = append(vendorDirs, path.Join(filepath.ToSlash(dirPath), "**")) | ||
return filepath.SkipDir | ||
} else { | ||
return nil | ||
} | ||
}) | ||
} | ||
|
||
outputStruct := BaselineConfig{PathsIgnore: vendorDirs} | ||
return json.Marshal(outputStruct) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func IsVendorDirExtractionEnabled() bool { | ||
return os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true" | ||
} |
1 change: 1 addition & 0 deletions
1
go/ql/integration-tests/all-platforms/go/configure-baseline/src/a/vendor/avendor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package abc |
Empty file.
1 change: 1 addition & 0 deletions
1
go/ql/integration-tests/all-platforms/go/configure-baseline/src/b/vendor/bvendor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package abc |
Empty file.
1 change: 1 addition & 0 deletions
1
go/ql/integration-tests/all-platforms/go/configure-baseline/src/c/vendor/cvendor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package abc |
1 change: 1 addition & 0 deletions
1
go/ql/integration-tests/all-platforms/go/configure-baseline/src/root.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package abc |
9 changes: 9 additions & 0 deletions
9
go/ql/integration-tests/all-platforms/go/configure-baseline/test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os.path | ||
import json | ||
|
||
def test(codeql, go): | ||
codeql.database.init(source_root="src") | ||
baseline_info_path = os.path.join("test-db", "baseline-info.json") | ||
with open(baseline_info_path, "r") as f: | ||
baseline_info = json.load(f) | ||
assert set(baseline_info["languages"]["go"]["files"]) == set(["root.go", "c/vendor/cvendor.go"]), "Expected root.go and cvendor.go in baseline" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: fix | ||
--- | ||
* Golang vendor directories not at the root of a repository are now correctly excluded from the baseline Go file count. This means code coverage information will be more accurate. |