Repository navigation
Description
Edit by rsc, Dec 1 2020: Please note that the "likely accepted" answer here is #42328 (comment), specifically:
-
patterns passed to //go:embed are evaluated exactly with filepath.Glob, not "Glob with special cases".
So matching * keeps matching .foo, because it does in Glob. -
when you name a directory explicitly, as in //go:embed dir, the code that walks that directory collecting
all the files to embed will ignore names beginning with dot or underscore, the same as the go command does
for deciding what to build.
This means that //go:embed static/* will embed .DS_Store if you really want to, while //go:embed static will not.
This is forked off from #41191 to talk about a specific issue with the design as-accepted. #42325 and #42321 talk about the same problem, but they are very solution-focused and I think it is more appropriate to discuss the actual problem first.
Namely: @carlmjohnson has pointed out that the //go:embed directive includes .DS_Store files under MacOS. This is certainly working-as-designed, but I think it should be discussed whether that's actually the semantic we want. I want to talk about the .DS_Store example specifically, but there are other dot-files with similar properties. .DS_Store does illustrate something significant though, because it is a directory that is (AIUI, I'm not a Mac-user myself) created non-interactively by a third-party software in every subdirectory. So it is not explicitly created by the user and it is permanent (and will be re-created at some point, if deleted).
There have been several suggestions made so far, which IMO are deficient in one way or another:
- Manually clean up before
go buildor live with inclusion of any such files. IMO this puts unreasonable expectations on users. No matter how much we feel that in an ideal world, this should be what happens - I just don't think it is what will happen, in practice. - Run
git cleanbeforego build. This is certainly useful in CI/CD, but during regular development, it would also delete progress made. It's certainly not something I'd want to run before everygo testorgo build. In CI/CD it is useful, but it will probably still be forgotten by many people - though it's also far less of a problem, because the chance of pollution is low. - Add ways to exclude specific files. IMO
.DS_Storeis something that ~always should be excluded and as others have pointed out, there are many other patterns of files that should be excluded and would have to be listed. In effect, this would mean that ~every//go:embeddirective would have to list a non-canonical, long list of exclusions, to cover any tools used by people. It also still suffers from the problem that people have to know about the problem in the first place. So IMO it would still end up with accidental inclusions of.DS_Storeand similar files. - Make
*not match dot-files. This isn't really helpful either, as even then, if a directory is named (either directly as//go:embed assetsor indirectly via a glob), we would still recursively include all subdirectories, including.DS_Store. - Not include dot-files at all, unless explicitly mentioned. It can be argued that this is a "dirty" approach, because dot-files aren't actually special and I would agree with that. OTOH, it's IMO a) the approach leading to the least accidents with the lowest impact in actual practice and b) when an accident happens, it can be debugged and fixed most easily (simple testing will show the dot-file to be missing and the docs can clearly say that they have to be mentioned explicitly).
There are probably other approaches that can be discussed. It would also be a valid answer to close this as WAI. But if we release go 1.16 with embedding, we get locked into the semantics we implemented, so IMO this should be closed one way or another before releasing it into the wild.
Discussion summary (as of 2020-11-21, 10:00 UTC)
This is my best attempt at a fair and unbiased discussion-summary. It is necessarily subjective, though, so apologies if I left something out or misrepresented someone.
The resulting proposal from the discussion which is currently marked as "likely accept" is to have globs match as it currently does, but to exclude .* and _* files (editors remark: "as the go command" probably implies testdata as well) from recursive directory walks when a directory is given explicitly.
There are a couple of open questions:
- Will hidden files also be excluded if a directory is matched by a glob, or just if it is mentioned by name (comment by @mpx)? Consensus seems to be that it would apply to globbed directories as well - that is, "expand globs first, then recursively walk every directory given with hidden files/dirs skipped".
- Does "hidden" also apply to extended filesystem attributes? (comment by @andrius4669)? There was one comment in favor by @Merovius and one comment against by @inliquid.
- Should
_*andtestdatareally be skipped (comment by @mpx)? The case for them comes down to consistency withgo buildand seems significantly weaker than for.*.
There where some alternatives suggested:
- Add a
**matching operator tofilepath.Glob- don't leave out hidden files when walking directories (comment by @ianthehat). @rsc remarks that this might be unexpectedly unspecified. Also, the presence of**would also make it easier to specifically match all hidden files, if they are desired (comment by @Merovius) so can be argued in favor of this proposal as well. - Provide a simple way to test for accidental file inclusion, leave semantics as-is (comment by @nightlyone). The main argument against that is that it requires knowledge that this should be tested (comment by @Merovius).
- Provide a wrapper-fs that filters out undesirable files (comment by @seankhliao). In addition to the same argument as against tests, even the embedding of undesired files might be harmful, not just their use (comment by @SamWhited)
- Remove support for recursive directory inclusion for now, leave globs as-is. A separate go-generate tool can create complete file-listings and we can experiment syntax/semantics using such a tool during the next cycle (comment by @ianthehat).
- Remove both recursive directory walking and globs, only allow explicit lists (comment by @mpx).
There where some counter arguments:
- It is inconsistent and confusing that hidden files are included when using
*but not in directory walks (comment by @dcormier). The proponents response is that we agree, but it might still be better than the current alternative. We need to make a tradeoff (comment by @Merovius). @SamWhited posted examples of where they embedded dot-files, which might be useful to inform that tredoff. - We need to have an escape hatch for "everything exactly as on disk" (comment by @seankhliao). The best escape hatch available so far is to use go-generate to create a list (comment by @mvdan), there might be the need for a more convenient one, if this comes up often enough.
Activity
mvdan commentedon Nov 1, 2020
Thanks for filing this issue. In my comment in the original proposal I did suggest filing a new issue from the point of view of a bug report, so I agree with you that we should begin with the problem and not multiple potential solutions. I hope this doesn't mean I get more thumbs down reactions :)
I fully agree that this needs a decision before 1.16, even if the decision is that we're okay with the current semantics. I also tend to agree that excluding files by accident is less harmful than including them by accident, because the former is easy to spot but the latter could go unnoticed for a long time.
I'm still uneasy about introducing the notion of "hidden files" in the Go toolchain, but they do have a sort of precedent:
Perhaps we could copy the same rule here.
*is about files and not packages, but since builds happen inside package directories, I think it could make sense to be consistent. And the notion of "ignored filenames" by the toolchain would remain easy to remember.Merovius commentedon Nov 2, 2020
I think that would be a fine approach (as long as explicit listing or a
.*-glob would still allow you to include them). Just to be clear though:I want to make sure we are in agreement that this isn't just about globs specifically. As I said, I think
//go:embed assetsalso shouldn't embedassets/.DS_Store(for example), even though it doesn't contain a glob.earthboundkid commentedon Nov 2, 2020
I think this issue has made a very strongly case for "fail safe" instead of "fail unsafe but document/provide an escape hatch" semantics.
One additional thought: if we get the semantics "wrong" in 1.16, would it be possible to revise them in 1.17 with a go.mod directive? If so, that to me suggests doing something "conservative" in 1.16 and revising to be "liberal" in later versions of Go if the conservative thing was found to be too conservative.
dmitshur commentedon Nov 2, 2020
CC @rsc.
mvdan commentedon Nov 3, 2020
@Merovius agreed. The rule would limit both recursing into directories, and the
*glob. I think any other glob shouldn't be affected, so that one may use globs such as.*or_*.earthboundkid commentedon Nov 3, 2020
Icon\risn’t dotfile, but it is hidden. Should Go include it?[-]embed: Surprising inclusion of "hidden" files[/-][+]proposal: cmd/go: avoid surprising inclusion of "hidden" files when using //go:embed[/+]zephyrtronium commentedon Nov 3, 2020
In a similar vein, any file can be marked hidden on NTFS. Should //go:embed read filesystem attributes, even on a Linux host compiling code on an NTFS volume, to ignore
thumbs.db?andig commentedon Nov 4, 2020
For sake of completeness: we need the ability to exclude files on purpose, thunk assets.go. Might also solve excluding dotfiles.
earthboundkid commentedon Nov 4, 2020
#42325 was closed to focus discussion here.
The proposal there was to add a magic comment for negative globs, like
//go:embed-exclude *.go. The objection was that it would lead to a lot of OS/IDE specific exclusions being listed and still miss some things that should filtered when encountering a new environment.My current feeling is that a combination of approaches should be taken:
129 remaining items
earthboundkid commentedon Jan 22, 2021
This is probably the wrong place for discussion but if you just need to embed some files with underscores, a quick workaround is that you can write a wrapper fs.FS that changes file names from -whatever to _whatever automatically and then rename the files on disk to start with a dash.
Merovius commentedon Jan 22, 2021
The workaround we've discussed is to write a small
go generateprogram that generates a//go:embedcomment. Something along the lines ofThat way you still get the efficiency and integration of the native embedding support (which you lack if you use a third-party tool). I agree that it's less convenient to still have to run
go generatewhen the set of files changes, but I'm still convinced that this is enough of an edge-case that it's fine if it,s less convenient.In any case, I think the release of go 1.16 is imminent, so I think it's too late to change it for this release. I see that you've opened a new issue, I think we can discuss options there.
jpap commentedon Jan 22, 2021
That doesn't work because these files aren't included in the executable to begin with: there's nothing to filter.
I've opened #43854 if you want to continue the discussion.
jpap commentedon Jan 22, 2021
This is an undesirable workaround for the reasons already discussed:
-allflag, or just not excluding files with leading underscore.Let's continue the discussion in #43854.
earthboundkid commentedon Jan 22, 2021
The idea is that you change a/_whatever.txt to a/-whatever.txt and then add a wrapper than interprets
Open("a/_whatever.txt")as meaningOpen("a/-whatever.txt")and similarly changes the ReadDir calls. It's an ugly hack, but probably could be done in like 20 LoC plus a bulk rename.jpap commentedon Jan 22, 2021
Thanks for the clarification. That's the approach I also found undesirable in my original post, quoted again below.
In my project that change isn't limited to a one-time bulk rename: it means changing the design of the all the code that uses this directory/file tree structure. The project uses many such directory trees; most are used outside of //go:generate on a live filesystem. The motivation for using //go:generate here is that there are some special trees that are "constant" and need to be distributed with the executable. Others that live on the filesystem are specific to the environment in which they run.
miquella commentedon Jan 22, 2021
Although my initial inclination was to add a
-allflag, or similar, I would worry about the additional complexities that incurs. Specifically that escaping or other escape hatches have to be added to the mix.One alternative may be to have a second directive with different semantics:
//go:embed— keeps the existing behavior//go:embed-all— embeds all filesThis could become unwieldy if many permutations were anticipated to be added in the future.
One additional thought: does the module proxy respect embed directives? If not, the files may be excluded before the embed directive takes place anyway.
Edit: I'll go add this comment to the discussion on #43854 as well.
gopherbot commentedon Oct 29, 2021
Change https://golang.org/cl/359413 mentions this issue:
cmd/go: add //go:embed all:patterncmd/go: add //go:embed all:pattern
cmd/go, embed: exclude .* and _* from embedded directory trees