diff --git a/ir/ir.go b/ir/ir.go index fc5ed66a..abbb403c 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -67,7 +67,7 @@ func NewProgram(lprogram *loader.Program) *Program { program := lprogram.LoadSSA() program.Build() - mainPkg := program.ImportedPackage(lprogram.MainPkg().ImportPath) + mainPkg := program.Package(lprogram.MainPkg().Pkg) if mainPkg == nil { panic("could not find main package") } @@ -79,7 +79,7 @@ func NewProgram(lprogram *loader.Program) *Program { } for _, pkg := range lprogram.Sorted() { - p.AddPackage(program.ImportedPackage(pkg.ImportPath)) + p.AddPackage(program.Package(pkg.Pkg)) } return p diff --git a/loader/loader.go b/loader/loader.go index 847acc25..d25b08a9 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -43,6 +43,7 @@ type Program struct { type PackageJSON struct { Dir string ImportPath string + Name string ForTest string // Source files @@ -335,7 +336,16 @@ func (p *Package) Check() error { // Do typechecking of the package. checker.Importer = p - typesPkg, err := checker.Check(p.ImportPath, p.program.fset, p.Files, &p.info) + packageName := p.ImportPath + if p.Name == "main" { + // The main package normally has a different import path, such as + // "command-line-arguments" or "./testdata/cgo". Therefore, use the name + // "main" in such a case: this package isn't imported from anywhere. + // This is safe as it isn't possible to import a package with the name + // "main". + packageName = "main" + } + typesPkg, err := checker.Check(packageName, p.program.fset, p.Files, &p.info) if err != nil { if err, ok := err.(Errors); ok { return err