Skip to content

Commit

Permalink
Make calls to realpath safe, like stat (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Mar 29, 2021
1 parent 99bb961 commit 75e43b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/pyright-internal/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
resolvePaths,
stripFileExtension,
stripTrailingDirectorySeparator,
tryRealpath,
tryStat,
} from '../common/pathUtils';
import { equateStringsCaseInsensitive } from '../common/stringUtils';
Expand Down Expand Up @@ -505,8 +506,8 @@ export class ImportResolver {
}

if (entry?.isSymbolicLink()) {
const realPath = this.fileSystem.realpathSync(path);
if (this.fileSystem.existsSync(realPath) && isFile(this.fileSystem, realPath)) {
const realPath = tryRealpath(this.fileSystem, path);
if (realPath && this.fileSystem.existsSync(realPath) && isFile(this.fileSystem, realPath)) {
return true;
}
}
Expand All @@ -531,8 +532,8 @@ export class ImportResolver {
}

if (entry?.isSymbolicLink()) {
const realPath = this.fileSystem.realpathSync(path);
if (this.fileSystem.existsSync(realPath) && isDirectory(this.fileSystem, realPath)) {
const realPath = tryRealpath(this.fileSystem, path);
if (realPath && this.fileSystem.existsSync(realPath) && isDirectory(this.fileSystem, realPath)) {
return true;
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
isDirectory,
normalizePath,
stripFileExtension,
tryRealpath,
tryStat,
} from '../common/pathUtils';
import { DocumentRange, Position, Range } from '../common/textRange';
Expand Down Expand Up @@ -1009,7 +1010,12 @@ export class AnalyzerService {

const seenDirs = new Set<string>();
const visitDirectory = (absolutePath: string, includeRegExp: RegExp) => {
const realDirPath = this._fs.realpathSync(absolutePath);
const realDirPath = tryRealpath(this._fs, absolutePath);
if (!realDirPath) {
this._console.warn(`Skipping broken link "${absolutePath}"`);
return;
}

if (seenDirs.has(realDirPath)) {
this._console.warn(`Skipping recursive symlink "${absolutePath}" -> "${realDirPath}"`);
return;
Expand Down
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/common/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ export function tryStat(fs: FileSystem, path: string): Stats | undefined {
}
}

export function tryRealpath(fs: FileSystem, path: string): string | undefined {
try {
return fs.realpathSync(path);
} catch (e) {
return undefined;
}
}

export function getFileSystemEntries(fs: FileSystem, path: string): FileSystemEntries {
try {
return getFileSystemEntriesFromDirEntries(fs.readdirEntriesSync(path || '.'), fs, path);
Expand Down

0 comments on commit 75e43b3

Please sign in to comment.