-
Notifications
You must be signed in to change notification settings - Fork 171
Ensure we read from overlay when calling readDir #786
Ensure we read from overlay when calling readDir #786
Conversation
`fileSystem.readDir` always reads from the actual filesystem, even if there is an overaly directory present. Read from the overlay directory in these circumstances; we shouldn't be hitting the disk. Also, if the root path is "/" we can skip loading and return true. This allows fully in-memory packages as per cuelang#607.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
1 similar comment
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it! |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
1 similar comment
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
Skip statting for genned dirs on webassembly.
e52901b
to
60d8365
Compare
How would this impact someone using Go created WASM from a NodeJS script, where the filesystem is available? I'm not super familiar, but this thought occurred to me |
https://github.com/cuelang/cue/tree/master/pkg/path This may be the preferred way to handle GOOS specific differences |
Oh wow, yeah, that's a great question. I'm not super familiar either, but as far as I know this logic applies:
In this case, maybe we should have documentation that specifies how this would work? I'm not entirely certain that you would be looking to mutate files when running in WASM; it's usually used to ensure you're running in a sandbox specifically so that mutations don't occur. So, short answer: I think this might be a risk but a very, very slim one, esp. as disk functionality is not supported by WASM yet. What are your thoughts? RE. GOOS specific differences, thanks for the pointer - will change! |
regarding WASM generally, my hunch is that it becomes a new JVM like platform with a LLVM flavor. That is to say that you get the intermediate assembly format that has a runtime for different platforms, and you get N language frontends that all compile to it. So I see it becoming more ubiquitous, not just for inside the browser. There are already projects pushing on this front, see https://github.com/wasmerio/wasmer |
Yep, I use these in my project (wasmtime, not wasmer - wasmer seems to be a UI over wasmtime) and these are still isolated. Edit: After some research there is I don't think this is supported in Go just yet - which is why os.Stat etc fail in the Go runtime. We will either be blocked on Go developing this, or have to revisit overlays when WASI compatibility is reached in Go. And, as anticipated in the third bullet point above, you explicitly tell the VM which directories and files are available before running:
Essentially, it is the same as your Overlay implementation :) That said, once I make the path changes it would be nice to merge this in so that Cue actually works with overlay packages in wasm? |
We're still waiting for @mpvl and @myitcv to chime in. They are the core maintainers and have permissions on the repo. I just hangout a lot and contribute where / when I can. I'm not sure how they will feel about conditionals on GOOS, it seems like an anti-pattern to me personally. They also tend to prefer holistic solutions than piecemeal fixes for edge cases. This might be part of a larger WASM of FileSystem story. It would be worthwhile to open an broader discussion particulars and see where that takes us. There have been previous discussions around how to make Cue work well in the context of an API server that are probably related to this. Generally, it's better to start with an issue / discussion that ends up with a proposal and then work on code. Marcel and Paul come from the Go team, and so a lot of good philosophy and practices carry over with them. It may feel slow at times, but it results in much better designed and implemented software in the long term. Separately, you can take a look at the contributing docs and get setup with Gerrit. Most development happens over there. This repo is more of a mirror than the source of truth. There are some sync services setup that I'm not familiar with (for a bit of context). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes.
This still needs a test to expose the problem.
@@ -142,6 +142,10 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance { | |||
|
|||
found := false | |||
for _, d := range dirs { | |||
if d[1] == "/" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this also work for Windows volumes and other root indicators?
@@ -414,6 +415,9 @@ func IsEllipsis(x ast.Decl) bool { | |||
|
|||
// GenPath reports the directory in which to store generated files. | |||
func GenPath(root string) string { | |||
if runtime.GOOS == "js" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would "js" have to be special-cased here?
Is it because avoiding picking up the "pkg" directory? This really should be disabled for all OSes, but probably only after a 3.0 release. Is there any reason why JS is special here? If so, please add a comment/ TODO.
@@ -167,6 +167,20 @@ func hasSubdir(root, dir string) (rel string, ok bool) { | |||
|
|||
func (fs *fileSystem) readDir(path string) ([]os.FileInfo, errors.Error) { | |||
path = fs.makeAbs(path) | |||
|
|||
if fi := fs.getDir(path, false); fi != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also a fs.getDir call below that is ostensibly there to achieve the same. So if the below code is buggy, it should be fixed there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one of the difficulties when lacking the fs.DirEntry
interface. We call ReadDir to iterate through, but this means we must have a concrete directory to read from. This breaks webassembly, as there is no underlying directory to read. This is why we must special case unless we comform to fs.DirEntry
and support only go > 1.16.
I do think that @myitcv was correct regarding this patch - it fixes this code such that we use overlays if possible - but the underlying root is the lack of io/fs
support.
To be honest, I'm fairly blocked on supporting cue with embedded packages without these fixes. I really understand that we should strive towards an io/fs
driver with interfaces - that's the correct solution here.
As you can probably tell, I'm not familiar with cue's internals enough to take this on - I could give it a shot, but there will be lots of feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then my suggestion is that we close this PR and move discussion to #607 or another issue if that one is not appropriate. In those issues, let's focus on specific use cases to help shape the solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR aims to fix the bugs in #607 - we might want to start an entirely new discussion on the architecture of overlays, files, and cue loading using io/fs
as the interface to account for windows, linux, and webassembly (virtual fs) usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR aims to fix the bugs in #607
Per my comments in #786 (comment), I'm not clear that the behaviour described in #607 is actually a bug (the issue itself is ostensibly a feature request). I'll comment on #607 to see if we can find a way forward.
Sorry for the delay in getting around to this.
It's not clear to me that this is correct, and therefore I'm not clear that this change is solving the right problem. An overlay, in its current form, exists in order to complement what is on disk, rather than wholesale replace its contents.
In that respect it is a different beast to What I think you're looking for here is an FWIW the CUE playground (which is WASM-based) does not run into this because it consumes its input over stdin. |
Closing per #786 (comment) |
Quickly wanted to reply to this and say that the playground does not resolve this because it consumes from stdin - it resolves this because it has no |
fileSystem.readDir
always reads from the actual filesystem, even ifthere is an overaly directory present. Read from the overlay directory
in these circumstances; we shouldn't be hitting the disk.
Also, if the root path is "/" we can skip loading and return true. This
allows fully in-memory packages as per
#607.
This helps with fix certain aspects in #607, but does not refactor
fileSystem
to use any of the newer interfaces that we could.