@@ -24,9 +24,11 @@ import (
24
24
"os"
25
25
"strconv"
26
26
"strings"
27
+
28
+ "golang.org/x/tools/go/packages"
27
29
)
28
30
29
- type ResolvePkgFunc func (importPath string ) string
31
+ type ResolvePkgFunc func (importPath string ) * packages. Package
30
32
31
33
// Copy and pasted from golang.org/x/tools/go/packages
32
34
type FlatPackagesError struct {
@@ -97,30 +99,30 @@ func WalkFlatPackagesFromJSON(jsonFile string, onPkg PackageFunc) error {
97
99
return nil
98
100
}
99
101
100
- func ( fp * FlatPackage ) ResolvePaths ( prf PathResolverFunc ) error {
101
- resolvePathsInPlace (prf , fp .CompiledGoFiles )
102
- resolvePathsInPlace (prf , fp .GoFiles )
103
- resolvePathsInPlace (prf , fp .OtherFiles )
104
- fp .ExportFile = prf (fp .ExportFile )
102
+ func ResolvePaths ( pak * packages. Package , prf PathResolverFunc ) error {
103
+ resolvePathsInPlace (prf , pak .CompiledGoFiles )
104
+ resolvePathsInPlace (prf , pak .GoFiles )
105
+ resolvePathsInPlace (prf , pak .OtherFiles )
106
+ pak .ExportFile = prf (pak .ExportFile )
105
107
return nil
106
108
}
107
109
108
110
// FilterFilesForBuildTags filters the source files given the current build
109
111
// tags.
110
- func ( fp * FlatPackage ) FilterFilesForBuildTags ( ) {
111
- fp .GoFiles = filterSourceFilesForTags (fp .GoFiles )
112
- fp .CompiledGoFiles = filterSourceFilesForTags (fp .CompiledGoFiles )
112
+ func FilterFilesForBuildTags ( pak * packages. Package ) {
113
+ pak .GoFiles = filterSourceFilesForTags (pak .GoFiles )
114
+ pak .CompiledGoFiles = filterSourceFilesForTags (pak .CompiledGoFiles )
113
115
}
114
116
115
- func ( fp * FlatPackage ) filterTestSuffix ( files []string ) (err error , testFiles []string , xTestFiles , nonTestFiles []string ) {
117
+ func filterTestSuffix ( pkg * packages. Package , files []string ) (err error , testFiles []string , xTestFiles , nonTestFiles []string ) {
116
118
for _ , filename := range files {
117
119
if strings .HasSuffix (filename , "_test.go" ) {
118
120
fset := token .NewFileSet ()
119
121
f , err := parser .ParseFile (fset , filename , nil , parser .PackageClauseOnly )
120
122
if err != nil {
121
123
return err , nil , nil , nil
122
124
}
123
- if f .Name .Name == fp .Name {
125
+ if f .Name .Name == pkg .Name {
124
126
testFiles = append (testFiles , filename )
125
127
} else {
126
128
xTestFiles = append (xTestFiles , filename )
@@ -132,44 +134,45 @@ func (fp *FlatPackage) filterTestSuffix(files []string) (err error, testFiles []
132
134
return
133
135
}
134
136
135
- func ( fp * FlatPackage ) MoveTestFiles () * FlatPackage {
136
- err , tgf , xtgf , gf := fp . filterTestSuffix (fp .GoFiles )
137
+ func MoveTestFiles ( pkg * packages. Package ) * packages. Package {
138
+ err , tgf , xtgf , gf := filterTestSuffix (pkg , pkg .GoFiles )
137
139
if err != nil {
138
140
return nil
139
141
}
140
142
141
- fp .GoFiles = append (gf , tgf ... )
143
+ pkg .GoFiles = append (gf , tgf ... )
142
144
143
- err , ctgf , cxtgf , cgf := fp . filterTestSuffix (fp .CompiledGoFiles )
145
+ err , ctgf , cxtgf , cgf := filterTestSuffix (pkg , pkg .CompiledGoFiles )
144
146
if err != nil {
145
147
return nil
146
148
}
147
149
148
- fp .CompiledGoFiles = append (cgf , ctgf ... )
150
+ pkg .CompiledGoFiles = append (cgf , ctgf ... )
149
151
150
152
if len (xtgf ) == 0 && len (cxtgf ) == 0 {
151
153
return nil
152
154
}
153
155
154
- newImports := make (map [string ]string , len (fp .Imports ))
155
- for k , v := range fp .Imports {
156
+ newImports := make (map [string ]* packages. Package , len (pkg .Imports ))
157
+ for k , v := range pkg .Imports {
156
158
newImports [k ] = v
157
159
}
158
160
159
- newImports [fp .PkgPath ] = fp .ID
161
+ newImports [pkg .PkgPath ] = & packages.Package {
162
+ ID : pkg .ID ,
163
+ }
160
164
161
165
// Clone package, only xtgf files
162
- return & FlatPackage {
163
- ID : fp .ID + "_xtest" ,
164
- Name : fp .Name + "_test" ,
165
- PkgPath : fp .PkgPath + "_test" ,
166
+ return & packages. Package {
167
+ ID : pkg .ID + "_xtest" ,
168
+ Name : pkg .Name + "_test" ,
169
+ PkgPath : pkg .PkgPath + "_test" ,
166
170
Imports : newImports ,
167
- Errors : fp .Errors ,
171
+ Errors : pkg .Errors ,
168
172
GoFiles : append ([]string {}, xtgf ... ),
169
173
CompiledGoFiles : append ([]string {}, cxtgf ... ),
170
- OtherFiles : fp .OtherFiles ,
171
- ExportFile : fp .ExportFile ,
172
- Standard : fp .Standard ,
174
+ OtherFiles : pkg .OtherFiles ,
175
+ ExportFile : pkg .ExportFile ,
173
176
}
174
177
}
175
178
@@ -179,15 +182,10 @@ func (fp *FlatPackage) IsStdlib() bool {
179
182
180
183
// ResolveImports resolves imports for non-stdlib packages and integrates file overlays
181
184
// to allow modification of package imports without modifying disk files.
182
- func (fp * FlatPackage ) ResolveImports (resolve ResolvePkgFunc , overlays map [string ][]byte ) error {
183
- // Stdlib packages are already complete import wise
184
- if fp .IsStdlib () {
185
- return nil
186
- }
187
-
185
+ func ResolveImports (pkg * packages.Package , resolve ResolvePkgFunc , overlays map [string ][]byte ) error {
188
186
fset := token .NewFileSet ()
189
187
190
- for _ , file := range fp .CompiledGoFiles {
188
+ for _ , file := range pkg .CompiledGoFiles {
191
189
// Only assign overlayContent when an overlay for the file exists, since ParseFile checks by type.
192
190
// If overlay is assigned directly from the map, it will have []byte as type
193
191
// Empty []byte types are parsed into io.EOF
@@ -200,8 +198,8 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin
200
198
return err
201
199
}
202
200
// If the name is not provided, fetch it from the sources
203
- if fp .Name == "" {
204
- fp .Name = f .Name .Name
201
+ if pkg .Name == "" {
202
+ pkg .Name = f .Name .Name
205
203
}
206
204
207
205
for _ , rawImport := range f .Imports {
@@ -213,12 +211,12 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin
213
211
if imp == "C" {
214
212
continue
215
213
}
216
- if _ , ok := fp .Imports [imp ]; ok {
214
+ if _ , ok := pkg .Imports [imp ]; ok {
217
215
continue
218
216
}
219
217
220
- if pkgID := resolve (imp ); pkgID != "" {
221
- fp .Imports [imp ] = pkgID
218
+ if impPkg := resolve (imp ); impPkg != nil {
219
+ pkg .Imports [imp ] = impPkg
222
220
}
223
221
}
224
222
}
0 commit comments