Skip to content

cmd/go: avoid surprising inclusion of "hidden" files when using //go:embed #42328

Closed

Description

@Merovius

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 build or 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 clean before go 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 every go test or go 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_Store is 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:embed directive 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_Store and 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 assets or 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 _* and testdata really be skipped (comment by @mpx)? The case for them comes down to consistency with go build and seems significantly weaker than for .*.

There where some alternatives suggested:

  • Add a ** matching operator to filepath.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

  1. mvdan commented on Nov 1, 2020

    @mvdan
    Member

    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:

    $ go help packages
    [...]
    
    Directory and file names that begin with "." or "_" are ignored
    by the go tool, as are directories named "testdata".
    

    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.

  2. Merovius commented on Nov 2, 2020

    @Merovius
    ContributorAuthor

    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:

    * is about files and not packages

    I want to make sure we are in agreement that this isn't just about globs specifically. As I said, I think //go:embed assets also shouldn't embed assets/.DS_Store (for example), even though it doesn't contain a glob.

  3. earthboundkid commented on Nov 2, 2020

    @earthboundkid
    Contributor

    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.

  4. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Nov 2, 2020
  5. added this to the Go1.16 milestone on Nov 2, 2020
  6. dmitshur commented on Nov 2, 2020

    @dmitshur
    Member

    CC @rsc.

  7. mvdan commented on Nov 3, 2020

    @mvdan
    Member

    @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 _*.

  8. earthboundkid commented on Nov 3, 2020

    @earthboundkid
    Contributor

    Icon\r isn’t dotfile, but it is hidden. Should Go include it?

  9. changed the title [-]embed: Surprising inclusion of "hidden" files[/-] [+]proposal: cmd/go: avoid surprising inclusion of "hidden" files when using //go:embed[/+] on Nov 3, 2020
  10. modified the milestones: Go1.16, Proposal on Nov 3, 2020
  11. zephyrtronium commented on Nov 3, 2020

    @zephyrtronium
    Contributor

    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?

  12. andig commented on Nov 4, 2020

    @andig
    Contributor

    For sake of completeness: we need the ability to exclude files on purpose, thunk assets.go. Might also solve excluding dotfiles.

  13. earthboundkid commented on Nov 4, 2020

    @earthboundkid
    Contributor

    #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:

    • Dot files should be excluded unless specifically included. (Definitely do this.)
    • Hidden files should probably also be excluded to the extent that we can figure out what files are hidden on the system. Since hidden files this is system specific, it may not always work properly, e.g. across a network. (Maybe do this if it can be made portable.)
    • A way to specifically exclude files might be nice. (Can be left for Go 1.17 if we exclude dotfiles now.)
    • Go source files could be automatically excluded at the cost of losing a few blog posts about easy Quines. (Probably do this unless there is a compelling usecase.)
  14. 129 remaining items

  15. earthboundkid commented on Jan 22, 2021

    @earthboundkid
    Contributor

    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.

  16. Merovius commented on Jan 22, 2021

    @Merovius
    ContributorAuthor

    The workaround we've discussed is to write a small go generate program that generates a //go:embed comment. Something along the lines of

    package main
    
    func main() {
        const base = "embed"
        var dirs []string
        err := filepath.Walk(base, func(path string, info os.FileInfo, err error) {
            if err != nil {
                return err
            }
            if info.IsDir() {
                rel, _ := filepath.Rel(base, path)
                dirs = append(dirs, filepath.Join(rel, "*"))
            }
        })
        if err != nil {
            log.Fatal(err)
        }
        err = ioutil.WriteFile("embed.gen.go", []byte(fmt.Sprintf(template, strings.Join(dirs, " "))), 0644)
        if err != nil {
            log.Fatal(err)
        }
    }
    
    const template = `// Code generated by go run embed.go; DO NOT EDIT.
    
    package pkg
    
    import "embed"
    
    //go:embed %s
    var embedded embed.FS`

    That 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 generate when 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.

  17. jpap commented on Jan 22, 2021

    @jpap
    Contributor

    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.

    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.

  18. jpap commented on Jan 22, 2021

    @jpap
    Contributor

    The workaround we've discussed is to write a small go generate program that generates a //go:embed comment.

    This is an undesirable workaround for the reasons already discussed:

    • Use of an external generator.
    • Not automatically kept fresh.
    • Increased complexity compared to a simple -all flag, or just not excluding files with leading underscore.

    Let's continue the discussion in #43854.

  19. earthboundkid commented on Jan 22, 2021

    @earthboundkid
    Contributor

    That doesn't work because these files aren't included in the executable to begin with: there's nothing to filter.

    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 meaning Open("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.

  20. jpap commented on Jan 22, 2021

    @jpap
    Contributor

    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 meaning Open("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.

    Thanks for the clarification. That's the approach I also found undesirable in my original post, quoted again below.

    Another approach which I dislike is to use some encoding to escape filenames that //go:embed otherwise denies me from using naturally. That would force design changes to a larger surface area of an otherwise already complex project just to simplify the build.

    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.

  21. miquella commented on Jan 22, 2021

    @miquella

    Although my initial inclination was to add a -all flag, 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 files

    This 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.

  22. gopherbot commented on Oct 29, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/359413 mentions this issue: cmd/go: add //go:embed all:pattern

  23. added a commit that references this issue on Nov 9, 2021
  24. moved this to Accepted in Proposalson Aug 10, 2022
  25. removed this from Proposalson Oct 19, 2022
  26. locked and limited conversation to collaborators on Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions