diff --git a/extensions/ql-vscode/src/cli.ts b/extensions/ql-vscode/src/cli.ts index 1478fe70b..07cb3cd63 100644 --- a/extensions/ql-vscode/src/cli.ts +++ b/extensions/ql-vscode/src/cli.ts @@ -917,8 +917,12 @@ export class CodeQLCliServer implements Disposable { return this.runJsonCodeQlCliCommand(['pack', 'download'], packs, 'Downloading packs'); } - async packInstall(dir: string) { - return this.runJsonCodeQlCliCommand(['pack', 'install'], [dir], 'Installing pack dependencies'); + async packInstall(dir: string, forceUpdate = false) { + const args = [dir]; + if (forceUpdate) { + args.push('--mode', 'update'); + } + return this.runJsonCodeQlCliCommand(['pack', 'install'], args, 'Installing pack dependencies'); } async packBundle(dir: string, workspaceFolders: string[], outputPath: string, precompile = true): Promise { @@ -1270,7 +1274,7 @@ export class CliVersionConstraint { /** * CLI version where the `resolve ml-models` subcommand was enhanced to work with packaging. */ -public static CLI_VERSION_WITH_PRECISE_RESOLVE_ML_MODELS = new SemVer('2.10.0'); + public static CLI_VERSION_WITH_PRECISE_RESOLVE_ML_MODELS = new SemVer('2.10.0'); /** * CLI version where the `--old-eval-stats` option to the query server was introduced. diff --git a/extensions/ql-vscode/src/quick-query.ts b/extensions/ql-vscode/src/quick-query.ts index b902c6bb7..8a30c7dd8 100644 --- a/extensions/ql-vscode/src/quick-query.ts +++ b/extensions/ql-vscode/src/quick-query.ts @@ -121,7 +121,9 @@ export async function displayQuickQuery( const quickQueryQlpackYaml: any = { name: 'vscode/quick-query', version: '1.0.0', - libraryPathDependencies: [qlpack] + dependencies: { + [qlpack]: '*' + } }; await fs.writeFile(qlPackFile, QLPACK_FILE_HEADER + yaml.dump(quickQueryQlpackYaml), 'utf8'); } @@ -130,6 +132,11 @@ export async function displayQuickQuery( await fs.writeFile(qlFile, getInitialQueryContents(dbItem.language, dbscheme), 'utf8'); } + if (shouldRewrite) { + await cliServer.clearCache(); + await cliServer.packInstall(queriesDir, true); + } + await Window.showTextDocument(await workspace.openTextDocument(qlFile)); } catch (e) { if (e instanceof ResponseError && e.code == ErrorCodes.RequestCancelled) { @@ -145,5 +152,5 @@ async function checkShouldRewrite(qlPackFile: string, newDependency: string) { return true; } const qlPackContents: any = yaml.load(await fs.readFile(qlPackFile, 'utf8')); - return qlPackContents.libraryPathDependencies?.[0] !== newDependency; + return !qlPackContents.dependencies?.[newDependency]; } diff --git a/extensions/ql-vscode/src/vscode-tests/cli-integration/queries.test.ts b/extensions/ql-vscode/src/vscode-tests/cli-integration/queries.test.ts index 06129bce9..fe4e266ce 100644 --- a/extensions/ql-vscode/src/vscode-tests/cli-integration/queries.test.ts +++ b/extensions/ql-vscode/src/vscode-tests/cli-integration/queries.test.ts @@ -38,6 +38,8 @@ describe('Queries', function() { let ctx: ExtensionContext; let qlpackFile: string; + let qlpackLockFile: string; + let oldQlpackLockFile: string; // codeql v2.6.3 and earlier let qlFile: string; @@ -53,6 +55,8 @@ describe('Queries', function() { cli.quiet = true; ctx = extension.ctx; qlpackFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.yml`; + qlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/codeql-pack.lock.yml`; + oldQlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.lock.yml`; qlFile = `${ctx.storageUri?.fsPath}/quick-queries/quick-query.ql`; } else { throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.'); @@ -153,7 +157,14 @@ describe('Queries', function() { fs.readFileSync(qlpackFile, 'utf8') ); // Should have chosen the js libraries - expect(qlpackContents.libraryPathDependencies[0]).to.include('javascript'); + expect(qlpackContents.dependencies['codeql/javascript-all']).to.eq('*'); + + // Should also have a codeql-pack.lock.yml file + const packFileToUse = fs.pathExistsSync(qlpackLockFile) ? qlpackLockFile : oldQlpackLockFile; + const qlpackLock: any = await yaml.load( + fs.readFileSync(packFileToUse, 'utf8') + ); + expect(!!qlpackLock.dependencies['codeql/javascript-all'].version).to.be.true; }); it('should avoid creating a quick query', async () => { @@ -161,7 +172,9 @@ describe('Queries', function() { fs.writeFileSync(qlpackFile, yaml.dump({ name: 'quick-query', version: '1.0.0', - libraryPathDependencies: ['codeql/javascript-all'] + dependencies: { + 'codeql/javascript-all': '*' + } })); fs.writeFileSync(qlFile, 'xxx'); await commands.executeCommand('codeQL.quickQuery');