diff --git a/go/BUILD.bazel b/go/BUILD.bazel index 936c86e0ed13..931f061da9ef 100644 --- a/go/BUILD.bazel +++ b/go/BUILD.bazel @@ -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", diff --git a/go/codeql-tools/baseline-config-empty.json b/go/codeql-tools/baseline-config-empty.json deleted file mode 100644 index 568d688fc3fe..000000000000 --- a/go/codeql-tools/baseline-config-empty.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "paths-ignore": [] -} \ No newline at end of file diff --git a/go/codeql-tools/baseline-config-vendor.json b/go/codeql-tools/baseline-config-vendor.json deleted file mode 100644 index d2f654073b03..000000000000 --- a/go/codeql-tools/baseline-config-vendor.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "paths-ignore": [ - "vendor/**" - ] -} \ No newline at end of file diff --git a/go/codeql-tools/configure-baseline.cmd b/go/codeql-tools/configure-baseline.cmd index 285c3d66829a..47789cfbd3ab 100644 --- a/go/codeql-tools/configure-baseline.cmd +++ b/go/codeql-tools/configure-baseline.cmd @@ -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% diff --git a/go/codeql-tools/configure-baseline.sh b/go/codeql-tools/configure-baseline.sh index f426773c3ba0..20edf8b4c93f 100755 --- a/go/codeql-tools/configure-baseline.sh +++ b/go/codeql-tools/configure-baseline.sh @@ -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" diff --git a/go/extractor/cli/go-configure-baseline/BUILD.bazel b/go/extractor/cli/go-configure-baseline/BUILD.bazel new file mode 100644 index 000000000000..df1af64d6a33 --- /dev/null +++ b/go/extractor/cli/go-configure-baseline/BUILD.bazel @@ -0,0 +1,18 @@ +# generated running `bazel run //go/gazelle`, do not edit + +load("@rules_go//go:def.bzl", "go_library") +load("//go:rules.bzl", "codeql_go_binary") + +go_library( + name = "go-configure-baseline_lib", + srcs = ["go-configure-baseline.go"], + importpath = "github.com/github/codeql-go/extractor/cli/go-configure-baseline", + visibility = ["//visibility:private"], + deps = ["//go/extractor/configurebaseline"], +) + +codeql_go_binary( + name = "go-configure-baseline", + embed = [":go-configure-baseline_lib"], + visibility = ["//visibility:public"], +) diff --git a/go/extractor/cli/go-configure-baseline/go-configure-baseline.go b/go/extractor/cli/go-configure-baseline/go-configure-baseline.go new file mode 100644 index 000000000000..c8159908e0a2 --- /dev/null +++ b/go/extractor/cli/go-configure-baseline/go-configure-baseline.go @@ -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)) + } +} diff --git a/go/extractor/configurebaseline/BUILD.bazel b/go/extractor/configurebaseline/BUILD.bazel new file mode 100644 index 000000000000..b12e89abaf3c --- /dev/null +++ b/go/extractor/configurebaseline/BUILD.bazel @@ -0,0 +1,11 @@ +# generated running `bazel run //go/gazelle`, do not edit + +load("@rules_go//go:def.bzl", "go_library") + +go_library( + name = "configurebaseline", + srcs = ["configurebaseline.go"], + importpath = "github.com/github/codeql-go/extractor/configurebaseline", + visibility = ["//visibility:public"], + deps = ["//go/extractor/util"], +) diff --git a/go/extractor/configurebaseline/configurebaseline.go b/go/extractor/configurebaseline/configurebaseline.go new file mode 100644 index 000000000000..f8e2c998f8c1 --- /dev/null +++ b/go/extractor/configurebaseline/configurebaseline.go @@ -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) +} diff --git a/go/extractor/extractor.go b/go/extractor/extractor.go index df3a43f80cfa..4926d8e3e13a 100644 --- a/go/extractor/extractor.go +++ b/go/extractor/extractor.go @@ -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") } diff --git a/go/extractor/util/BUILD.bazel b/go/extractor/util/BUILD.bazel index d0195e05be23..b7a7783aa799 100644 --- a/go/extractor/util/BUILD.bazel +++ b/go/extractor/util/BUILD.bazel @@ -5,6 +5,7 @@ load("@rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "util", srcs = [ + "extractvendordirs.go", "semver.go", "util.go", ], diff --git a/go/extractor/util/extractvendordirs.go b/go/extractor/util/extractvendordirs.go new file mode 100644 index 000000000000..778d5120cf20 --- /dev/null +++ b/go/extractor/util/extractvendordirs.go @@ -0,0 +1,9 @@ +package util + +import ( + "os" +) + +func IsVendorDirExtractionEnabled() bool { + return os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true" +} diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/src/a/vendor/avendor.go b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/a/vendor/avendor.go new file mode 100644 index 000000000000..6e423a610721 --- /dev/null +++ b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/a/vendor/avendor.go @@ -0,0 +1 @@ +package abc diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/src/a/vendor/modules.txt b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/a/vendor/modules.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/src/b/vendor/bvendor.go b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/b/vendor/bvendor.go new file mode 100644 index 000000000000..6e423a610721 --- /dev/null +++ b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/b/vendor/bvendor.go @@ -0,0 +1 @@ +package abc diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/src/b/vendor/modules.txt b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/b/vendor/modules.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/src/c/vendor/cvendor.go b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/c/vendor/cvendor.go new file mode 100644 index 000000000000..6e423a610721 --- /dev/null +++ b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/c/vendor/cvendor.go @@ -0,0 +1 @@ +package abc diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/src/root.go b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/root.go new file mode 100644 index 000000000000..6e423a610721 --- /dev/null +++ b/go/ql/integration-tests/all-platforms/go/configure-baseline/src/root.go @@ -0,0 +1 @@ +package abc diff --git a/go/ql/integration-tests/all-platforms/go/configure-baseline/test.py b/go/ql/integration-tests/all-platforms/go/configure-baseline/test.py new file mode 100644 index 000000000000..e92cc868cab5 --- /dev/null +++ b/go/ql/integration-tests/all-platforms/go/configure-baseline/test.py @@ -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" diff --git a/go/ql/lib/change-notes/2024-08-20-vendor-dirs-baseline.md b/go/ql/lib/change-notes/2024-08-20-vendor-dirs-baseline.md new file mode 100644 index 000000000000..cab6b49f3baa --- /dev/null +++ b/go/ql/lib/change-notes/2024-08-20-vendor-dirs-baseline.md @@ -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.