mirror of
https://github.com/golang/go
synced 2024-11-23 11:40:07 -07:00
cmd/go: report position info in package errors
Also refactor common position filling code into a function. Fixes #18011 Change-Id: I76528626da67a7309193fa92af1e361c8e2fcf84 Reviewed-on: https://go-review.googlesource.com/33631 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
b079869dad
commit
3825656e28
@ -1063,14 +1063,14 @@ func TestInternalPackagesInGOROOTAreRespected(t *testing.T) {
|
|||||||
tg := testgo(t)
|
tg := testgo(t)
|
||||||
defer tg.cleanup()
|
defer tg.cleanup()
|
||||||
tg.runFail("build", "-v", "./testdata/testinternal")
|
tg.runFail("build", "-v", "./testdata/testinternal")
|
||||||
tg.grepBoth("use of internal package not allowed", "wrong error message for testdata/testinternal")
|
tg.grepBoth(`testinternal(\/|\\)p\.go\:3\:8\: use of internal package not allowed`, "wrong error message for testdata/testinternal")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInternalPackagesOutsideGOROOTAreRespected(t *testing.T) {
|
func TestInternalPackagesOutsideGOROOTAreRespected(t *testing.T) {
|
||||||
tg := testgo(t)
|
tg := testgo(t)
|
||||||
defer tg.cleanup()
|
defer tg.cleanup()
|
||||||
tg.runFail("build", "-v", "./testdata/testinternal2")
|
tg.runFail("build", "-v", "./testdata/testinternal2")
|
||||||
tg.grepBoth("use of internal package not allowed", "wrote error message for testdata/testinternal2")
|
tg.grepBoth(`testinternal2(\/|\\)p\.go\:3\:8\: use of internal package not allowed`, "wrote error message for testdata/testinternal2")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunInternal(t *testing.T) {
|
func TestRunInternal(t *testing.T) {
|
||||||
@ -1080,7 +1080,7 @@ func TestRunInternal(t *testing.T) {
|
|||||||
tg.setenv("GOPATH", dir)
|
tg.setenv("GOPATH", dir)
|
||||||
tg.run("run", filepath.Join(dir, "src/run/good.go"))
|
tg.run("run", filepath.Join(dir, "src/run/good.go"))
|
||||||
tg.runFail("run", filepath.Join(dir, "src/run/bad.go"))
|
tg.runFail("run", filepath.Join(dir, "src/run/bad.go"))
|
||||||
tg.grepStderr("use of internal package not allowed", "unexpected error for run/bad.go")
|
tg.grepStderr(`testdata(\/|\\)src(\/|\\)run(\/|\\)bad\.go\:3\:8\: use of internal package not allowed`, "unexpected error for run/bad.go")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMove(t *testing.T, vcs, url, base, config string) {
|
func testMove(t *testing.T, vcs, url, base, config string) {
|
||||||
|
@ -371,10 +371,8 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo
|
|||||||
err = fmt.Errorf("code in directory %s expects import %q", bp.Dir, bp.ImportComment)
|
err = fmt.Errorf("code in directory %s expects import %q", bp.Dir, bp.ImportComment)
|
||||||
}
|
}
|
||||||
p.load(stk, bp, err)
|
p.load(stk, bp, err)
|
||||||
if p.Error != nil && p.Error.Pos == "" && len(importPos) > 0 {
|
if p.Error != nil && p.Error.Pos == "" {
|
||||||
pos := importPos[0]
|
p = setErrorPos(p, importPos)
|
||||||
pos.Filename = shortPath(pos.Filename)
|
|
||||||
p.Error.Pos = pos.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if origPath != cleanImport(origPath) {
|
if origPath != cleanImport(origPath) {
|
||||||
@ -388,11 +386,11 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo
|
|||||||
|
|
||||||
// Checked on every import because the rules depend on the code doing the importing.
|
// Checked on every import because the rules depend on the code doing the importing.
|
||||||
if perr := disallowInternal(srcDir, p, stk); perr != p {
|
if perr := disallowInternal(srcDir, p, stk); perr != p {
|
||||||
return perr
|
return setErrorPos(perr, importPos)
|
||||||
}
|
}
|
||||||
if mode&useVendor != 0 {
|
if mode&useVendor != 0 {
|
||||||
if perr := disallowVendor(srcDir, origPath, p, stk); perr != p {
|
if perr := disallowVendor(srcDir, origPath, p, stk); perr != p {
|
||||||
return perr
|
return setErrorPos(perr, importPos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,12 +400,7 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo
|
|||||||
ImportStack: stk.copy(),
|
ImportStack: stk.copy(),
|
||||||
Err: fmt.Sprintf("import %q is a program, not an importable package", path),
|
Err: fmt.Sprintf("import %q is a program, not an importable package", path),
|
||||||
}
|
}
|
||||||
if len(importPos) > 0 {
|
return setErrorPos(&perr, importPos)
|
||||||
pos := importPos[0]
|
|
||||||
pos.Filename = shortPath(pos.Filename)
|
|
||||||
perr.Error.Pos = pos.String()
|
|
||||||
}
|
|
||||||
return &perr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.local && parent != nil && !parent.local {
|
if p.local && parent != nil && !parent.local {
|
||||||
@ -416,17 +409,21 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo
|
|||||||
ImportStack: stk.copy(),
|
ImportStack: stk.copy(),
|
||||||
Err: fmt.Sprintf("local import %q in non-local package", path),
|
Err: fmt.Sprintf("local import %q in non-local package", path),
|
||||||
}
|
}
|
||||||
if len(importPos) > 0 {
|
return setErrorPos(&perr, importPos)
|
||||||
pos := importPos[0]
|
|
||||||
pos.Filename = shortPath(pos.Filename)
|
|
||||||
perr.Error.Pos = pos.String()
|
|
||||||
}
|
|
||||||
return &perr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setErrorPos(p *Package, importPos []token.Position) *Package {
|
||||||
|
if len(importPos) > 0 {
|
||||||
|
pos := importPos[0]
|
||||||
|
pos.Filename = shortPath(pos.Filename)
|
||||||
|
p.Error.Pos = pos.String()
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func cleanImport(path string) string {
|
func cleanImport(path string) string {
|
||||||
orig := path
|
orig := path
|
||||||
path = pathpkg.Clean(path)
|
path = pathpkg.Clean(path)
|
||||||
|
Loading…
Reference in New Issue
Block a user