Skip to content

Commit

Permalink
Added code to clean up the temp directory that pyright creates when d…
Browse files Browse the repository at this point in the history
…etermining whether the file system is case sensitive. This addresses #4142.
  • Loading branch information
msfterictraut committed Nov 5, 2022
1 parent 5334197 commit c5d1cfc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/pyright-internal/src/common/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface TmpfileOptions {
export interface FileSystem {
existsSync(path: string): boolean;
mkdirSync(path: string, options?: MkDirOptions): void;
rmdirSync(path: string): void;
chdir(path: string): void;
readdirEntriesSync(path: string): fs.Dirent[];
readdirSync(path: string): string[];
Expand Down
26 changes: 21 additions & 5 deletions packages/pyright-internal/src/common/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,15 +940,18 @@ export function isFileSystemCaseSensitive(fs: FileSystem) {
}

export function isFileSystemCaseSensitiveInternal(fs: FileSystem) {
let filePath: string | undefined = undefined;
let filePath: string | undefined;
let tempDirPath: string | undefined;

try {
// Make unique file name.
let name: string;
let mangledFilePath: string;
do {
name = `${randomBytesHex(21)}-a`;
filePath = path.join(fs.tmpdir(), name);
mangledFilePath = path.join(fs.tmpdir(), name.toUpperCase());
tempDirPath = fs.tmpdir();
filePath = path.join(tempDirPath, name);
mangledFilePath = path.join(tempDirPath, name.toUpperCase());
} while (fs.existsSync(filePath) || fs.existsSync(mangledFilePath));

fs.writeFileSync(filePath, '', 'utf8');
Expand All @@ -959,8 +962,21 @@ export function isFileSystemCaseSensitiveInternal(fs: FileSystem) {
return false;
} finally {
if (filePath) {
// remove temp file created
fs.unlinkSync(filePath);
try {
// Remove temp file.
fs.unlinkSync(filePath);
} catch (e: any) {
// Ignore exception.
}
}

if (tempDirPath) {
try {
// Remove temp directory.
fs.rmdirSync(tempDirPath);
} catch (e: any) {
// Ignore exception.
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/common/realFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ class RealFileSystem implements FileSystem {
yarnFS.mkdirSync(path, options);
}

rmdirSync(path: string) {
yarnFS.rmdirSync(path);
}

chdir(path: string) {
process.chdir(path);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/pyrightFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class PyrightFileSystem
this._realFS.mkdirSync(path, options);
}

override rmdirSync(path: string): void {
this._realFS.rmdirSync(path);
}

override chdir(path: string): void {
this._realFS.chdir(path);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/readonlyAugmentedFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class ReadOnlyAugmentedFileSystem implements FileSystem {
throw new Error('Operation is not allowed.');
}

rmdirSync(path: string): void {
throw new Error('Operation is not allowed.');
}

chdir(path: string): void {
throw new Error('Operation is not allowed.');
}
Expand Down

0 comments on commit c5d1cfc

Please sign in to comment.