wazero
wazero copied to clipboard
Expose fsConfig.WithSysFSMount for custom writable FS support
I would like to implement a read/write filesystem in pure-Go and in memory, (not exposing an os fs).
The writable fs.FS proposal for Go was declined for the time being: https://github.com/golang/go/issues/45757
There is a comment still mentioning this proposal here: https://github.com/tetratelabs/wazero/blob/1458ccc8b07e87f68d3d29c2149ce77679b3e6ea/experimental/sys/fs.go#L28
experimental/sys.FS is the writable FS interface: https://github.com/tetratelabs/wazero/blob/1458ccc8b07e87f68d3d29c2149ce77679b3e6ea/experimental/sys/fs.go#L29
fsConfig has WithSysFSMount but it is not exposed in the FSConfig interface and is therefore inaccessible: https://github.com/tetratelabs/wazero/blob/1458ccc8b07e87f68d3d29c2149ce77679b3e6ea/fsconfig.go#L186
I want to write an implementation of experimental/sys.FS and provide it to wazero via WithSysFSMount.
Could WithSysFSMount be added to fsConfig or otherwise be exposed so I can call it? Thanks!
I can expose it like this:
// FSConfigWithSysFSMount extends FSConfig to expose the existing WithSysFSMount function.
// https://github.com/tetratelabs/wazero/issues/2076
type FSConfigWithSysFSMount interface {
WithSysFSMount(fs wazero_sys.FS, guestPath string) wazero.FSConfig
}
var writableFS wazero_sys.FS
fsConf.(FSConfigWithSysFSMount).WithSysFSMount(writableFS, "/")
But this is a runtime type assertion and can't be enforced at compile time, ideally FSConfigWithSysFSMount would be exposed by wazero and type-asserted that fsConfig conforms to that exported interface.
Admittedly, I think this may not be easy to find, but you should be able to write the following:
import (
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental/sysfs"
)
func main() {
var sys.FS = ...
config := wazero.NewFSConfig()
config.(sysfs.FSConfig).WithSysFSMount(myFs, guestPath)
...
}
e.g. see
https://github.com/tetratelabs/wazero/blob/2f2b6a9d2c21c31827cd98712d291e8937223084/experimental/sysfs/config_example_test.go#L14-L23
Could that just be added to the main FSConfig or perhaps add a NewFSConfig to sysfs which returns sysfs.FSConfig and have the type assertion in wazero? That way we can trust that the type assertion won't panic (covered by wazero tests)
Eventually, they will. They are not yet because this API has not been tested extensively by our users so far. So, please do play with it and let us know what other roadblocks you encounter :)