golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.48k stars 17.4k forks source link

cmd/go: subprocess (linker, cgo command) does not use the value of GOTMPDIR variable #59636

Open vault-thirteen opened 1 year ago

vault-thirteen commented 1 year ago

What version of Go are you using (go version)?

1.20.3.

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

Windows 10, Intel x86-64.

What did you do?

My environment variable GOTMPDIR is set to a custom place.

go env GOTMPDIR D:\Temp\_Go_

When I am trying to build something I look at the compiler's output using the -x argument. It looks like, the compiler is using the GOTMPDIR at the frist stage of executable file building, at the compile stage, but at the second stage it uses the default system path instead of the GOTMPDIR variable.

What did you expect to see?

I expect to use the GOTMPDIR variable value for all build stages in Go.

What did you see instead?

It looks like, the compiler is using the GOTMPDIR at the frist stage of executable file building, at the compile stage, but at the second stage it uses the default system path instead of the GOTMPDIR variable.

build.bat
CGO_ENABLED=1
CC=clang
CFLAGS=
CGO_CFLAGS=-fuse-ld=lld
CGO_CFLAGS_ALLOW=-fuse-ld=lld
LDFLAGS=
CGO_LDFLAGS=-lSDL2
WORK=D:\Temp\_Go_\go-build2426032274
cd J:\Go\src\github.com\vault-thirteen\SDLW
git status --porcelain
cd J:\Go\src\github.com\vault-thirteen\SDLW
git -c log.showsignature=false show -s --format=%H:%ct
mkdir -p $WORK\b001\
cat >D:\Temp\_Go_\go-build2426032274\b001\importcfg.link << 'EOF' # internal
packagefile github.com/vault-thirteen/SDLW=J:\Go\cache\52\52542e1c5ce1b7ea5a1f87a8df8b1a4d83ecd8beeae4f5762569349605878012-d
packagefile fmt=J:\Go\cache\6b\6b6754731b574d38b24e90057bc6ceeed681cf4ad4aeac41494c32f5ef2158b5-d
packagefile github.com/vault-thirteen/SDLW/sdl=J:\Go\cache\80\80a0751affd24bac832f9edef1091537779ffed871fb5d99e62dd71eea519a7c-d
packagefile log=J:\Go\cache\da\da5cad94e56043c0a29802c8c775afd35ea4adf386288459dce9348d53d4077c-d
packagefile time=J:\Go\cache\25\25d4036494d51670eb299321c553694e26217a30c66adb7d10bf6faf6dc504bf-d
packagefile runtime=J:\Go\cache\83\8336b2089b6cda64e0eb09bd31812094dea2022de938979926c53c4ce57ceba1-d
packagefile errors=J:\Go\cache\d0\d0a96667916b3dec7cae00b05e3de3219051e41b0dc8aceccee55f2e245d49e3-d
packagefile internal/fmtsort=J:\Go\cache\cb\cbe7337013b7a0fcbe4a172b74246f14dad37a87f1cdcbf23fc61811c8d99410-d
packagefile io=J:\Go\cache\97\975a75fcccb98b67b285ed6803b44aada6698c26c589eaddf56a5e5eb724620d-d
packagefile math=J:\Go\cache\b6\b6103d112c73f5b021001063df8ed869e58125b716363d7c86056c301ef79bec-d
packagefile os=J:\Go\cache\a6\a6e9c50901bfb3eea15292b742a391b17e944f0df99f1be6893b8140d44313d6-d
packagefile reflect=J:\Go\cache\74\746b2174da3b11ea4d4d1fc22e9cea5df1296b444af2b3e20e66cd6fdd0160ec-d
packagefile sort=J:\Go\cache\8e\8eadc0215f01641905a0d5596e815d82831e956c4e26c921ac6203250076b953-d
packagefile strconv=J:\Go\cache\e9\e94ede53509e370796ded4817d1b7b79e0731cebc5067257483a8d20bdc47a14-d
packagefile sync=J:\Go\cache\18\1882116daf2c91d06e82c2398af8920baf00e9de4681f164d0a31acd88b5def4-d
packagefile unicode/utf8=J:\Go\cache\c7\c736af65602fbbe4e2bc6b210bd15012debdab2b56b82ca0f01e4b45a7b9dded-d
packagefile runtime/cgo=J:\Go\cache\b5\b5e0257ad5a182a0469dddc258851028691ed8444ac0503569cdee2e05b7c8f0-d
packagefile syscall=J:\Go\cache\f2\f2cfd2231e126b7b6507ad65ad92917794aef36e85d187254d3cb54ae768e82d-d
packagefile sync/atomic=J:\Go\cache\2b\2b08ecfdd83a5d45ab98fbba49b53d45135d43a5c39e334527457700b4a0c70c-d
packagefile internal/syscall/windows/registry=J:\Go\cache\53\53ecb98f5129a32e8253b208c54076da74eef3f44c177bbabc789abf729117d5-d
packagefile internal/abi=J:\Go\cache\f4\f481a078b5331256da98442e39e3ed871afc71b0f77705f0010fadcc7732dec0-d
packagefile internal/bytealg=J:\Go\cache\98\98a8f14f0340a4451c9a4eeb2d4ad287de9c3fd81b495515c72e50c9a8d8a6c8-d
packagefile internal/coverage/rtcov=J:\Go\cache\d8\d859bd2975a3f4929d836ce96a164dad56df0f382ba0a5155752bbbe34e8ce51-d
packagefile internal/cpu=J:\Go\cache\04\04403fba21c1f28830aede480b93f660db52f61d8abb1401f34071d0deb4ac84-d
packagefile internal/goarch=J:\Go\cache\ab\abe3a9621910da1fa083ddc6d617afb7d1c1ad37a4e2fbd8a9d36286fc0b3a44-d
packagefile internal/goexperiment=J:\Go\cache\a5\a54e6205034dc023181ae592d76a52efa29ebaa41bbbeb29099839ecccbad178-d
packagefile internal/goos=J:\Go\cache\f1\f16c8261a5ae89a7c9d1c4244dffcc9affb2aadfa90940120e537dbf9a8b95b9-d
packagefile runtime/internal/atomic=J:\Go\cache\bf\bf07a986d099eb252a6f896c5e0e70c69b385be6db942dd0eb1d453c48b74314-d
packagefile runtime/internal/math=J:\Go\cache\53\538862994ce03e8879742a530269442e21480d5c978d24de6b31efcc0c482762-d
packagefile runtime/internal/sys=J:\Go\cache\88\8861c8cfcd34026b21defc386ecc0c3e840adc91a147c45e40f24b528c253480-d
packagefile internal/reflectlite=J:\Go\cache\9c\9c6967db7ec4197c8d0bbcbb944784fe6ccfa0738b602ff08ab8e9e787c41a0b-d
packagefile math/bits=J:\Go\cache\55\55aac49c24120089eba90718682ddcfc4b05b46aadb767f8b835b7fe16d274c2-d
packagefile internal/itoa=J:\Go\cache\cf\cf83466007118a2afc64dbeb66801aac38ac84bd08873fc4e88109183b6571bc-d
packagefile internal/poll=J:\Go\cache\c3\c34351e1401a98e8f59e57d5bdb1330c011e14385e902c7094119716ca155450-d
packagefile internal/safefilepath=J:\Go\cache\db\db9aede8329aabbf0656c7126e4bf65fd752bb731212b3a6e73943b9c04e2c7a-d
packagefile internal/syscall/execenv=J:\Go\cache\ad\ad4cab046bd583d854351b7264b94888a46674bb13fda726a1f24b421cac65a2-d
packagefile internal/syscall/windows=J:\Go\cache\fe\fe309e3af517f131396415a0830daffefa14d1f75deff41fed55fde8472751dc-d
packagefile internal/testlog=J:\Go\cache\a6\a6248127f3caa65cd4f20d66358fbdd2863af4101676aca2e31db4ee64d71d8f-d
packagefile io/fs=J:\Go\cache\b1\b1c63c7bef0a9cdf1c47eb169257c33b29948105521fc54fa9a536b7b368e072-d
packagefile unicode/utf16=J:\Go\cache\4c\4cf62bfc325c4f5d9bd04b0283325db234ad67e3532baaa67480774137ebcb2c-d
packagefile internal/unsafeheader=J:\Go\cache\ab\ab7e82ccb5d5bf5031ccbbf75b8766fa6f2803d08c0221779fddfcde2eb92220-d
packagefile unicode=J:\Go\cache\ca\ca17028a7b93fc67aca95b2c2f910b8cf4783d76d6ced282a09172ea5bf07fef-d
packagefile internal/race=J:\Go\cache\ee\eef2c4d9cd1b8d7c62f226f29568816c4d193fd4ca2070943ee997a21ec45e2a-d
packagefile internal/oserror=J:\Go\cache\aa\aaffebc849a6cdbd4b6f17e60ebc889a5403bd7b81ee1a0b78f925dccde6447f-d
packagefile internal/syscall/windows/sysdll=J:\Go\cache\aa\aad12ba42f23e8c355b7782a19c385d2257a0975674ea261eccdb77d25a977e6-d
packagefile path=J:\Go\cache\7a\7a433da405d5a13c9b357afe778222f38ca92969d8d0cc2f1e61e327867c88cb-d
modinfo "0w\xaf\f\x92t\b\x02A\xe1\xc1\a\xe6\xd6\x18\xe6path\tgithub.com/vault-thirteen/SDLW\nmod\tgithub.com/vault-thirteen/SDLW\t(devel)\t\nbuild\t-buildmode=exe\nbuild\t-compiler=gc\nbuild\t-ldflags=-linkmode=external\nbuild\tCGO_ENABLED=1\nbuild\tCGO_CFLAGS=-fuse-ld=lld\nbuild\tCGO_CPPFLAGS=\nbuild\tCGO_CXXFLAGS=\nbuild\tCGO_LDFLAGS=-lSDL2\nbuild\tGOARCH=amd64\nbuild\tGOOS=windows\nbuild\tGOAMD64=v1\nbuild\tvcs=git\nbuild\tvcs.revision=fe4efee1604a46b7457bf8654dd76d44b2a8bc3c\nbuild\tvcs.time=2023-04-12T00:27:18Z\nbuild\tvcs.modified=true\n\xf92C1\x86\x18 r\x00\x82B\x10A\x16\xd8\xf2"
EOF
mkdir -p $WORK\b001\exe\
cd .
"D:\\Program Files\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "$WORK\\b001\\exe\\a.out.exe" -importcfg "$WORK\\b001\\importcfg.link" -buildmode=pie -buildid=KA9A9jLzapdujzPI8KBv/lZ_ScHiLyde-PxEAb1O-/PKVFfbeMvAceyWW3g6Q7/KA9A9jLzapdujzPI8KBv -linkmode=external -extld=clang "J:\\Go\\cache\\52\\52542e1c5ce1b7ea5a1f87a8df8b1a4d83ecd8beeae4f5762569349605878012-d"
# github.com/vault-thirteen/SDLW
D:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running clang failed: exit status 1107
LINK : warning LNK4044: unrecognized option '/-tsaware'; ignored
LINK : warning LNK4044: unrecognized option '/-nxcompat'; ignored
LINK : warning LNK4044: unrecognized option '/-major-os-version=6'; ignored
LINK : warning LNK4044: unrecognized option '/-minor-os-version=1'; ignored
LINK : warning LNK4044: unrecognized option '/-major-subsystem-version=6'; ignored
LINK : warning LNK4044: unrecognized option '/-minor-subsystem-version=1'; ignored
LINK : warning LNK4044: unrecognized option '/-dynamicbase'; ignored
LINK : warning LNK4044: unrecognized option '/-high-entropy-va'; ignored
LINK : warning LNK4044: unrecognized option '/T'; ignored
LINK : warning LNK4044: unrecognized option '/-start-group'; ignored
LINK : warning LNK4044: unrecognized option '/-end-group'; ignored
C:\Users\Username\AppData\Local\Temp\go-link-1223654893\fix_debug_gdb_scripts.ld : fatal error LNK1107: invalid or corrupt file: cannot read at 0x87
clang: error: linker command failed with exit code 1107 (use -v to see invocation)

Here we can see that path in first stage is:

WORK=D:\Temp\_Go_\go-build2426032274
...
cat >D:\Temp\_Go_\go-build2426032274\b001\importcfg.link << 'EOF' # internal

But in the linking stage it is suddenly changed to the default path:

C:\Users\Username\AppData\Local\Temp\go-link-1587058198\fix_debug_gdb_scripts.ld : fatal error LNK1107: invalid or corrupt file: cannot read at 0x87
clang: error: linker command failed with exit code 1107 (use -v to see invocation)
ianlancetaylor commented 1 year ago

CC @thanm @cherrymui @bcmills

If we want to be fully consistent, I wonder whether cmd/go should set TMPDIR=$GOTMPDIR before invoking any subprocess. For example, cmd/cgo also uses os.CreateTemp which will currently use TMPDIR rather than GOTMPDIR.

cherrymui commented 1 year ago

Agreed with @ianlancetaylor that cmd/go is probably a better place to do the setting. One consequence is that it will also affect external commands invoked by Go tools (e.g. the C compiler, C linker, ar command, etc. invoked by the Go linker or cgo command, which may also fix (work around) #59026).

Updated the issue title to cmd/go tentatively.