Merge pull request #12450 from owen-mc/unexpected-directory-layout

Diagnostic for imports with relative package paths
This commit is contained in:
Owen Mansel-Chan
2023-03-10 14:35:02 +00:00
committed by GitHub
6 changed files with 58 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import (
"strings"
"github.com/github/codeql-go/extractor"
"github.com/github/codeql-go/extractor/diagnostics"
)
var cpuprofile, memprofile string
@@ -115,7 +116,12 @@ func main() {
log.Printf("Build flags: '%s'; patterns: '%s'\n", strings.Join(buildFlags, " "), strings.Join(patterns, " "))
err := extractor.ExtractWithFlags(buildFlags, patterns)
if err != nil {
log.Fatalf("Error running go tooling: %s\n", err.Error())
errString := err.Error()
if strings.Contains(errString, "unexpected directory layout:") {
diagnostics.EmitRelativeImportPaths()
}
log.Fatalf("Error running go tooling: %s\n", errString)
}
if memprofile != "" {

View File

@@ -181,3 +181,14 @@ func EmitGoFilesFoundButNotProcessed() {
noLocation,
)
}
func EmitRelativeImportPaths() {
emitDiagnostic(
"go/autobuilder/relative-import-paths",
"Some imports use unsupported relative package paths",
"You should replace relative package paths (that contain `.` or `..`) with absolute paths. Alternatively you can [use a Go module](https://go.dev/blog/using-go-modules).",
severityError,
fullVisibility,
noLocation,
)
}

View File

@@ -0,0 +1,14 @@
{
"markdownMessage": "You should replace relative package paths (that contain `.` or `..`) with absolute paths. Alternatively you can [use a Go module](https://go.dev/blog/using-go-modules).",
"severity": "error",
"source": {
"extractorName": "go",
"id": "go/autobuilder/relative-import-paths",
"name": "Some imports use unsupported relative package paths"
},
"visibility": {
"cliSummaryTable": true,
"statusPage": true,
"telemetry": true
}
}

View File

@@ -0,0 +1,9 @@
import sys
from create_database_utils import *
from diagnostics_test_utils import *
os.environ['GITHUB_REPOSITORY'] = "a/b"
run_codeql_database_create([], lang="go", source="work", db=None, runFunction=runUnsuccessfully)
check_diagnostics()

View File

@@ -0,0 +1,9 @@
package main
import (
"./subpkg"
)
func main() {
subpkg.F()
}

View File

@@ -0,0 +1,8 @@
package subpkg
import "fmt"
func F() {
// It is required that there is an import in this package, which is import by another package using a local path
fmt.Println("subpkg.F")
}