memfs icon indicating copy to clipboard operation
memfs copied to clipboard

realpath problems on windows

Open noamokman opened this issue 6 years ago • 2 comments

Hey, I'm having problems with windows and the realpath function. It seems that the realpath function always returns the path in a unix format and not windows.

How to reproduce (on windows):

const {resolve} = require('path');
const {vol} = require('memfs');

process.chdir('/');

vol.writeFileSync('package.json', '');

const resolvedPath = resolve('package.json');
const realPath = vol.realpathSync(resolvedPath);

console.log(realPath); // '/package.json'
console.log(resolvedPath); // 'D:\\package.json'

What can I do?

noamokman avatar Feb 02 '19 10:02 noamokman

You can actually manually select what path format the path API uses by using the win32 & posix path properties to call path methods:

path.posix.resolve

I would recommend doing this w/ memfs, as it will clear up a lot of problems, and make everything nicely consistent.

Otherwise, there has been talk on "proper" win32 support via #327 - I've not had a proper read, but I might be willing to make a PR if the proposed solution is all that's needed.

Regardless, personally I found switching to using .posix resolved all my path problems; the Node fs API accepts posixs path correctly regardless of OS :)

G-Rath avatar Jul 17 '19 02:07 G-Rath

@G-Rath The problem I have is that memfs cuts out the drive letter and in this example (and in my case), it is not the default drive C:/ which gets accessed. And Windows automatically thinks that a path without a drive letter should get resolved to C:/. Am I understanding something wrong or is there a way around that?

Edit: Also how can I use posix.resolve with external drives? Edit2: Replacing resolve with join seems like a good way to deal with resolve. I have not found a solution for the drive letters and memfs though.

Okay, using join also works perfectly with memfs. Thank you also for creating this awesome library!

vdawg-git avatar Jul 09 '22 10:07 vdawg-git