Skip to content

Commit 72c2b55

Browse files
committed
loader: use name "main" for the main package
This should make exported names a bit more consistent. I believe there was a bug report for this issue, but I can't easily find it. In any case, I think it's an important improvement to match the behavior of the Go toolchain.
1 parent da0161d commit 72c2b55

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

loader/loader.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Program struct {
4343
type PackageJSON struct {
4444
Dir string
4545
ImportPath string
46+
Name string
4647
ForTest string
4748

4849
// Source files
@@ -335,7 +336,16 @@ func (p *Package) Check() error {
335336
// Do typechecking of the package.
336337
checker.Importer = p
337338

338-
typesPkg, err := checker.Check(p.ImportPath, p.program.fset, p.Files, &p.info)
339+
packageName := p.ImportPath
340+
if p.Name == "main" {
341+
// The main package normally has a different import path, such as
342+
// "command-line-arguments" or "./testdata/cgo". Therefore, use the name
343+
// "main" in such a case: this package isn't imported from anywhere.
344+
// This is safe as it isn't possible to import a package with the name
345+
// "main".
346+
packageName = "main"
347+
}
348+
typesPkg, err := checker.Check(packageName, p.program.fset, p.Files, &p.info)
339349
if err != nil {
340350
if err, ok := err.(Errors); ok {
341351
return err

0 commit comments

Comments
 (0)