Skip to content

Commit 05cc8b5

Browse files
Bryan C. Millsgopherbot
authored andcommitted
go/build: omit PkgObj for packages "unsafe" and "builtin"
Package "builtin" is not a real, importable package; it exists only for documentation. Package "unsafe" is not compiled into an object file from its source code; instead, imports of "unsafe" are handled specially by the compiler. (In Go 1.19.3, package "unsafe" did not have an install target, while package "builtin" did but that target was never written.) Fixes #56687. Updates #47257. Change-Id: I1d1e90ff9e1629b80e0df93e1f7e17242c8dab69 Reviewed-on: https://go-review.googlesource.com/c/go/+/449376 Reviewed-by: Michael Matloob <matloob@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
1 parent b820fb8 commit 05cc8b5

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/cmd/go/internal/modindex/read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b
436436
p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
437437

438438
// Set the install target if applicable.
439-
if strings.ToLower(godebug.Get("installgoroot")) == "all" || !p.Goroot {
439+
if !p.Goroot || (strings.EqualFold(godebug.Get("installgoroot"), "all") && p.ImportPath != "unsafe" && p.ImportPath != "builtin") {
440440
p.PkgObj = ctxt.joinPath(p.Root, pkga)
441441
}
442442
}

src/cmd/go/testdata/script/install_goroot_targets.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[short] skip
2-
[!cgo] skip
32

43
# Most packages in std do not have an install target.
54
go list -f '{{.Target}}' fmt
@@ -8,18 +7,23 @@ go list -export -f '{{.Export}}' fmt
87
stdout $GOCACHE
98

109
# Packages that use cgo still do.
11-
go list -f '{{.Target}}' runtime/cgo
12-
stdout .
13-
go list -export -f '{{.Export}}' runtime/cgo
14-
! stdout $GOCACHE
15-
stdout cgo\.a
10+
[cgo] go list -f '{{.Target}}' runtime/cgo
11+
[cgo] stdout .
12+
[cgo] go list -export -f '{{.Export}}' runtime/cgo
13+
[cgo] ! stdout $GOCACHE
14+
[cgo] stdout cgo\.a
1615

1716
# With GODEBUG=installgoroot=all, fmt has a target.
1817
# (Though we can't try installing it without modifying goroot).
1918
env GODEBUG=installgoroot=all
2019
go list -f '{{.Target}}' fmt
2120
stdout fmt\.a
2221

22+
# However, the fake packages "builtin" and "unsafe" do not.
23+
go list -f '{{.Target}}' builtin unsafe
24+
! stdout .
25+
go install builtin unsafe # Should succeed as no-ops.
26+
2327
# With CGO_ENABLED=0, packages that would have
2428
# an install target with cgo on no longer do.
2529
env GODEBUG=

src/go/build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ Found:
783783
p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
784784

785785
// Set the install target if applicable.
786-
if strings.ToLower(godebug.Get("installgoroot")) == "all" || !p.Goroot {
786+
if !p.Goroot || (strings.EqualFold(godebug.Get("installgoroot"), "all") && p.ImportPath != "unsafe" && p.ImportPath != "builtin") {
787787
p.PkgObj = ctxt.joinPath(p.Root, pkga)
788788
}
789789
}

0 commit comments

Comments
 (0)