-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
INTRO
Since GO 1.16 was released, many people feel really confused due to the fact that they are unable to use newly added go:embed pattern, to include files in a parent directory.
CAUSE
As stated in #46056 (comment) the issue happens, because embed.FS implements io/fs.FS. Documentation says:
// Open opens the named file.
//
// When Open returns an error, it should be of type *PathError
// with the Op field set to "open", the Path field set to name,
// and the Err field describing the problem.
//
// Open should reject attempts to open names that do not satisfy
// ValidPath(name), returning a *PathError with Err set to
// ErrInvalid or ErrNotExist.
so in theory .. patterns does not satisfy fs.ValidPath
Path names must not contain an element that is “.” or “..” or the empty string, except for the special case that the root directory is named “.”.
WHAT IS WRONG WITH THIS APPROACH
Although I don't understand why ValidPath does not support dot-dot and I wouldn't challange this, I am sure that it isn't a right approach in case of go:embed (embed.FS). - There is no real reason for disallowing .. in this particular case.
Here is the exact chapter of design doc: https://go.googlesource.com/proposal/+/master/design/draft-embed.md?pli=1#dot_dot_module-boundaries_and-file-name-restrictions
it states that .. pattern should be allowed as long as it doesn't cross module boundries (for the obvious reasons).
Generally, I see no reason why it shouldn't be done this way
Usa cases
- embeding a README.md in a
gofile - making
assetsfolder in the root of project
Alternative
In my opinion the following alternative could be considered:
In order to allow embeding files in a directory being higher in tree, a special "path-prefix" / could be introduced.
For example //go:embed /README.md, where / means go.mod's directory.
Adventages
- does not break compatibility with past versions
- fix the issue
disadventages
May be a bit confusing for some linux users (where / means filesystem's root 😄).
REFERENCE
https://go.googlesource.com/proposal/+/master/design/draft-embed.md
#46056