Interrupt go test or any other build command using ^C or with kill -SIGINT.
It's easy to reproduce this by setting GOTMPDIR to a local directory and then running a test that sleeps for a few seconds.
What did you expect to see?
No temporary go-build directories are left behind.
What did you see instead?
Directories with names like go-build312263156 are left behind.
go test should trap the signal, delete the work directory, and exit gracefully. It should not trim the build cache or do anything else that might take a long time (which it normally does when exiting).
Fixing this would be non-trivial: we'll need to plumb contexts through to network, build, and test operations to handle the cancellation. We probably want to do that anyway though for tracing.
Cleaning up build directories happens in base.Exit, which is run from the main goroutine. I think that would be mostly unchanged. We'd just get there faster by cancelling anything that takes a long time.