aboutsummaryrefslogtreecommitdiff
path: root/internal/fetchdatasource/fetchdatasource.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-08-26 06:36:22 -0400
committerJonathan Amsterdam <jba@google.com>2021-08-26 13:55:16 +0000
commitf6f5076d4026eb90c4e6603f2b56b86402fc0bfb (patch)
treed391fbb14ce7daeab1fdac6b5d009d0ea3b2fd07 /internal/fetchdatasource/fetchdatasource.go
parent0eeccf082fa68b8e061bdd376a923e04d6073d0f (diff)
downloadgo-x-pkgsite-f6f5076d4026eb90c4e6603f2b56b86402fc0bfb.tar.xz
internal/fetchdatasource: handle build contexts properly
GetUnit returns a Unit with its Documentation set to a matching BuildContext. This is the same behavior as postgres.DB.GetUnit. For golang/go#47780 Change-Id: I2bc23b7bc5a006e78bec54f6f3229e59ab5a03ef Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/345269 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org> Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/fetchdatasource/fetchdatasource.go')
-rw-r--r--internal/fetchdatasource/fetchdatasource.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/internal/fetchdatasource/fetchdatasource.go b/internal/fetchdatasource/fetchdatasource.go
index fe2eacca..30115a4e 100644
--- a/internal/fetchdatasource/fetchdatasource.go
+++ b/internal/fetchdatasource/fetchdatasource.go
@@ -199,10 +199,20 @@ func (ds *FetchDataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, f
if err != nil {
return nil, err
}
- if u := findUnit(m, um.Path); u != nil {
- return u, nil
+ u := findUnit(m, um.Path)
+ if u == nil {
+ return nil, fmt.Errorf("import path %s not found in module %s: %w", um.Path, um.ModulePath, derrors.NotFound)
+ }
+ // Return only the Documentation matching the given BuildContext, if any.
+ // Since we cache the module and its units, we have to copy this unit before we modify it.
+ // It can be a shallow copy, since we're only modifying the Unit.Documentation field.
+ u2 := *u
+ if d := matchingDoc(u.Documentation, bc); d != nil {
+ u2.Documentation = []*internal.Documentation{d}
+ } else {
+ u2.Documentation = nil
}
- return nil, fmt.Errorf("import path %s not found in module %s: %w", um.Path, um.ModulePath, derrors.NotFound)
+ return &u2, nil
}
// findUnit returns the unit with the given path in m, or nil if none.
@@ -215,6 +225,23 @@ func findUnit(m *internal.Module, path string) *internal.Unit {
return nil
}
+// matchingDoc returns the Documentation that matches the given build context
+// and comes earliest in build-context order. It returns nil if there is none.
+func matchingDoc(docs []*internal.Documentation, bc internal.BuildContext) *internal.Documentation {
+ var (
+ dMin *internal.Documentation
+ bcMin = internal.BuildContext{GOOS: "unk", GOARCH: "unk"} // sorts last
+ )
+ for _, d := range docs {
+ dbc := d.BuildContext()
+ if bc.Match(dbc) && internal.CompareBuildContexts(dbc, bcMin) < 0 {
+ dMin = d
+ bcMin = dbc
+ }
+ }
+ return dMin
+}
+
// GetLatestInfo returns latest information for unitPath and modulePath.
func (ds *FetchDataSource) GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *internal.UnitMeta) (latest internal.LatestInfo, err error) {
defer derrors.Wrap(&err, "FetchDataSource.GetLatestInfo(ctx, %q, %q)", unitPath, modulePath)