Repository navigation
Description
Description
On WASI, Path.GetTempFileName() always throws, and the exception message says the operation succeeded:
System.IO.IOException : Success : '/tmp/'
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
at System.IO.Path.GetTempFileName()
GetTempFileName calls SystemNative_MksTemps. wasi-libc provides neither mkstemps nor mkstemp (HAVE_MKSTEMPS and HAVE_MKSTEMP are 0 for wasi), so the shim takes its WASI fallback, which returns -1 without setting errno:
// src/native/libs/System.Native/pal_io.c
#elif TARGET_WASI
assert_msg(false, "Not supported on WASI", 0);
result = -1;SystemNative_MkdTemp, used by Directory.CreateTempSubdirectory, has the same shape: it returns NULL on WASI without setting errno.
The temp directory is usable: with the test harness's --dir=. preopen, /tmp resolves to the app bundle's tmp directory. Only the unique-name creation is missing.
This fails any code that creates temp files. In the CoreCLR WASI library test runs on #134813 it accounts for about 160 failures across System.Data.Common, System.CodeDom, Microsoft.Extensions.Configuration.Functional, System.ComponentModel.Composition, System.ServiceModel.Syndication, System.Diagnostics.TextWriterTraceListener, System.Reflection.Metadata, System.Memory.Data and Microsoft.Extensions.Configuration.FileExtensions.
Proposed fix
Implement both entry points for WASI in pal_io.c using primitives wasi-libc supports, the same way libc implements mkstemp:
SystemNative_MksTemps: validate the template (at least sixXbefore the suffix, otherwiseEINVAL), then loop a bounded number of times:- fill the
XXXXXXfromgetentropy(backed bywasi:random), mapped to[A-Za-z0-9] open(pathTemplate, O_RDWR | O_CREAT | O_EXCL, 0600)- return the descriptor on success, retry on
EEXIST, return -1 witherrnopreserved on any other error - after the attempts run out, fail with
EEXIST
- fill the
SystemNative_MkdTemp: the same loop withmkdir(pathTemplate, 0700).- Set
errnoon every failure path, so a remaining failure produces a meaningful exception instead of "Success".
Filling the template can go in a small helper shared by both entry points. Tests that already cover Path.GetTempFileName and Directory.CreateTempSubdirectory exercise the change once they run on WASI.
Note
This issue was drafted with the help of GitHub Copilot.
Activity
dotnet-policy-service commentedon Sep 30, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Sep 30, 2026
Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Sep 30, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
[wasi] Quarantine temp-file and File.Copy failures on WASI
[wasi] Quarantine temp-file and File.Copy failures on WASI
[wasi] Quarantine temp-file and File.Copy failures on WASI
[wasi] Quarantine temp-file and File.Copy failures on WASI
[wasi] Quarantine temp-file and File.Copy failures on WASI
[wasi] Implement MksTemps/MkdTemp so GetTempFileName and CreateTempSu…