-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(TreeWalker): handle large directory structures safely
Replace all references to "glob" with "vscode.workspace.findFiles" in order to further support remote development and complex directory structures. BREAKING CHANGE: The configuration option "typeahead.exclude" has been removed in favour of VS Code native "files.exclude" option.
- Loading branch information
Showing
7 changed files
with
60 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,37 @@ | ||
import * as fs from 'fs'; | ||
import * as glob from 'glob'; | ||
import * as path from 'path'; | ||
import { workspace } from 'vscode'; | ||
import { getConfiguration } from '../lib/config'; | ||
import { RelativePattern, Uri, workspace } from 'vscode'; | ||
|
||
export class TreeWalker { | ||
|
||
public async directories(sourcePath: string): Promise<string[]> { | ||
const ignore = this.configIgnore(); | ||
const files = glob.sync('**', { cwd: sourcePath, ignore }); | ||
return files | ||
.filter((file) => fs.statSync(path.join(sourcePath, file)).isDirectory()) | ||
.map((file) => path.sep + file); | ||
try { | ||
this.ensureFailSafeFileLookup(); | ||
const pattern = new RelativePattern(sourcePath, '**'); | ||
const files = await workspace.findFiles(pattern, undefined, Number.MAX_VALUE); | ||
const directories = files.reduce(this.directoryReducer(sourcePath), new Set<string>()); | ||
return this.toSortedArray(directories); | ||
} catch (err) { | ||
throw new Error(`Unable to list subdirectories for directory "${sourcePath}". Details: (${err.message})`); | ||
} | ||
} | ||
|
||
private configIgnore(): string[] { | ||
const excludedFilesConfig = { | ||
...getConfiguration('typeahead.exclude'), | ||
...workspace.getConfiguration('files.exclude', null) | ||
private ensureFailSafeFileLookup() { | ||
(process as any).noAsar = true; | ||
} | ||
|
||
private directoryReducer(sourcePath: string) { | ||
return (accumulator: Set<string>, file: Uri) => { | ||
const directory = path.dirname(file.fsPath).replace(sourcePath, ''); | ||
if (directory) { | ||
accumulator.add(directory); | ||
} | ||
return accumulator; | ||
}; | ||
return Object | ||
.keys(excludedFilesConfig) | ||
.filter((key) => excludedFilesConfig[key] === true) | ||
.map(((pattern) => pattern.replace(/^!/, ''))); | ||
} | ||
|
||
private toSortedArray(directories: Set<string>): string[] { | ||
return Array | ||
.from(directories) | ||
.sort(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
import * as glob from 'glob'; | ||
import * as Mocha from 'mocha'; | ||
import * as path from 'path'; | ||
import { RelativePattern, workspace } from 'vscode'; | ||
|
||
export function run(): Promise<void> { | ||
export async function run(): Promise<void> { | ||
const mocha = new Mocha({ | ||
reporter: 'list', | ||
ui: 'bdd', | ||
useColors: true | ||
}); | ||
|
||
const testsRoot = path.resolve(__dirname, '..'); | ||
const pattern = new RelativePattern(testsRoot, '**/**.test.js'); | ||
const files = await workspace.findFiles(pattern, undefined, Number.MAX_VALUE); | ||
|
||
return new Promise((resolve, reject) => { | ||
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
// Add files to the test suite | ||
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f))); | ||
// Add files to the test suite | ||
files.forEach((file) => mocha.addFile(file.fsPath)); | ||
|
||
try { | ||
// Run the mocha test | ||
mocha.run((failures) => { | ||
if (failures > 0) { | ||
reject(new Error(`${failures} tests failed.`)); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
// Run the mocha test | ||
return new Promise((resolve, reject) => { | ||
try { | ||
mocha.run((failures) => { | ||
if (failures > 0) { | ||
reject(new Error(`${failures} tests failed.`)); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1783,7 +1783,7 @@ [email protected]: | |
once "^1.3.0" | ||
path-is-absolute "^1.0.0" | ||
|
||
glob@^7.0.3, glob@^7.0.6, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4: | ||
glob@^7.0.3, glob@^7.0.6, glob@^7.1.1, glob@^7.1.3, glob@^7.1.4: | ||
version "7.1.4" | ||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255" | ||
integrity sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A== | ||
|