Skip to content

Commit 7bcc6a1

Browse files
committed
cmd/compile: add -importmap option
The -importmap option takes an argument of the form old=new and specifies that import "old" should be interpreted as if it said import "new". The option may be repeated to specify multiple mappings. This option is here to support the go command's new -vendor flag. Change-Id: I31b4ed4249b549982a720bf61bb230462b33c59b Reviewed-on: https://go-review.googlesource.com/10922 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent f5d494b commit 7bcc6a1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/cmd/compile/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Flags:
5454
Remove the limit on the number of errors reported (default limit is 10).
5555
-h
5656
Halt with a stack trace at the first error detected.
57+
-importmap old=new
58+
Interpret import "old" as import "new" during compilation.
59+
The option may be repeated to add multiple mappings.
5760
-installsuffix suffix
5861
Look for packages in $GOROOT/pkg/$GOOS_$GOARCH_suffix
5962
instead of $GOROOT/pkg/$GOOS_$GOARCH.

src/cmd/compile/internal/gc/lex.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func Main() {
214214
obj.Flagcount("g", "debug code generation", &Debug['g'])
215215
obj.Flagcount("h", "halt on error", &Debug['h'])
216216
obj.Flagcount("i", "debug line number stack", &Debug['i'])
217+
obj.Flagfn1("importmap", "add `definition` of the form source=actual to import map", addImportMap)
217218
obj.Flagstr("installsuffix", "set pkg directory `suffix`", &flag_installsuffix)
218219
obj.Flagcount("j", "debug runtime-initialized variables", &Debug['j'])
219220
obj.Flagcount("l", "disable inlining", &Debug['l'])
@@ -501,6 +502,20 @@ func Main() {
501502
Flusherrors()
502503
}
503504

505+
var importMap = map[string]string{}
506+
507+
func addImportMap(s string) {
508+
if strings.Count(s, "=") != 1 {
509+
log.Fatal("-importmap argument must be of the form source=actual")
510+
}
511+
i := strings.Index(s, "=")
512+
source, actual := s[:i], s[i+1:]
513+
if source == "" || actual == "" {
514+
log.Fatal("-importmap argument must be of the form source=actual; source and actual must be non-empty")
515+
}
516+
importMap[source] = actual
517+
}
518+
504519
func saveerrors() {
505520
nsavederrors += nerrors
506521
nerrors = 0
@@ -687,6 +702,11 @@ func importfile(f *Val, line int) {
687702
}
688703

689704
path_ := f.U.(string)
705+
706+
if mapped, ok := importMap[path_]; ok {
707+
path_ = mapped
708+
}
709+
690710
if islocalname(path_) {
691711
if path_[0] == '/' {
692712
Yyerror("import path cannot be absolute path")

0 commit comments

Comments
 (0)