Skip to content

readFile not working with URL instance with file: protocol in Windows.Β #1098

@miguel-leon

Description

@miguel-leon

Given the real dir structure

C:/
└── src/
    └── file.txt

The following program without memfs

import { resolve } from 'path';
import { pathToFileURL } from 'url';
import { readdirSync, readFileSync } from 'fs';

console.log(resolve('/src'));
console.log(readdirSync(resolve('/src')));
console.log('string path:', readFileSync(resolve('/src/file.txt'), 'utf8'));
console.log('   file URL:', readFileSync(pathToFileURL(resolve('/src/file.txt')), 'utf8'));

Prints:

C:\src
[ 'file.txt' ]
string path: Hello from file!
   file URL: Hello from file!

Using memfs, the equivalent program

import { resolve } from 'path';
import { pathToFileURL } from 'url';
import { fs, vol } from 'memfs';

vol.fromJSON({
	'file.txt': 'Hello from fake file!'
}, resolve('/src'));

console.log(resolve('/src'));
console.log(fs.readdirSync(resolve('/src')));
console.log('string path:', fs.readFileSync(resolve('/src/file.txt'), 'utf8'));
console.log('   file URL:', fs.readFileSync(pathToFileURL(resolve('/src/file.txt')), 'utf8'));

Prints and throws:

C:\src
[ 'file.txt' ]
string path: Hello from fake file!
Error: ENOENT: no such file or directory, open '/C:/src/file.txt'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions