Skip to content

wasi: fix working directories and test filesystem access - #5874

Draft
jakebailey wants to merge 3 commits into
tinygo-org:devfrom
jakebailey:wasi-test-root
Draft

jakebailey wants to merge 3 commits into
tinygo-org:devfrom
jakebailey:wasi-test-root

Conversation

@jakebailey

Copy link
Copy Markdown
Member

The WASI working directory handling wasn't quite right. WASIp1 wasn't using PWD or the first preopen like upstream Go, and WASIp2 only worked when the working directory exactly matched a preopen.

Fix those, then let the test runner mount the host root like upstream Go's does. The old directory mounts got in the way of stdlib filesystem tests. Tests also need the physical package directory rather than a path through the synthetic GOROOT.

This will enable more stdlib tests, though they can't get enabled in this PR quite yet. There are new tests for some stuff, anyway.

This also does mean that a tinygo test can access more files, but... upstream go test is the same, so I don't really think it matters?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

WASIp1 transfers an incompatible allocation to libc ownership, and Windows host paths are not mapped to valid WASI guest paths.

2 open findings
What changed in this PR

Fixes WASI working-directory initialization and filesystem access for tests.

Changes:

  • Initializes WASIp1/WASIp2 working directories from PWD and preopens.
  • Revises Wasmtime filesystem mounts and test working directories.
  • Adds WASI filesystem and preopen coverage.
File Description
tests/​testing/​pass/​pass_wasm_test.go Tests WASIp1 startup directory access.
testdata/​wasip2_cwd.go Adds WASIp2 CWD scenarios.
testdata/​filesystem.go Tests relative access after Chdir.
testdata/​env.go Tests PWD and relative access.
targets/​wasm-unknown.json Removes default temporary mount.
targets/​wasip2.json Removes default temporary mount.
targets/​wasip1.json Links CWD support and removes default mount.
src/​syscall/​libc_wasip2.go Resolves nested and normalized CWDs.
src/​syscall/​file_hosted.go Narrows hosted Getwd targets.
src/​syscall/​cwd_wasip1.go Implements WASIp1 CWD initialization.
main.go Configures Wasmtime mounts and PWD.
main_test.go Adds runner and CWD integration tests.
compileopts/​target_test.go Tests emulator template expansion.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread main.go Outdated
}

emuArgs = append(emuArgs, "--dir="+wd)
emuArgs = append(emuArgs, "--env=PWD="+wd)
Comment thread src/syscall/cwd_wasip1.go
Comment on lines +32 to +33
initialWorkingDirBuffer = cstring(cwd)
libcCwd = unsafe.SliceData(initialWorkingDirBuffer)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true?

@jakebailey
jakebailey marked this pull request as draft October 13, 2026 17:16
Go initializes the WASI working directory from PWD or the first preopen.
TinyGo leaves wasi-libc at its virtual root, so filesystem calls can
resolve relative paths against a different directory than callers expect.

Use the same initial directory without changing filesystem permissions.
A working directory can be inside a preopened directory without matching
its root. Requiring an exact match leaves relative file access rooted at
the wrong directory, including when a WASI host exposes only its root.
Upstream Go runs WASI tests with the host root preopened. Restricting
TinyGo tests to package directories and temporary files makes portable
filesystem tests fail even when the runtime supports their operations.

Match upstream test access without changing normal program execution.
Tests have host-root filesystem access and must be trusted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants