Skip to content

Commit 71f9dbb

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: emit error message on mismatch import path
Fixes #54542 Change-Id: I16cfb84fc54892923106d0a6f0b3ba810886d077 Reviewed-on: https://go-review.googlesource.com/c/go/+/596396 Auto-Submit: Cuong Manh Le <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 148755a commit 71f9dbb

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/cmd/compile/internal/noder/unified.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package noder
77
import (
88
"fmt"
99
"internal/pkgbits"
10+
"internal/types/errors"
1011
"io"
1112
"runtime"
1213
"sort"
@@ -403,7 +404,10 @@ func readPackage(pr *pkgReader, importpkg *types.Pkg, localStub bool) {
403404
r := pr.newReader(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
404405

405406
pkg := r.pkg()
406-
base.Assertf(pkg == importpkg, "have package %q (%p), want package %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg)
407+
if pkg != importpkg {
408+
base.ErrorfAt(base.AutogeneratedPos, errors.BadImportPath, "mismatched import path, have %q (%p), want %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg)
409+
base.ErrorExit()
410+
}
407411

408412
r.Bool() // TODO(mdempsky): Remove; was "has init"
409413

test/fixedbugs/issue54542.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// run
2+
3+
//go:build !js && !wasip1
4+
5+
// Copyright 2024 The Go Authors. All rights reserved.
6+
// Use of this source code is governed by a BSD-style
7+
// license that can be found in the LICENSE file.
8+
9+
package main
10+
11+
import (
12+
"bytes"
13+
"fmt"
14+
"os"
15+
"os/exec"
16+
"path/filepath"
17+
)
18+
19+
const aSrc = `package a
20+
21+
func A() { println("a") }
22+
`
23+
24+
const mainSrc = `package main
25+
26+
import "a"
27+
28+
func main() { a.A() }
29+
`
30+
31+
var srcs = map[string]string{
32+
"a.go": aSrc,
33+
"main.go": mainSrc,
34+
}
35+
36+
func main() {
37+
dir, err := os.MkdirTemp("", "issue54542")
38+
if err != nil {
39+
panic(err)
40+
}
41+
defer os.RemoveAll(dir)
42+
43+
for fn, src := range srcs {
44+
if err := os.WriteFile(filepath.Join(dir, fn), []byte(src), 0644); err != nil {
45+
panic(err)
46+
}
47+
}
48+
49+
if _, err := runInDir(dir, "tool", "compile", "-p=lie", "a.go"); err != nil {
50+
panic(err)
51+
}
52+
53+
out, err := runInDir(dir, "tool", "compile", "-I=.", "-p=main", "main.go")
54+
if err == nil {
55+
panic("compiling succeed unexpectedly")
56+
}
57+
58+
if bytes.Contains(out, []byte("internal compiler error:")) {
59+
panic(fmt.Sprintf("unexpected ICE:\n%s", string(out)))
60+
}
61+
}
62+
63+
func runInDir(dir string, args ...string) ([]byte, error) {
64+
cmd := exec.Command("go", args...)
65+
cmd.Dir = dir
66+
return cmd.CombinedOutput()
67+
}

0 commit comments

Comments
 (0)