-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go/internal/modload: rework import resolution
modload.Import previously performed two otherwise-separable tasks: 1. Identify which module in the build list contains the requested package. 2. If no such module exists, search available modules to try to find the missing package. This change splits those two tasks into two separate unexported functions, and reports import-resolution errors by attaching them to the package rather than emitting them directly to stderr. That allows 'list' to report the errors, but 'list -e' to ignore them. With the two tasks now separate, it will be easier to avoid the overhead of resolving missing packages during lazy loading if we discover that some existing dependency needs to be promoted to the top level (potentially altering the main module's selected versions, and thus suppling packages that were previously missing). For #36460 Updates #26909 Change-Id: I32bd853b266d7cd231d1f45f92b0650d95c4bcbd Reviewed-on: https://go-review.googlesource.com/c/go/+/251445 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
- Loading branch information
Bryan C. Mills
committed
Sep 9, 2020
1 parent
26d27f9
commit 015a5a5
Showing
8 changed files
with
158 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,39 +10,52 @@ import ( | |
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/mod/module" | ||
) | ||
|
||
var importTests = []struct { | ||
path string | ||
m module.Version | ||
err string | ||
}{ | ||
{ | ||
path: "golang.org/x/net/context", | ||
err: "missing module for import: golang.org/x/net@.* provides golang.org/x/net/context", | ||
m: module.Version{ | ||
Path: "golang.org/x/net", | ||
}, | ||
}, | ||
{ | ||
path: "golang.org/x/net", | ||
err: `module golang.org/x/net@.* found \(v0.0.0-.*\), but does not contain package golang.org/x/net`, | ||
}, | ||
{ | ||
path: "golang.org/x/text", | ||
err: "missing module for import: golang.org/x/text@.* provides golang.org/x/text", | ||
m: module.Version{ | ||
Path: "golang.org/x/text", | ||
}, | ||
}, | ||
{ | ||
path: "github.com/rsc/quote/buggy", | ||
err: "missing module for import: github.com/rsc/[email protected] provides github.com/rsc/quote/buggy", | ||
m: module.Version{ | ||
Path: "github.com/rsc/quote", | ||
Version: "v1.5.2", | ||
}, | ||
}, | ||
{ | ||
path: "github.com/rsc/quote", | ||
err: "missing module for import: github.com/rsc/[email protected] provides github.com/rsc/quote", | ||
m: module.Version{ | ||
Path: "github.com/rsc/quote", | ||
Version: "v1.5.2", | ||
}, | ||
}, | ||
{ | ||
path: "golang.org/x/foo/bar", | ||
err: "cannot find module providing package golang.org/x/foo/bar", | ||
}, | ||
} | ||
|
||
func TestImport(t *testing.T) { | ||
func TestQueryImport(t *testing.T) { | ||
testenv.MustHaveExternalNetwork(t) | ||
testenv.MustHaveExecPath(t, "git") | ||
defer func(old bool) { | ||
|
@@ -55,12 +68,23 @@ func TestImport(t *testing.T) { | |
for _, tt := range importTests { | ||
t.Run(strings.ReplaceAll(tt.path, "/", "_"), func(t *testing.T) { | ||
// Note that there is no build list, so Import should always fail. | ||
m, dir, err := Import(ctx, tt.path) | ||
if err == nil { | ||
t.Fatalf("Import(%q) = %v, %v, nil; expected error", tt.path, m, dir) | ||
m, err := queryImport(ctx, tt.path) | ||
|
||
if tt.err == "" { | ||
if err != nil { | ||
t.Fatalf("queryImport(_, %q): %v", tt.path, err) | ||
} | ||
} else { | ||
if err == nil { | ||
t.Fatalf("queryImport(_, %q) = %v, nil; expected error", tt.path, m) | ||
} | ||
if !regexp.MustCompile(tt.err).MatchString(err.Error()) { | ||
t.Fatalf("queryImport(_, %q): error %q, want error matching %#q", tt.path, err, tt.err) | ||
} | ||
} | ||
if !regexp.MustCompile(tt.err).MatchString(err.Error()) { | ||
t.Fatalf("Import(%q): error %q, want error matching %#q", tt.path, err, tt.err) | ||
|
||
if m.Path != tt.m.Path || (tt.m.Version != "" && m.Version != tt.m.Version) { | ||
t.Errorf("queryImport(_, %q) = %v, _; want %v", tt.path, m, tt.m) | ||
} | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.