Skip to content

Commit

Permalink
fix(TreeWalker): handle large directory structures safely
Browse files Browse the repository at this point in the history
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
sleistner committed Aug 27, 2019
1 parent 876c99f commit c419c78
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 60 deletions.
9 changes: 8 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ tsconfig.json
vsc-extension-quickstart.md
tsd.json
*.vsix
releases/**
releases/**
yarn-error.log
yarn.lock
.releaserc
tslint.json
.devcontainer/**
.github/**
node_modules/**
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ Nonexistent folders are created automatically.
"type": "boolean",
"default": true,
"description": "Controls if directory selector should be shown."
},
"fileutils.typeahead.exclude": {
"type": "object",
"default": {},
"description": "Configure glob patterns for excluding files and folders."
}
}
```
Expand Down
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-fileutils",
"displayName": "File Utils",
"description": "A convenient way of creating, duplicating, moving, renaming and deleting files and directories.",
"version": "2.14.9",
"version": "3.0.0-beta",
"private": true,
"license": "MIT",
"publisher": "sleistner",
Expand Down Expand Up @@ -164,11 +164,6 @@
"type": "boolean",
"default": true,
"description": "Controls if directory selector should be shown."
},
"fileutils.typeahead.exclude": {
"type": "object",
"default": {},
"description": "Configure glob patterns for excluding files and folders."
}
}
}
Expand All @@ -190,7 +185,6 @@
"@types/bluebird": "^3.5.20",
"@types/bluebird-retry": "0.11.4",
"@types/chai": "^4.2.0",
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.6",
"@types/node": "^10.12.21",
"@types/sinon": "^7.0.3",
Expand All @@ -210,9 +204,7 @@
"typescript": "^3.5.3",
"vscode-test": "^1.2.0"
},
"dependencies": {
"glob": "^7.1.2"
},
"dependencies": {},
"pre-commit": [
"lint",
"test"
Expand Down
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {
RemoveFileController
} from './controller';

function handleError(err: any) {
if (err) {
vscode.window.showErrorMessage(err);
function handleError(err: Error) {
if (err && err.message) {
vscode.window.showErrorMessage(err.message);
}
return err;
}
Expand Down
44 changes: 27 additions & 17 deletions src/lib/TreeWalker.ts
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();
}
}
42 changes: 19 additions & 23 deletions test/index.ts
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);
}
});
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ glob@7.1.3:
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==
Expand Down

0 comments on commit c419c78

Please sign in to comment.