fix(core): resolve symlinks for non-existent paths during validation (#21487)

This commit is contained in:
Adib234
2026-03-09 12:02:13 -04:00
committed by GitHub
parent 35ee2a841a
commit 7837194ab5
5 changed files with 55 additions and 56 deletions
+2 -28
View File
@@ -4,10 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { isNodeError } from '../utils/errors.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { debugLogger } from './debugLogger.js';
import { resolveToRealPath } from './paths.js';
export type Unsubscribe = () => void;
@@ -227,22 +227,7 @@ export class WorkspaceContext {
* if it did exist.
*/
private fullyResolvedPath(pathToCheck: string): string {
try {
return fs.realpathSync(path.resolve(this.targetDir, pathToCheck));
} catch (e: unknown) {
if (
isNodeError(e) &&
e.code === 'ENOENT' &&
e.path &&
// realpathSync does not set e.path correctly for symlinks to
// non-existent files.
!this.isFileSymlink(e.path)
) {
// If it doesn't exist, e.path contains the fully resolved path.
return e.path;
}
throw e;
}
return resolveToRealPath(path.resolve(this.targetDir, pathToCheck));
}
/**
@@ -262,15 +247,4 @@ export class WorkspaceContext {
!path.isAbsolute(relative)
);
}
/**
* Checks if a file path is a symbolic link that points to a file.
*/
private isFileSymlink(filePath: string): boolean {
try {
return !fs.readlinkSync(filePath).endsWith('/');
} catch (_error) {
return false;
}
}
}