@@ -151,17 +151,52 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
151
151
event .Log (ctx , eventName , labels ... )
152
152
}
153
153
154
+ if standalone {
155
+ // Handle standalone package result.
156
+ //
157
+ // In general, this should just be a single "command-line-arguments"
158
+ // package containing the requested file. However, if the file is a test
159
+ // file, go/packages may return test variants of the command-line-arguments
160
+ // package. We don't support this; theoretically we could, but it seems
161
+ // unnecessarily complicated.
162
+ //
163
+ // Prior to golang/go#64233 we just assumed that we'd get exactly one
164
+ // package here. The categorization of bug reports below may be a bit
165
+ // verbose, but anticipates that perhaps we don't fully understand
166
+ // possible failure modes.
167
+ errorf := bug .Errorf
168
+ if s .view .typ == GoPackagesDriverView {
169
+ errorf = fmt .Errorf // all bets are off
170
+ }
171
+
172
+ var standalonePkg * packages.Package
173
+ for _ , pkg := range pkgs {
174
+ if pkg .ID == "command-line-arguments" {
175
+ if standalonePkg != nil {
176
+ return errorf ("internal error: go/packages returned multiple standalone packages" )
177
+ }
178
+ standalonePkg = pkg
179
+ } else if packagesinternal .GetForTest (pkg ) == "" && ! strings .HasSuffix (pkg .ID , ".test" ) {
180
+ return errorf ("internal error: go/packages returned unexpected package %q for standalone file" , pkg .ID )
181
+ }
182
+ }
183
+ if standalonePkg == nil {
184
+ return errorf ("internal error: go/packages failed to return non-test standalone package" )
185
+ }
186
+ if len (standalonePkg .CompiledGoFiles ) > 0 {
187
+ pkgs = []* packages.Package {standalonePkg }
188
+ } else {
189
+ pkgs = nil
190
+ }
191
+ }
192
+
154
193
if len (pkgs ) == 0 {
155
194
if err == nil {
156
195
err = errNoPackages
157
196
}
158
197
return fmt .Errorf ("packages.Load error: %w" , err )
159
198
}
160
199
161
- if standalone && len (pkgs ) > 1 {
162
- return bug .Errorf ("internal error: go/packages returned multiple packages for standalone file" )
163
- }
164
-
165
200
moduleErrs := make (map [string ][]packages.Error ) // module path -> errors
166
201
filterFunc := s .view .filterFunc ()
167
202
newMetadata := make (map [PackageID ]* metadata.Package )
0 commit comments