Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Resolving Missing Standard Library Archives When Using go tool compile in Go 1.20+

Tech 1

Running go tool compile -S concurrent_check.go on modern Go toolchains may trigger an import resolution failure:

concurrent_check.go:4:8: could not import sync (file not found)

This error ocurs because low-level compiler utilities expect precompiled archive files (.a) for standard library packages, but recent Go distributions no longer ship them by default.

Starting with Go 1.20, official installers omit precompiled standard library archives to reduce distribution size. The toolchain now builds and caches these packages dynamically in the user's local build cache during the first compilation. While high-level commands like go build and go run handle this transparently, direct invocations of go tool compile bypass the build system and search strictly within $GOROOT/pkg/$GOOS_$GOARCH. When the archives are absent, import resolution fails immediately.

To restore the expected directory structure, force the toolchain to compile and install all standard library packages into the legacy package directory:

GODEBUG=installgoroot=all go install -v std

The GODEBUG=installgoroot=all environment variable overrides the default caching behavior, directing the compiler to populate $GOROOT/pkg. After execution, verify that the architecture-specific folder contains the compiled archives:

ls "$GOROOT/pkg/$(go env GOOS)_$(go env GOARCH)" | grep -E '\.a$'

Expected output includes core packages:

sync.a
runtime.a
fmt.a
os.a
net.a
...

With the archives in place, re-run the disassembly command:

go tool compile -S concurrent_check.go

The compiler now successfully resolves standard library dependencies and emits the expected assembly output:

"".main STEXT size=145 args=0x0 locals=0x18 funcid=0x0 align=0x0
	0x0000 00000 (main.go:5)	TEXT	"".main(SB), ABIInternal, $24-0
	0x0000 00000 (main.go:5)	CMPQ	SP, 16(R14)
	0x0004 00004 (main.go:5)	PCDATA	$0, $-2
	0x0004 00004 (main.go:5)	JLS	132
	0x000a 00010 (main.go:5)	PUSHQ	BP
	0x000b 00011 (main.go:5)	MOVQ	SP, BP
	...

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.