| 1 |
From: Nicolas Peugnet <[email protected]> |
| 2 |
Date: Sun, 16 Mar 2025 13:48:57 +0100 |
| 3 |
Subject: Fix loading packages with Go modules disabled |
| 4 |
Forwarded: https://github.com/kubernetes/gengo/pull/291 |
| 5 |
|
| 6 |
This allows the parser to load packages even when Go modules are |
| 7 |
disabled, and when GOPATH mode is used. |
| 8 |
This is especially useful in Debian as we build Go packages in GOPATH |
| 9 |
mode, and the tests are also run in this mode. |
| 10 |
--- |
| 11 |
v2/parser/parse.go | 10 +++++++++- |
| 12 |
1 file changed, 9 insertions(+), 1 deletion(-) |
| 13 |
|
| 14 |
diff --git a/v2/parser/parse.go b/v2/parser/parse.go |
| 15 |
index 4c1efa0..01db6b4 100644 |
| 16 |
--- a/v2/parser/parse.go |
| 17 |
+++ b/v2/parser/parse.go |
| 18 |
@@ -20,6 +20,7 @@ import ( |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"go/ast" |
| 22 |
+ "go/build" |
| 23 |
"go/constant" |
| 24 |
"go/token" |
| 25 |
gotypes "go/types" |
| 26 |
@@ -129,7 +130,14 @@ func (p *Parser) findPackages(baseCfg *packages.Config, patterns ...string) ([]s |
| 27 |
} |
| 28 |
var allErrs []error |
| 29 |
for _, pkg := range pkgs { |
| 30 |
- results = append(results, pkg.PkgPath) |
| 31 |
+ pkgPath := pkg.PkgPath |
| 32 |
+ if pkgPath[0] == '_' && strings.HasPrefix(pkgPath[1:], build.Default.GOPATH) { |
| 33 |
+ // If GOPATH mode is used, the PkgPath returned by packages.Load |
| 34 |
+ // will be of the form "_<GOPATH>/src/<IMPORT_PATH>". We thus |
| 35 |
+ // reslice the string to only get the <IMPORT_PATH> part. |
| 36 |
+ pkgPath = pkg.PkgPath[len(build.Default.GOPATH)+6:] |
| 37 |
+ } |
| 38 |
+ results = append(results, pkgPath) |
| 39 |
|
| 40 |
// pkg.Errors is not a slice of `error`, but concrete types. We have |
| 41 |
// to iteratively convert each one into `error`. |