Merge commit from fork

This commit is contained in:
Ben Lambert
2026-01-20 16:04:33 +01:00
committed by GitHub
parent 6a36e1e06f
commit ae4dd5d157
4 changed files with 135 additions and 17 deletions
+47 -2
View File
@@ -14,7 +14,49 @@
* limitations under the License.
*/
import { relative, isAbsolute } from 'path';
import {
relative,
isAbsolute,
resolve as resolvePath,
dirname,
basename,
} from 'path';
import { realpathSync, lstatSync, readlinkSync } from 'fs';
// Resolves a path to its real location, following symlinks.
// Handles cases where the final target doesn't exist by recursively
// resolving parent directories.
function resolveRealPath(path: string): string {
try {
return realpathSync(path);
} catch (ex) {
if (ex.code !== 'ENOENT') {
throw ex;
}
}
// Check if path itself is a dangling symlink - recursively resolve the target
// to handle symlink chains (e.g., link1 -> link2 -> /outside)
try {
if (lstatSync(path).isSymbolicLink()) {
const target = resolvePath(dirname(path), readlinkSync(path));
return resolveRealPath(target);
}
} catch (ex) {
if (ex.code !== 'ENOENT') {
throw ex;
}
}
// Path doesn't exist - walk up the tree until we find an existing path,
// resolve it, then rebuild the non-existent portion on top
const parent = dirname(path);
if (parent === path) {
return path; // Hit filesystem root
}
return resolvePath(resolveRealPath(parent), basename(path));
}
/**
* Checks if path is the same as or a child path of base.
@@ -22,7 +64,10 @@ import { relative, isAbsolute } from 'path';
* @public
*/
export function isChildPath(base: string, path: string): boolean {
const relativePath = relative(base, path);
const resolvedBase = resolveRealPath(base);
const resolvedPath = resolveRealPath(path);
const relativePath = relative(resolvedBase, resolvedPath);
if (relativePath === '') {
// The same directory
return true;