cmd/go/internal/clean: print all removals

Print only the removals that would actually happen,
and print them for all removals.

Fixes #33573

Change-Id: Ic73e5e73329dd5dcc111696e9732e7e36a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/768461
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Sean Liao 2026-04-17 19:51:34 +01:00
parent 96db4cf31f
commit b8246db0c3

View file

@ -322,25 +322,21 @@ func clean(p *load.Package) {
}
_, elem := filepath.Split(p.Dir)
var allRemove []string
toRemove := map[string]bool{}
// Remove dir-named executable only if this is package main.
if p.Name == "main" {
allRemove = append(allRemove,
elem,
elem+".exe",
p.DefaultExecName(),
p.DefaultExecName()+".exe",
)
toRemove[elem] = true
toRemove[elem+".exe"] = true
toRemove[p.DefaultExecName()] = true
toRemove[p.DefaultExecName()+".exe"] = true
}
// Remove package test executables.
allRemove = append(allRemove,
elem+".test",
elem+".test.exe",
p.DefaultExecName()+".test",
p.DefaultExecName()+".test.exe",
)
toRemove[elem+".test"] = true
toRemove[elem+".test.exe"] = true
toRemove[p.DefaultExecName()+".test"] = true
toRemove[p.DefaultExecName()+".test.exe"] = true
// Remove a potential executable, test executable for each .go file in the directory that
// is not part of the directory's package.
@ -355,47 +351,31 @@ func clean(p *load.Package) {
}
if base, found := strings.CutSuffix(name, "_test.go"); found {
allRemove = append(allRemove, base+".test", base+".test.exe")
toRemove[base+".test"] = true
toRemove[base+".test.exe"] = true
}
if base, found := strings.CutSuffix(name, ".go"); found {
// TODO(adg,rsc): check that this .go file is actually
// in "package main", and therefore capable of building
// to an executable file.
allRemove = append(allRemove, base, base+".exe")
toRemove[base] = true
toRemove[base+".exe"] = true
}
}
if cfg.BuildN || cfg.BuildX {
sh.ShowCmd(p.Dir, "rm -f %s", strings.Join(allRemove, " "))
}
toRemove := map[string]bool{}
for _, name := range allRemove {
toRemove[name] = true
}
for _, dir := range dirs {
name := dir.Name()
if dir.IsDir() {
continue
}
if cfg.BuildN {
continue
}
if cleanFile[name] || cleanExt[filepath.Ext(name)] || toRemove[name] {
removeFile(filepath.Join(p.Dir, name))
removeFile(sh, filepath.Join(p.Dir, name))
}
}
if cleanI && p.Target != "" {
if cfg.BuildN || cfg.BuildX {
sh.ShowCmd("", "rm -f %s", p.Target)
}
if !cfg.BuildN {
removeFile(p.Target)
}
removeFile(sh, p.Target)
}
if cleanR {
@ -407,7 +387,13 @@ func clean(p *load.Package) {
// removeFile tries to remove file f, if error other than file doesn't exist
// occurs, it will report the error.
func removeFile(f string) {
func removeFile(sh *work.Shell, f string) {
if cfg.BuildN || cfg.BuildX {
sh.ShowCmd("", "rm -f %s", f)
}
if cfg.BuildN {
return
}
err := os.Remove(f)
if err == nil || os.IsNotExist(err) {
return