Skip to content

cmd/vet: warn about "// go:" comments (with space) #43698

Open
@bcmills

Description

@bcmills

The embed.FS type added for #41191 documents the following behavior:

An FS is a read-only collection of files, usually initialized with a //go:embed directive. When declared without a //go:embed directive, an FS is an empty file system.

I expect that package authors essentially never intend to end up with an empty read-only filesystem in a production package. (In tests, perhaps, but not otherwise.) For the few cases where they do, they should be able to add an explicit //go:embed directive indicating that the filesystem is intentionally empty.

Otherwise, it is too easy for an unintentional typo in the //go:embed directive, such as the one discovered in #43682 (comment), to result in a difficult-to-debug failure at run time.

I propose that cmd/vet should emit a warning when:

  • a package-level variable is declared with type embed.FS,
  • without any corresponding //go:embed directive (not even one with an empty pattern list),
  • in a non-test source file.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Accepted

Relationships

None yet

Development

No branches or pull requests

Issue actions