Skip to content

Commit d78a4d1

Browse files
authored
Merge pull request #2960 from github/koesie10/install-workspace-packs
Do not show non-workspace packs when installing pack dependencies
2 parents 3cbaa5a + 25ba9e4 commit d78a4d1

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CodeQLCliServer } from "../codeql-cli/cli";
1+
import { CodeQLCliServer, QlpacksInfo } from "../codeql-cli/cli";
22
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
33
import { QuickPickItem, window } from "vscode";
44
import {
@@ -16,6 +16,7 @@ import { redactableError } from "../common/errors";
1616
import { PACKS_BY_QUERY_LANGUAGE } from "../common/query-language";
1717
import { PackagingCommands } from "../common/commands";
1818
import { telemetryListener } from "../common/vscode/telemetry";
19+
import { containsPath } from "../common/files";
1920

2021
type PackagingOptions = {
2122
cliServer: CodeQLCliServer;
@@ -126,9 +127,10 @@ export async function handleInstallPackDependencies(
126127
step: 1,
127128
maxStep: 2,
128129
});
129-
const workspacePacks = await cliServer.resolveQlpacks(
130-
getOnDiskWorkspaceFolders(),
131-
);
130+
const workspaceFolders = getOnDiskWorkspaceFolders();
131+
const resolvedPacks = await cliServer.resolveQlpacks(workspaceFolders);
132+
const workspacePacks = filterWorkspacePacks(resolvedPacks, workspaceFolders);
133+
132134
const quickPickItems = Object.entries(
133135
workspacePacks,
134136
).map<QLPackQuickPickItem>(([key, value]) => ({
@@ -179,3 +181,27 @@ export async function handleInstallPackDependencies(
179181
throw new UserCancellationException("No packs selected.");
180182
}
181183
}
184+
185+
/**
186+
* This filter will remove any packs from the qlpacks that are not in the workspace. It will
187+
* filter out any packs that are in e.g. the package cache or in the distribution, which the
188+
* user does not need to install dependencies for.
189+
*
190+
* @param qlpacks The qlpacks to filter.
191+
* @param workspaceFolders The workspace folders to filter by.
192+
*/
193+
export function filterWorkspacePacks(
194+
qlpacks: QlpacksInfo,
195+
workspaceFolders: string[],
196+
): QlpacksInfo {
197+
return Object.fromEntries(
198+
Object.entries(qlpacks).filter(([, packDirs]) =>
199+
// If any of the pack dirs are in the workspace, keep the pack
200+
packDirs.some((packDir) =>
201+
workspaceFolders.some((workspaceFolder) =>
202+
containsPath(workspaceFolder, packDir),
203+
),
204+
),
205+
),
206+
);
207+
}

extensions/ql-vscode/test/vscode-tests/cli-integration/packaging/packaging.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import {
1212
import { mockedQuickPickItem } from "../../utils/mocking.helpers";
1313
import { getActivatedExtension } from "../../global.helper";
1414
import {
15-
showAndLogInformationMessage,
1615
showAndLogExceptionWithTelemetry,
16+
showAndLogInformationMessage,
1717
} from "../../../../src/common/logging";
18+
import * as workspaceFolders from "../../../../src/common/vscode/workspace-folders";
19+
import { getOnDiskWorkspaceFolders } from "../../../../src/common/vscode/workspace-folders";
20+
import { pathsEqual } from "../../../../src/common/files";
1821

1922
describe("Packaging commands", () => {
2023
let cli: CodeQLCliServer;
@@ -87,6 +90,48 @@ describe("Packaging commands", () => {
8790
).toEqual("Unable to download all packs. See log for more details.");
8891
});
8992

93+
it("should only show workspace packs", async () => {
94+
const originalWorkspaceFolders = getOnDiskWorkspaceFolders();
95+
96+
// Remove the CodeQL workspace folder from the list of workspace folders
97+
// since that includes all the packs that are already in the package cache,
98+
// so the test would be useless if we included it since nothing would be
99+
// filtered out (except for maybe the distribution legacy-upgrades).
100+
jest
101+
.spyOn(workspaceFolders, "getOnDiskWorkspaceFolders")
102+
.mockReturnValue(
103+
originalWorkspaceFolders.filter(
104+
(folder) => !pathsEqual(folder, process.env.TEST_CODEQL_PATH ?? ""),
105+
),
106+
);
107+
108+
const rootDir = join(__dirname, "../data");
109+
quickPickSpy.mockResolvedValue(
110+
mockedQuickPickItem([
111+
{
112+
label: "integration-test-queries-javascript",
113+
packRootDir: [rootDir],
114+
},
115+
]),
116+
);
117+
118+
await handleInstallPackDependencies(cli, progress);
119+
expect(quickPickSpy).toHaveBeenCalledWith(
120+
[
121+
expect.objectContaining({
122+
label: "integration-test-debugger-javascript",
123+
}),
124+
expect.objectContaining({
125+
label: "semmle/has-extension",
126+
}),
127+
expect.objectContaining({
128+
label: "semmle/targets-extension",
129+
}),
130+
],
131+
expect.anything(),
132+
);
133+
});
134+
90135
it("should install valid workspace pack", async () => {
91136
const rootDir = join(__dirname, "../data");
92137
quickPickSpy.mockResolvedValue(

0 commit comments

Comments
 (0)