Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go / configure-baseline: account for multiple vendor directories and the CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS setting #17227

Merged
Merged
1 change: 1 addition & 0 deletions go/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ codeql_pkg_files(
"//go/extractor/cli/go-autobuilder",
"//go/extractor/cli/go-bootstrap",
"//go/extractor/cli/go-build-runner",
"//go/extractor/cli/go-configure-baseline",
"//go/extractor/cli/go-extractor",
"//go/extractor/cli/go-gen-dbscheme",
"//go/extractor/cli/go-tokenizer",
Expand Down
3 changes: 0 additions & 3 deletions go/codeql-tools/baseline-config-empty.json

This file was deleted.

5 changes: 0 additions & 5 deletions go/codeql-tools/baseline-config-vendor.json

This file was deleted.

8 changes: 3 additions & 5 deletions go/codeql-tools/configure-baseline.cmd
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%
6 changes: 1 addition & 5 deletions go/codeql-tools/configure-baseline.sh
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"
18 changes: 18 additions & 0 deletions go/extractor/cli/go-configure-baseline/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions go/extractor/cli/go-configure-baseline/go-configure-baseline.go
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))
}
}
11 changes: 11 additions & 0 deletions go/extractor/configurebaseline/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions go/extractor/configurebaseline/configurebaseline.go
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)
}
2 changes: 1 addition & 1 deletion go/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {

// If CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS is "true", we extract `vendor` directories;
// otherwise (the default) is to exclude them from extraction
includeVendor := os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true"
includeVendor := util.IsVendorDirExtractionEnabled()
if !includeVendor {
excludedDirs = append(excludedDirs, "vendor")
}
Expand Down
1 change: 1 addition & 0 deletions go/extractor/util/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions go/extractor/util/extractvendordirs.go
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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package abc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package abc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package abc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package abc
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"
4 changes: 4 additions & 0 deletions go/ql/lib/change-notes/2024-08-20-vendor-dirs-baseline.md
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.
Loading