-
Notifications
You must be signed in to change notification settings - Fork 18.1k
proposal: embed: allow embeding files in a parent directory #58519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
embed.FS
not use io.FS
I'm a bit confused, your proposal is that |
exactly EDIT ofc |
The section you quoted from the draft design says it should be disallowed entirely. The solution to this would be to make the package at the root level export the embedded FS. Import cycles don't have to be an issue if you organize your code structure appropriately, or just pass it down as an argument to dependencies rather than importing it. |
@seankhliao and? Despite this fact, I can't see any reason for closing this proposal (fact that "It is tested for in testing/fast does not mean that it is not possible to be changed, does it?). Over that, there is also an alternative solution (in
Thank you! |
proposals need to be achievable within the guarantees we make as part of https://go.dev/doc/go1compat |
@seankhliao I think this is a valid proposal, at least for the AFAIK this is not breaking the go1compat promise. I think the |
@Jorropo We promise today that |
So lets make it language-change if really no way around |
@gucio321 I understand that you find this to be frustrating, but we are not going to change the language merely so that people can embed files in a parent directory. |
ok, @ianlancetaylor I see your point, but I have one more question:
ok, so I see whay
//go:embed /example/path
var data embed.FS
// ...
f, _ := data.Open("example/path/file.txt") and will not break compatibility, will it?
|
I may have missed something but I agree that doesn't seem to break compatibility. I think it might be confusing for people reading the code, but otherwise I think it could work. Want to open a new proposal so that people can skip the discussion on this one? |
@ianlancetaylor I was saying that
I think that this code inside //go:embed ../asserts
var assets embed.FS
// ...
dog, err := assets.Open("dog.png") or //go:embed /asserts
var assets embed.FS
// ...
dog, err := assets.Open("dog.png") would work without breaking anything. In other words, I don't understand why this is not a valid proposal. I can think of counter arguments:
|
//go:embed ../asserts
var assets embed.FS
// ...
dog, err := assets.Open("dog.png") it will break, since this embed.FS actually looks this way > assets
> dog.png so you would need |
ah ok, I see thx |
For the case of a VERSION file, we were able to get past this by using symbolic link from VERSION at repo root (go mod root) to VERSION file in embeds package. The embeds package loads the real VERSION file, but anytime a dev opens and edits VERSION, it really edits the one in the embeds directory. Helps a lot with keeping the traditional VERSION file visible at repo root. |
Uh oh!
There was an error while loading. Please reload this page.
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
implementsio/fs.FS
. Documentation says:so in theory
..
patterns does not satisfyfs.ValidPath
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 ofgo: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
go
fileassets
folder in the root of projectAlternative
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/
meansgo.mod
's directory.Adventages
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
The text was updated successfully, but these errors were encountered: