Skip to content

cmd/go/internal/imports: ScanDir ignores .go files that start with _ but not . #42047

Description

@dmitshur

From https://golang.org/cmd/go/:

Files whose names begin with "_" (including "_test.go") or "." are ignored.

ScanDir has a !strings.HasPrefix(name, "_") condition but does not have a !strings.HasPrefix(name, ".") one.

As a result, imports in a _ignore.go file don't contribute to the module graph:

$ cd $(mktemp -d)
$ go mod init m.test
go: creating new go.mod: module m.test
$ echo 'package p' > p.go

$ cat <<EOF >_ignore.go
package p
import _ "golang.org/x/mod/modfile"
EOF
$ go mod tidy
$ cat go.mod
module m.test

go 1.16

But imports in a .ignore.go file do:

# ... same as above

$ cat <<EOF >.ignore.go
package p
import _ "golang.org/x/mod/modfile"
EOF
$ go mod tidy          
go: finding module for package golang.org/x/mod/modfile
go: found golang.org/x/mod/modfile in golang.org/x/mod v0.3.0
$ cat go.mod
module m.test

go 1.16

require golang.org/x/mod v0.3.0

CC @jayconrod, @matloob, @bcmills.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions