From c87bb0e9cc1a9d145af25c223c9795ca1a8d236e Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Fri, 31 Dec 2021 10:29:24 -0800 Subject: [PATCH] pass more --dirs to wasmtime so it can read the entire module tree This allows compress/flate to pass on -target=wasi out of the box Fixes #2367 --- builder/build.go | 14 ++++++++++++++ loader/loader.go | 8 ++++++++ main.go | 20 ++++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/builder/build.go b/builder/build.go index a2675736..f3b006af 100644 --- a/builder/build.go +++ b/builder/build.go @@ -45,6 +45,11 @@ type BuildResult struct { // binary must be run in the directory of the tested package. MainDir string + // The root of the Go module tree. This is used for running tests in emulator + // that restrict file system access to allow them to grant access to the entire + // source tree they're likely to need to read testdata from. + ModuleRoot string + // ImportPath is the import path of the main package. This is useful for // correctly printing test results: the import path isn't always the same as // the path listed on the command line. @@ -799,9 +804,18 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil default: return fmt.Errorf("unknown output binary format: %s", outputBinaryFormat) } + + // If there's a module root, use that. + moduleroot := lprogram.MainPkg().Module.Dir + if moduleroot == "" { + // if not, just the regular root + moduleroot = lprogram.MainPkg().Root + } + return action(BuildResult{ Binary: tmppath, MainDir: lprogram.MainPkg().Dir, + ModuleRoot: moduleroot, ImportPath: lprogram.MainPkg().ImportPath, }) } diff --git a/loader/loader.go b/loader/loader.go index 489ed24f..0e921fa6 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -47,6 +47,14 @@ type PackageJSON struct { ImportPath string Name string ForTest string + Root string + Module struct { + Path string + Main bool + Dir string + GoMod string + GoVersion string + } // Source files GoFiles []string diff --git a/main.go b/main.go index 52856bb7..e7668187 100644 --- a/main.go +++ b/main.go @@ -233,6 +233,19 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options return passed, err } +func dirsToModuleRoot(maindir, modroot string) []string { + var dirs = []string{"."} + last := ".." + // strip off path elements until we hit the module root + // adding `..`, `../..`, `../../..` until we're done + for maindir != modroot { + dirs = append(dirs, last) + last = filepath.Join(last, "..") + maindir = filepath.Dir(maindir) + } + return dirs +} + // runPackageTest runs a test binary that was previously built. The return // values are whether the test passed and any errors encountered while trying to // run the binary. @@ -269,9 +282,12 @@ func runPackageTest(config *compileopts.Config, stdout, stderr io.Writer, result args = append(args, "--dir="+tmpdir, "--env=TMPDIR="+tmpdir) // TODO: add option to not delete temp dir for debugging? defer os.RemoveAll(tmpdir) - // allow reading from current directory: --dir=. + // allow reading from directories up to module root + for _, d := range dirsToModuleRoot(result.MainDir, result.ModuleRoot) { + args = append(args, "--dir="+d) + } // mark end of wasmtime arguments and start of program ones: -- - args = append(args, "--dir=.", "--") + args = append(args, "--") if testVerbose { args = append(args, "-test.v") }