fix(fs): truncate the file when writeFile writes a stream (#3629)

In plugins/fs/guest-js/index.ts writeFile, the stream path opened the
file with {write, create} but no truncate, unlike the buffer path which
truncates unless `append` is set (commands.rs write_file_inner), so
shorter content left stale trailing bytes.

The stream path now passes `truncate: !options?.append`. api-iife.js
regenerated. E2E spec covers overwrite and append with a stream.
This commit is contained in:
Lucas Fernandes Nogueira authored and GitHub committed 2026-10-09 13:01:30 -03:00
1 parent 5e6c023a79
commit dcc8cb032b
4 files changed
+43 -2

No files matched your search

+33
View File
@@ -99,6 +99,39 @@ describePlugin('fs', () => {
expect(read).toEqual(bytes)
})
it('writeFile with a stream overwrites or appends', async () => {
const result = await tauri(async (api, path) => {
const baseDir = api.fs.BaseDirectory.AppData
const encoder = new TextEncoder()
const streamOf = (...chunks: string[]) =>
new ReadableStream<Uint8Array>({
start(controller) {
for (const chunk of chunks) {
controller.enqueue(encoder.encode(chunk))
}
controller.close()
}
})
await api.fs.writeTextFile(path, 'a much longer previous content', {
baseDir
})
await api.fs.writeFile(path, streamOf('short', ' stream'), { baseDir })
const overwritten = await api.fs.readTextFile(path, { baseDir })
await api.fs.writeFile(path, streamOf(', appended'), {
baseDir,
append: true
})
return {
overwritten,
appended: await api.fs.readTextFile(path, { baseDir })
}
}, `${dir}/stream.txt`)
expect(result).toEqual({
overwritten: 'short stream',
appended: 'short stream, appended'
})
})
it('stat, lstat and size describe files and directories', async () => {
const result = await tauri(async (api, dir) => {
const baseDir = api.fs.BaseDirectory.AppData