Skip to content

Commit 7d6461a

Browse files
committed
Handle codeql-pack.yml files everywhere
This will add support for the `codeql-pack.yml` filename in all places where we currently support `qlpack.yml`. It centralizes the list of the supported filenames in a single place and a method that can figure out the correct filename to use.
1 parent 81502fe commit 7d6461a

5 files changed

Lines changed: 60 additions & 30 deletions

File tree

extensions/ql-vscode/src/contextual/queryResolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getOnDiskWorkspaceFolders,
1010
QlPacksForLanguage,
1111
showAndLogExceptionWithTelemetry,
12+
QLPACK_FILENAMES,
1213
} from "../helpers";
1314
import { KeyType, kindOfKeyType, nameOfKeyType, tagOfKeyType } from "./keyType";
1415
import { CodeQLCliServer } from "../cli";
@@ -113,7 +114,7 @@ async function resolveContextualQuery(
113114
// Work out the enclosing pack.
114115
const packContents = await cli.packPacklist(query, false);
115116
const packFilePath = packContents.find((p) =>
116-
["codeql-pack.yml", "qlpack.yml"].includes(basename(p)),
117+
QLPACK_FILENAMES.includes(basename(p)),
117118
);
118119
if (packFilePath === undefined) {
119120
// Should not happen; we already resolved this query.

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ async function activateWithInstalledDistribution(
845845
documentSelector: [
846846
{ language: "ql", scheme: "file" },
847847
{ language: "yaml", scheme: "file", pattern: "**/qlpack.yml" },
848+
{ language: "yaml", scheme: "file", pattern: "**/codeql-pack.yml" },
848849
],
849850
synchronize: {
850851
configurationSection: "codeQL",

extensions/ql-vscode/src/helpers.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,22 @@ async function findDbschemePack(
387387
): Promise<{ name: string; isLibraryPack: boolean }> {
388388
for (const { packDir, packName } of packs) {
389389
if (packDir !== undefined) {
390-
const qlpack = load(
391-
await readFile(join(packDir, "qlpack.yml"), "utf8"),
392-
) as { dbscheme?: string; library?: boolean };
393-
if (
394-
qlpack.dbscheme !== undefined &&
395-
basename(qlpack.dbscheme) === basename(dbschemePath)
396-
) {
397-
return {
398-
name: packName,
399-
isLibraryPack: qlpack.library === true,
390+
const qlpackPath = await getQlPackPath(packDir);
391+
392+
if (qlpackPath !== undefined) {
393+
const qlpack = load(await readFile(qlpackPath, "utf8")) as {
394+
dbscheme?: string;
395+
library?: boolean;
400396
};
397+
if (
398+
qlpack.dbscheme !== undefined &&
399+
basename(qlpack.dbscheme) === basename(dbschemePath)
400+
) {
401+
return {
402+
name: packName,
403+
isLibraryPack: qlpack.library === true,
404+
};
405+
}
401406
}
402407
}
403408
}
@@ -737,3 +742,20 @@ export async function* walkDirectory(
737742
}
738743
}
739744
}
745+
746+
export const QLPACK_FILENAMES = ["qlpack.yml", "codeql-pack.yml"];
747+
export const FALLBACK_QLPACK_FILENAME = QLPACK_FILENAMES[0];
748+
749+
export async function getQlPackPath(
750+
packRoot: string,
751+
): Promise<string | undefined> {
752+
for (const filename of QLPACK_FILENAMES) {
753+
const path = join(packRoot, filename);
754+
755+
if (await pathExists(path)) {
756+
return path;
757+
}
758+
}
759+
760+
return undefined;
761+
}

extensions/ql-vscode/src/quick-query.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import { LSPErrorCodes, ResponseError } from "vscode-languageclient";
1212
import { CodeQLCliServer } from "./cli";
1313
import { DatabaseUI } from "./databases-ui";
1414
import {
15+
FALLBACK_QLPACK_FILENAME,
1516
getInitialQueryContents,
1617
getPrimaryDbscheme,
1718
getQlPackForDbscheme,
19+
getQlPackPath,
1820
showBinaryChoiceDialog,
1921
} from "./helpers";
2022
import { ProgressCallback, UserCancellationException } from "./commandRunner";
@@ -112,7 +114,7 @@ export async function displayQuickQuery(
112114
const dbscheme = await getPrimaryDbscheme(datasetFolder);
113115
const qlpack = (await getQlPackForDbscheme(cliServer, dbscheme))
114116
.dbschemePack;
115-
const qlPackFile = join(queriesDir, "qlpack.yml");
117+
const qlPackFile = await getQlPackPath(queriesDir);
116118
const qlFile = join(queriesDir, QUICK_QUERY_QUERY_NAME);
117119
const shouldRewrite = await checkShouldRewrite(qlPackFile, qlpack);
118120

@@ -126,7 +128,7 @@ export async function displayQuickQuery(
126128
},
127129
};
128130
await writeFile(
129-
qlPackFile,
131+
qlPackFile ?? join(queriesDir, FALLBACK_QLPACK_FILENAME),
130132
QLPACK_FILE_HEADER + dump(quickQueryQlpackYaml),
131133
"utf8",
132134
);
@@ -158,7 +160,13 @@ export async function displayQuickQuery(
158160
}
159161
}
160162

161-
async function checkShouldRewrite(qlPackFile: string, newDependency: string) {
163+
async function checkShouldRewrite(
164+
qlPackFile: string | undefined,
165+
newDependency: string,
166+
) {
167+
if (!qlPackFile) {
168+
return true;
169+
}
162170
if (!(await pathExists(qlPackFile))) {
163171
return true;
164172
}

extensions/ql-vscode/src/remote-queries/run-remote-query.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { CancellationToken, Uri, window } from "vscode";
22
import { relative, join, sep, dirname, parse, basename } from "path";
33
import { dump, load } from "js-yaml";
4-
import { pathExists, copy, writeFile, readFile, mkdirp } from "fs-extra";
4+
import { copy, writeFile, readFile, mkdirp } from "fs-extra";
55
import { dir, tmpName } from "tmp-promise";
66
import {
77
askForLanguage,
88
findLanguage,
99
getOnDiskWorkspaceFolders,
1010
tryGetQueryMetadata,
1111
tmpDir,
12+
FALLBACK_QLPACK_FILENAME,
13+
getQlPackPath,
14+
QLPACK_FILENAMES,
1215
} from "../helpers";
1316
import { Credentials } from "../common/authentication";
1417
import * as cli from "../cli";
@@ -67,7 +70,7 @@ async function generateQueryPack(
6770
const targetQueryFileName = join(queryPackDir, packRelativePath);
6871

6972
let language: string | undefined;
70-
if (await getExistingPackFile(originalPackRoot)) {
73+
if (await getQlPackPath(originalPackRoot)) {
7174
// don't include ql files. We only want the queryFile to be copied.
7275
const toCopy = await cliServer.packPacklist(originalPackRoot, false);
7376

@@ -120,7 +123,10 @@ async function generateQueryPack(
120123
},
121124
defaultSuite: generateDefaultSuite(packRelativePath),
122125
};
123-
await writeFile(join(queryPackDir, "qlpack.yml"), dump(syntheticQueryPack));
126+
await writeFile(
127+
join(queryPackDir, FALLBACK_QLPACK_FILENAME),
128+
dump(syntheticQueryPack),
129+
);
124130
}
125131
if (!language) {
126132
throw new UserCancellationException("Could not determine language.");
@@ -163,7 +169,7 @@ async function generateQueryPack(
163169
async function findPackRoot(queryFile: string): Promise<string> {
164170
// recursively find the directory containing qlpack.yml
165171
let dir = dirname(queryFile);
166-
while (!(await getExistingPackFile(dir))) {
172+
while (!(await getQlPackPath(dir))) {
167173
dir = dirname(dir);
168174
if (isFileSystemRoot(dir)) {
169175
// there is no qlpack.yml in this directory or any parent directory.
@@ -175,16 +181,6 @@ async function findPackRoot(queryFile: string): Promise<string> {
175181
return dir;
176182
}
177183

178-
async function getExistingPackFile(dir: string) {
179-
if (await pathExists(join(dir, "qlpack.yml"))) {
180-
return join(dir, "qlpack.yml");
181-
}
182-
if (await pathExists(join(dir, "codeql-pack.yml"))) {
183-
return join(dir, "codeql-pack.yml");
184-
}
185-
return undefined;
186-
}
187-
188184
function isFileSystemRoot(dir: string): boolean {
189185
const pathObj = parse(dir);
190186
return pathObj.root === dir && pathObj.base === "";
@@ -319,12 +315,14 @@ async function fixPackFile(
319315
queryPackDir: string,
320316
packRelativePath: string,
321317
): Promise<void> {
322-
const packPath = await getExistingPackFile(queryPackDir);
318+
const packPath = await getQlPackPath(queryPackDir);
323319

324320
// This should not happen since we create the pack ourselves.
325321
if (!packPath) {
326322
throw new Error(
327-
`Could not find qlpack.yml or codeql-pack.yml file in '${queryPackDir}'`,
323+
`Could not find ${QLPACK_FILENAMES.join(
324+
" or ",
325+
)} file in '${queryPackDir}'`,
328326
);
329327
}
330328
const qlpack = load(await readFile(packPath, "utf8")) as QlPack;

0 commit comments

Comments
 (0)