Skip to content

cmd/go: clean sub command should interop with generate a bit better #10059

@mmlb

Description

@mmlb

We currently have a file that is generated via go generate to which the .go suffix is added to the filename. When packaging we run go clean which proceeds to delete the source file, this was unexpected. Here is a minimal example:

~/t  
❯ tail -n +1 main.go print.dummy
==> main.go <==
package main

//go:generate cp print.dummy print.dummy.go

func main() {
    printIt("hi")
}

==> print.dummy <==
package main

import "fmt"

func printIt(s string) {
    fmt.Println(s)
}

~/t  
❯ go generate && ls
main.go  print.dummy  print.dummy.go

~/t  
❯ go clean -x
cd /home/mmlb/t
rm -f t t.exe t.test t.test.exe main main.exe print.dummy print.dummy.exe

I expect that the $source.file -> $source.file.go pattern is probably common enough that go clean should special case it. I was very surprised to see go clean -x purposefully deleting the file even though the match is documented/specified.

A couple of options are possible to handle this case:

  1. Do not delete MAINFILE if it is not executable.
    It seems MAINFILE is deleted due to it being an expected binary generated via go build *.go or similar.
  2. Special case a file that shares the root file name of a .go source file as long as the root contains a . in the name.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions