Skip to content

Commit

Permalink
feat: remove sync fs access from siw
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Aug 4, 2023
1 parent 42c57e1 commit 186037c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -957,10 +957,10 @@ describe('ripgrep-search-in-workspace-server', function (): void {

describe('#extractSearchPathsFromIncludes', function (): void {
this.timeout(10000);
it('should not resolve paths from a not absolute / relative pattern', function (): void {
it('should not resolve paths from a not absolute / relative pattern', async () => {
const pattern = 'carrots';
const options = { include: [pattern] };
const searchPaths = ripgrepServer['extractSearchPathsFromIncludes']([rootDirA], options);
const searchPaths = await ripgrepServer['extractSearchPathsFromIncludes']([rootDirA], options);
// Same root directory
expect(searchPaths.length).equal(1);
expect(searchPaths[0]).equal(rootDirA);
Expand All @@ -970,21 +970,21 @@ describe('#extractSearchPathsFromIncludes', function (): void {
expect(options.include[0]).equals(pattern);
});

it('should resolve pattern to path for relative filename', function (): void {
it('should resolve pattern to path for relative filename', async () => {
const filename = 'carrots';
const pattern = `./${filename}`;
checkResolvedPathForPattern(pattern, path.join(rootDirA, filename));
await checkResolvedPathForPattern(pattern, path.join(rootDirA, filename));
});

it('should resolve relative pattern with sub-folders glob', function (): void {
it('should resolve relative pattern with sub-folders glob', async () => {
const filename = 'carrots';
const pattern = `./${filename}/**`;
checkResolvedPathForPattern(pattern, path.join(rootDirA, filename));
await checkResolvedPathForPattern(pattern, path.join(rootDirA, filename));
});

it('should resolve absolute path pattern', function (): void {
it('should resolve absolute path pattern', async () => {
const pattern = `${rootDirA}/carrots`;
checkResolvedPathForPattern(pattern, pattern);
await checkResolvedPathForPattern(pattern, pattern);
});
});

Expand Down Expand Up @@ -1064,9 +1064,9 @@ describe('#addGlobArgs', function (): void {
});
});

function checkResolvedPathForPattern(pattern: string, expectedPath: string): void {
async function checkResolvedPathForPattern(pattern: string, expectedPath: string): Promise<void> {
const options = { include: [pattern] };
const searchPaths = ripgrepServer['extractSearchPathsFromIncludes']([rootDirA], options);
const searchPaths = await ripgrepServer['extractSearchPathsFromIncludes']([rootDirA], options);
expect(searchPaths.length).equal(1, 'searchPath result should contain exactly one element');
expect(options.include.length).equals(0, 'options.include should be empty');
expect(searchPaths[0]).equal(path.normalize(expectedPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class RipgrepSearchInWorkspaceServer implements SearchInWorkspaceServer {
const rootPaths = rootUris.map(root => FileUri.fsPath(root));
// If there are absolute paths in `include` we will remove them and use
// those as paths to search from.
const searchPaths = this.extractSearchPathsFromIncludes(rootPaths, options);
const searchPaths = await this.extractSearchPathsFromIncludes(rootPaths, options);
options.include = this.replaceRelativeToAbsolute(searchPaths, options.include);
options.exclude = this.replaceRelativeToAbsolute(searchPaths, options.exclude);
const rgArgs = this.getArgs(options);
Expand Down Expand Up @@ -388,23 +388,28 @@ export class RipgrepSearchInWorkspaceServer implements SearchInWorkspaceServer {
* Any pattern that resulted in a valid search path will be removed from the 'include' list as it is
* provided as an equivalent search path instead.
*/
protected extractSearchPathsFromIncludes(rootPaths: string[], options: SearchInWorkspaceOptions): string[] {
protected async extractSearchPathsFromIncludes(rootPaths: string[], options: SearchInWorkspaceOptions): Promise<string[]> {
if (!options.include) {
return rootPaths;
}
const resolvedPaths = new Set<string>();
options.include = options.include.filter(pattern => {
const include: string[] = [];
for (const pattern of options.include) {
let keep = true;
for (const root of rootPaths) {
const absolutePath = this.getAbsolutePathFromPattern(root, pattern);
const absolutePath = await this.getAbsolutePathFromPattern(root, pattern);
// undefined means the pattern cannot be converted into an absolute path
if (absolutePath) {
resolvedPaths.add(absolutePath);
keep = false;
}
}
return keep;
});
if (keep) {
include.push(pattern);
}

}
options.include = include;
return resolvedPaths.size > 0
? Array.from(resolvedPaths)
: rootPaths;
Expand All @@ -417,7 +422,7 @@ export class RipgrepSearchInWorkspaceServer implements SearchInWorkspaceServer {
*
* @returns undefined if the pattern cannot be converted into an absolute path.
*/
protected getAbsolutePathFromPattern(root: string, pattern: string): string | undefined {
protected async getAbsolutePathFromPattern(root: string, pattern: string): Promise<string | undefined> {
pattern = pattern.replace(/\\/g, '/');
// The pattern is not referring to a single file or folder, i.e. not to be converted
if (!path.isAbsolute(pattern) && !pattern.startsWith('./')) {
Expand All @@ -429,7 +434,7 @@ export class RipgrepSearchInWorkspaceServer implements SearchInWorkspaceServer {
}
// if `pattern` is absolute then `root` will be ignored by `path.resolve()`
const targetPath = path.resolve(root, pattern);
if (fs.existsSync(targetPath)) {
if (await fs.pathExists(targetPath)) {
return targetPath;
}
return undefined;
Expand Down

0 comments on commit 186037c

Please sign in to comment.