Skip to content

Commit 286a56c

Browse files
authored
Merge pull request github#12450 from owen-mc/unexpected-directory-layout
Diagnostic for imports with relative package paths
2 parents 52e4076 + 674799a commit 286a56c

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

go/extractor/cli/go-extractor/go-extractor.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/github/codeql-go/extractor"
12+
"github.com/github/codeql-go/extractor/diagnostics"
1213
)
1314

1415
var cpuprofile, memprofile string
@@ -115,7 +116,12 @@ func main() {
115116
log.Printf("Build flags: '%s'; patterns: '%s'\n", strings.Join(buildFlags, " "), strings.Join(patterns, " "))
116117
err := extractor.ExtractWithFlags(buildFlags, patterns)
117118
if err != nil {
118-
log.Fatalf("Error running go tooling: %s\n", err.Error())
119+
errString := err.Error()
120+
if strings.Contains(errString, "unexpected directory layout:") {
121+
diagnostics.EmitRelativeImportPaths()
122+
}
123+
124+
log.Fatalf("Error running go tooling: %s\n", errString)
119125
}
120126

121127
if memprofile != "" {

go/extractor/diagnostics/diagnostics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,14 @@ func EmitGoFilesFoundButNotProcessed() {
181181
noLocation,
182182
)
183183
}
184+
185+
func EmitRelativeImportPaths() {
186+
emitDiagnostic(
187+
"go/autobuilder/relative-import-paths",
188+
"Some imports use unsupported relative package paths",
189+
"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).",
190+
severityError,
191+
fullVisibility,
192+
noLocation,
193+
)
194+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"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).",
3+
"severity": "error",
4+
"source": {
5+
"extractorName": "go",
6+
"id": "go/autobuilder/relative-import-paths",
7+
"name": "Some imports use unsupported relative package paths"
8+
},
9+
"visibility": {
10+
"cliSummaryTable": true,
11+
"statusPage": true,
12+
"telemetry": true
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import sys
2+
3+
from create_database_utils import *
4+
from diagnostics_test_utils import *
5+
6+
os.environ['GITHUB_REPOSITORY'] = "a/b"
7+
run_codeql_database_create([], lang="go", source="work", db=None, runFunction=runUnsuccessfully)
8+
9+
check_diagnostics()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"./subpkg"
5+
)
6+
7+
func main() {
8+
subpkg.F()
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package subpkg
2+
3+
import "fmt"
4+
5+
func F() {
6+
// It is required that there is an import in this package, which is import by another package using a local path
7+
fmt.Println("subpkg.F")
8+
}

0 commit comments

Comments
 (0)