unionfs icon indicating copy to clipboard operation
unionfs copied to clipboard

`ufs.createWriteStream` fails when folder exists but file does not

Open iamogbz opened this issue 5 years ago • 1 comments

Description

Creating a folder and then writing directly to a file in the folder is a normal usecase that unionfs does not support.

Steps to reproduce

With file

const fs = require('fs')

const { vol } = require('memfs')
const { ufs } = require('unionfs')

ufs.use(vol).use(fs)

const runExampleWith = fsInstance => {
    try {
        fsInstance.mkdirSync('example', { recursive: true })
        const stream = fsInstance.createWriteStream('example/file.txt')
        stream.write('test', () => stream.end())
    } finally {
        fsInstance.rmdirSync('example', { recursive: true })
    }
}

Using the default node fs works

runExampleWith(fs)

Using memfs directly works

runExampleWith(vol)

Using the combined ufs fails with

runExampleWith(ufs)
// Error: ENOENT: no such file or directory, open 'example/file.txt'

Cause

Union fs checks file exists before attempting to use the file systems

https://github.com/streamich/unionfs/blob/302e21a10e12e1854ce92a300ee5dfe9f939a8e8/src/union.ts#L307

Suggestion

import {dirname} from 'path' 
// ...
fs.statSync(dirname(path)); //we simply stat first to exit early for mocked fs'es

Related

  • #384

iamogbz avatar Mar 16 '20 21:03 iamogbz

I've just encountered the problem.

ddziara avatar Nov 16 '20 20:11 ddziara

:tada: This issue has been resolved in version 4.5.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Mar 19 '24 18:03 github-actions[bot]