Skip to content

Commit 1ad20fa

Browse files
authored
Merge pull request #3163 from github/koesie10/queries-panel-no-cli-server
Do not use the CLI server to determine query pack language
2 parents 870874d + ed4b296 commit 1ad20fa

File tree

15 files changed

+418
-174
lines changed

15 files changed

+418
-174
lines changed

extensions/ql-vscode/scripts/generate-schemas.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import { format, resolveConfig } from "prettier";
66
const extensionDirectory = resolve(__dirname, "..");
77

88
const schemas = [
9+
{
10+
path: join(extensionDirectory, "src", "packaging", "qlpack-file.ts"),
11+
type: "QlPackFile",
12+
schemaPath: join(
13+
extensionDirectory,
14+
"src",
15+
"packaging",
16+
"qlpack-file.schema.json",
17+
),
18+
},
919
{
1020
path: join(
1121
extensionDirectory,

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -738,29 +738,6 @@ export class CodeQLCliServer implements Disposable {
738738
);
739739
}
740740

741-
/**
742-
* Resolve the library path and dbscheme for a query.
743-
* @param workspaces The current open workspaces
744-
* @param queryPath The path to the query
745-
*/
746-
async resolveLibraryPath(
747-
workspaces: string[],
748-
queryPath: string,
749-
silent = false,
750-
): Promise<QuerySetup> {
751-
const subcommandArgs = [
752-
"--query",
753-
queryPath,
754-
...this.getAdditionalPacksArg(workspaces),
755-
];
756-
return await this.runJsonCodeQlCliCommand<QuerySetup>(
757-
["resolve", "library-path"],
758-
subcommandArgs,
759-
"Resolving library paths",
760-
{ silent },
761-
);
762-
}
763-
764741
/**
765742
* Resolves the language for a query.
766743
* @param queryUri The URI of the query

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DisposableObject } from "./disposable-object";
22
import { getErrorMessage } from "./helpers-pure";
3-
import { Logger } from "./logging";
3+
import { BaseLogger } from "./logging";
44

55
/**
66
* Base class for "discovery" operations, which scan the file system to find specific kinds of
@@ -13,7 +13,7 @@ export abstract class Discovery extends DisposableObject {
1313

1414
constructor(
1515
protected readonly name: string,
16-
private readonly logger: Logger,
16+
protected readonly logger: BaseLogger,
1717
) {
1818
super();
1919
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { QueryLanguage } from "./query-language";
2+
import { loadQlpackFile } from "../packaging/qlpack-file-loader";
3+
4+
/**
5+
* @param qlpackPath The path to the `qlpack.yml` or `codeql-pack.yml` file.
6+
* @return the language of the given qlpack file, or undefined if the file is
7+
* not a valid qlpack file or does not contain exactly one language.
8+
*/
9+
export async function getQlPackLanguage(
10+
qlpackPath: string,
11+
): Promise<QueryLanguage | undefined> {
12+
const qlPack = await loadQlpackFile(qlpackPath);
13+
const dependencies = qlPack?.dependencies;
14+
if (!dependencies) {
15+
return;
16+
}
17+
18+
const matchingLanguages = Object.values(QueryLanguage).filter(
19+
(language) => `codeql/${language}-all` in dependencies,
20+
);
21+
if (matchingLanguages.length !== 1) {
22+
return undefined;
23+
}
24+
25+
return matchingLanguages[0];
26+
}

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,7 @@ async function activateWithInstalledDistribution(
800800
);
801801
ctx.subscriptions.push(databaseUI);
802802

803-
const queriesModule = QueriesModule.initialize(
804-
app,
805-
languageContext,
806-
cliServer,
807-
);
803+
const queriesModule = QueriesModule.initialize(app, languageContext);
808804

809805
void extLogger.log("Initializing evaluator log viewer.");
810806
const evalLogViewer = new EvalLogViewer();

extensions/ql-vscode/src/local-queries/skeleton-query-wizard.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ import {
2828
isCodespacesTemplate,
2929
setQlPackLocation,
3030
} from "../config";
31-
import { lstat, pathExists, readFile } from "fs-extra";
31+
import { lstat, pathExists } from "fs-extra";
3232
import { askForLanguage } from "../codeql-cli/query-language";
3333
import { showInformationMessageWithAction } from "../common/vscode/dialog";
3434
import { redactableError } from "../common/errors";
3535
import { App } from "../common/app";
3636
import { QueryTreeViewItem } from "../queries-panel/query-tree-view-item";
3737
import { containsPath, pathsEqual } from "../common/files";
3838
import { getQlPackPath } from "../common/ql";
39-
import { load } from "js-yaml";
40-
import { QlPackFile } from "../packaging/qlpack-file";
39+
import { getQlPackLanguage } from "../common/qlpack-language";
4140

4241
type QueryLanguagesToDatabaseMap = Record<string, string>;
4342

@@ -253,24 +252,12 @@ export class SkeletonQueryWizard {
253252
return undefined;
254253
}
255254

256-
const qlPack = load(await readFile(qlPackPath, "utf8")) as
257-
| QlPackFile
258-
| undefined;
259-
const dependencies = qlPack?.dependencies;
260-
if (!dependencies || typeof dependencies !== "object") {
261-
return;
262-
}
263-
264-
const matchingLanguages = Object.values(QueryLanguage).filter(
265-
(language) => `codeql/${language}-all` in dependencies,
266-
);
267-
if (matchingLanguages.length !== 1) {
268-
return undefined;
255+
const language = await getQlPackLanguage(qlPackPath);
256+
if (language) {
257+
this.qlPackStoragePath = matchingQueryPackPath;
269258
}
270259

271-
this.qlPackStoragePath = matchingQueryPackPath;
272-
273-
return matchingLanguages[0];
260+
return language;
274261
}
275262

276263
private async chooseLanguage() {

extensions/ql-vscode/src/model-editor/extension-pack-metadata.schema.json

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@
55
"ExtensionPackMetadata": {
66
"type": "object",
77
"properties": {
8+
"name": {
9+
"type": ["string", "null"]
10+
},
11+
"version": {
12+
"type": ["string", "null"]
13+
},
814
"extensionTargets": {
9-
"type": "object",
10-
"additionalProperties": {
11-
"type": "string"
12-
}
15+
"anyOf": [
16+
{
17+
"type": "object",
18+
"additionalProperties": {
19+
"type": "string"
20+
}
21+
},
22+
{
23+
"type": "null"
24+
}
25+
]
1326
},
1427
"dataExtensions": {
1528
"anyOf": [
@@ -21,35 +34,46 @@
2134
},
2235
{
2336
"type": "string"
37+
},
38+
{
39+
"type": "null"
2440
}
2541
]
2642
},
27-
"name": {
28-
"type": "string"
29-
},
30-
"version": {
31-
"type": "string"
32-
},
3343
"dependencies": {
34-
"type": "object",
35-
"additionalProperties": {
36-
"type": "string"
37-
}
44+
"anyOf": [
45+
{
46+
"type": "object",
47+
"additionalProperties": {
48+
"type": "string"
49+
}
50+
},
51+
{
52+
"type": "null"
53+
}
54+
]
3855
},
3956
"dbscheme": {
40-
"type": "string"
57+
"type": ["string", "null"]
4158
},
4259
"library": {
43-
"type": "boolean"
60+
"type": ["boolean", "null"]
4461
},
4562
"defaultSuite": {
46-
"type": "array",
47-
"items": {
48-
"$ref": "#/definitions/SuiteInstruction"
49-
}
63+
"anyOf": [
64+
{
65+
"type": "array",
66+
"items": {
67+
"$ref": "#/definitions/SuiteInstruction"
68+
}
69+
},
70+
{
71+
"type": "null"
72+
}
73+
]
5074
},
5175
"defaultSuiteFile": {
52-
"type": "string"
76+
"type": ["string", "null"]
5377
}
5478
},
5579
"required": ["dataExtensions", "extensionTargets", "name", "version"]
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { QlPackFile } from "../packaging/qlpack-file";
22

33
export type ExtensionPackMetadata = QlPackFile & {
4-
// Make both extensionTargets and dataExtensions required
4+
// Make name, version, extensionTargets, and dataExtensions required
5+
name: string;
6+
version: string;
57
extensionTargets: Record<string, string>;
68
dataExtensions: string[] | string;
79
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Ajv from "ajv";
2+
import * as qlpackFileSchemaJson from "./qlpack-file.schema.json";
3+
import { QlPackFile } from "./qlpack-file";
4+
import { load } from "js-yaml";
5+
import { readFile } from "fs-extra";
6+
7+
const ajv = new Ajv({ allErrors: true });
8+
const qlpackFileValidate = ajv.compile(qlpackFileSchemaJson);
9+
10+
export async function loadQlpackFile(path: string): Promise<QlPackFile> {
11+
const qlpackFileText = await readFile(path, "utf8");
12+
13+
let qlPack = load(qlpackFileText) as QlPackFile | undefined;
14+
15+
if (qlPack === undefined || qlPack === null) {
16+
// An empty file is not valid according to the schema since it's not an object,
17+
// but it is equivalent to an empty object.
18+
qlPack = {};
19+
}
20+
21+
qlpackFileValidate(qlPack);
22+
23+
if (qlpackFileValidate.errors) {
24+
throw new Error(
25+
`Invalid extension pack YAML: ${qlpackFileValidate.errors
26+
.map((error) => `${error.instancePath} ${error.message}`)
27+
.join(", ")}`,
28+
);
29+
}
30+
31+
return qlPack;
32+
}

0 commit comments

Comments
 (0)