async validatePath(filePath?: string): Promise<void> {
if (filePath === undefined) return;
const roots = this.roots();
if (roots === undefined) return;
const abs = path.resolve(filePath);
let canonical;
try {
canonical = await fs.realpath(abs);
} catch (err) {
if (err.code === 'ENOENT') {
const parent = await fs.realpath(path.dirname(abs));
canonical = path.join(parent, path.basename(abs));
} else {
throw err;
}
}
for (const root of roots) {
const canonicalRoot = await fs.realpath(fileURLToPath(root.uri));
if (
canonical === canonicalRoot ||
canonical.startsWith(canonicalRoot + path.sep)
) {
return;
}
}
throw new Error(
`Access denied: ${filePath} (canonical: ${canonical}) is not within any workspace root.`,
);
}