bun icon indicating copy to clipboard operation
bun copied to clipboard

`path.win32.parse` gives incorrect data

Open jhmaster2000 opened this issue 3 years ago • 2 comments

Version

0.1.3

Platform

Linux DESKTOP-A7ND7QS 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

What steps will reproduce the bug?

Take the following sample.ts file and run with bun run sample.ts:

import path from 'path';

function WSLSafePath(patharg: string): string {
    if (!process.env.WSL_DISTRO_NAME) return patharg; // Comment out this line to test outside of WSL
    const parsed = path.win32.parse(patharg);
    console.dir(parsed);
    return path.posix.join('/mnt/', parsed.root[0]!.toLowerCase(), parsed.dir.slice(parsed.root.length), parsed.base);
}

const FILEPATH: string = WSLSafePath('C:/foo/bar/baz/myfile.jpeg');
console.log(FILEPATH);

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

Output from ts-node-esm sample.ts:

{
  root: 'Q:/',
  dir: 'Q:/foo/bar/baz',
  base: 'myfile.jpeg',
  ext: '.jpeg',
  name: 'myfile'
}
/mnt/q/foo/bar/baz/myfile.jpeg

What do you see instead?

Output from bun run sample.ts:

myfile.jpeg { dir: "Q:/foo/bar/baz", root: "", base: "myfile", name: "myfile.jpeg", ext: ".jpeg" }

Followed by a TypeError for trying to access toLowerCase of root[0] which is undefined due to root being an empty string.

Additional information

parsed.dir -> OK parsed.ext -> OK parsed.root -> Empty when it shouldn't be (Expected drive letter) parsed.base -> Incorrectly swapped with parsed.name parsed.name -> Incorrectly swapped with parsed.base

jhmaster2000 avatar Jul 12 '22 21:07 jhmaster2000

path/win32 is not implemented/tested yet

PRs welcome!

Jarred-Sumner avatar Jul 13 '22 05:07 Jarred-Sumner

FWIW I've sort of reimplemented/ported these in a standalone module: https://github.com/fabiospampinato/nanopath

It's tested with Node's test suite, it seems to work but I wouldn't bet my life that it's perfect, too many regexes.

fabiospampinato avatar Dec 06 '22 17:12 fabiospampinato

fixed on #6938

paperclover avatar Dec 06 '23 06:12 paperclover