Skip to content

Commit bd42faf

Browse files
sehoon38ruomengz
authored andcommitted
fix(core): handle EISDIR in robustRealpath on Windows (#21984)
1 parent a2cb026 commit bd42faf

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

packages/core/src/utils/paths.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,22 @@ describe('resolveToRealPath', () => {
528528
expect(resolveToRealPath(input)).toBe(expected);
529529
});
530530

531+
it('should return decoded path even if fs.realpathSync fails with EISDIR', () => {
532+
vi.spyOn(fs, 'realpathSync').mockImplementationOnce(() => {
533+
const err = new Error(
534+
'Illegal operation on a directory',
535+
) as NodeJS.ErrnoException;
536+
err.code = 'EISDIR';
537+
throw err;
538+
});
539+
540+
const p = path.resolve('path', 'to', 'New Project');
541+
const input = pathToFileURL(p).toString();
542+
const expected = p;
543+
544+
expect(resolveToRealPath(input)).toBe(expected);
545+
});
546+
531547
it('should recursively resolve symlinks for non-existent child paths', () => {
532548
const parentPath = path.resolve('/some/parent/path');
533549
const resolvedParentPath = path.resolve('/resolved/parent/path');

packages/core/src/utils/paths.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,12 @@ function robustRealpath(p: string, visited = new Set<string>()): string {
384384
try {
385385
return fs.realpathSync(p);
386386
} catch (e: unknown) {
387-
if (e && typeof e === 'object' && 'code' in e && e.code === 'ENOENT') {
387+
if (
388+
e &&
389+
typeof e === 'object' &&
390+
'code' in e &&
391+
(e.code === 'ENOENT' || e.code === 'EISDIR')
392+
) {
388393
try {
389394
const stat = fs.lstatSync(p);
390395
if (stat.isSymbolicLink()) {
@@ -400,7 +405,7 @@ function robustRealpath(p: string, visited = new Set<string>()): string {
400405
lstatError &&
401406
typeof lstatError === 'object' &&
402407
'code' in lstatError &&
403-
lstatError.code === 'ENOENT'
408+
(lstatError.code === 'ENOENT' || lstatError.code === 'EISDIR')
404409
)
405410
) {
406411
throw lstatError;

0 commit comments

Comments
 (0)