Skip to content

Commit e612764

Browse files
authored
Move findPackRoot to ql.ts (#3268)
1 parent e834d32 commit e612764

3 files changed

Lines changed: 29 additions & 27 deletions

File tree

extensions/ql-vscode/src/common/ql.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from "path";
1+
import { dirname, join, parse } from "path";
22
import { pathExists } from "fs-extra";
33

44
export const QLPACK_FILENAMES = ["qlpack.yml", "codeql-pack.yml"];
@@ -27,3 +27,28 @@ export async function getQlPackFilePath(
2727

2828
return undefined;
2929
}
30+
31+
/**
32+
* Recursively find the directory containing qlpack.yml or codeql-pack.yml. If
33+
* no such directory is found, the directory containing the query file is returned.
34+
* @param queryFile The query file to start from.
35+
* @returns The path to the pack root.
36+
*/
37+
export async function findPackRoot(queryFile: string): Promise<string> {
38+
let dir = dirname(queryFile);
39+
while (!(await getQlPackFilePath(dir))) {
40+
dir = dirname(dir);
41+
if (isFileSystemRoot(dir)) {
42+
// there is no qlpack.yml or codeql-pack.yml in this directory or any parent directory.
43+
// just use the query file's directory as the pack root.
44+
return dirname(queryFile);
45+
}
46+
}
47+
48+
return dir;
49+
}
50+
51+
function isFileSystemRoot(dir: string): boolean {
52+
const pathObj = parse(dir);
53+
return pathObj.root === dir && pathObj.base === "";
54+
}

extensions/ql-vscode/src/variant-analysis/run-remote-query.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CancellationToken } from "vscode";
22
import { Uri, window } from "vscode";
3-
import { relative, join, sep, dirname, parse, basename } from "path";
3+
import { relative, join, sep, basename } from "path";
44
import { dump, load } from "js-yaml";
55
import { copy, writeFile, readFile, mkdirp } from "fs-extra";
66
import type { DirectoryResult } from "tmp-promise";
@@ -262,26 +262,6 @@ async function copyExistingQueryPack(
262262
await fixPackFile(queryPackDir, packRelativePath);
263263
}
264264

265-
export async function findPackRoot(queryFile: string): Promise<string> {
266-
// recursively find the directory containing qlpack.yml or codeql-pack.yml
267-
let dir = dirname(queryFile);
268-
while (!(await getQlPackFilePath(dir))) {
269-
dir = dirname(dir);
270-
if (isFileSystemRoot(dir)) {
271-
// there is no qlpack.yml or codeql-pack.yml in this directory or any parent directory.
272-
// just use the query file's directory as the pack root.
273-
return dirname(queryFile);
274-
}
275-
}
276-
277-
return dir;
278-
}
279-
280-
function isFileSystemRoot(dir: string): boolean {
281-
const pathObj = parse(dir);
282-
return pathObj.root === dir && pathObj.base === "";
283-
}
284-
285265
interface RemoteQueryTempDir {
286266
remoteQueryDir: DirectoryResult;
287267
queryPackDir: string;

extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ import type {
4242
LoadResultsOptions,
4343
VariantAnalysisResultsManager,
4444
} from "./variant-analysis-results-manager";
45-
import {
46-
findPackRoot,
47-
getQueryName,
48-
prepareRemoteQueryRun,
49-
} from "./run-remote-query";
45+
import { getQueryName, prepareRemoteQueryRun } from "./run-remote-query";
5046
import {
5147
mapVariantAnalysis,
5248
mapVariantAnalysisRepositoryTask,
@@ -94,6 +90,7 @@ import { handleRequestError } from "./custom-errors";
9490
import { createMultiSelectionCommand } from "../common/vscode/selection-commands";
9591
import { askForLanguage } from "../codeql-cli/query-language";
9692
import type { QlPackDetails } from "./ql-pack-details";
93+
import { findPackRoot } from "../common/ql";
9794

9895
const maxRetryCount = 3;
9996

0 commit comments

Comments
 (0)