1- import { promises } from "fs-extra" ;
2- import { basename , dirname , resolve } from "path" ;
3-
41import { getOnDiskWorkspaceFolders } from "../../common/vscode/workspace-folders" ;
52import { QlPacksForLanguage } from "../../databases/qlpack" ;
63import {
@@ -17,7 +14,7 @@ import { TeeLogger } from "../../common/logging";
1714import { CancellationToken } from "vscode" ;
1815import { ProgressCallback } from "../../common/vscode/progress" ;
1916import { CoreCompletedQuery , QueryRunner } from "../../query-server" ;
20- import { QLPACK_FILENAMES } from "../../common/ql " ;
17+ import { createLockFileForStandardQuery } from "../../local-queries/standard-queries " ;
2118
2219export async function resolveQueries (
2320 cli : CodeQLCliServer ,
@@ -30,64 +27,6 @@ export async function resolveQueries(
3027 } ) ;
3128}
3229
33- async function resolveContextualQuery (
34- cli : CodeQLCliServer ,
35- query : string ,
36- ) : Promise < { packPath : string ; createdTempLockFile : boolean } > {
37- // Contextual queries now live within the standard library packs.
38- // This simplifies distribution (you don't need the standard query pack to use the AST viewer),
39- // but if the library pack doesn't have a lockfile, we won't be able to find
40- // other pack dependencies of the library pack.
41-
42- // Work out the enclosing pack.
43- const packContents = await cli . packPacklist ( query , false ) ;
44- const packFilePath = packContents . find ( ( p ) =>
45- QLPACK_FILENAMES . includes ( basename ( p ) ) ,
46- ) ;
47- if ( packFilePath === undefined ) {
48- // Should not happen; we already resolved this query.
49- throw new Error (
50- `Could not find a CodeQL pack file for the pack enclosing the contextual query ${ query } ` ,
51- ) ;
52- }
53- const packPath = dirname ( packFilePath ) ;
54- const lockFilePath = packContents . find ( ( p ) =>
55- [ "codeql-pack.lock.yml" , "qlpack.lock.yml" ] . includes ( basename ( p ) ) ,
56- ) ;
57- let createdTempLockFile = false ;
58- if ( ! lockFilePath ) {
59- // No lock file, likely because this library pack is in the package cache.
60- // Create a lock file so that we can resolve dependencies and library path
61- // for the contextual query.
62- void extLogger . log (
63- `Library pack ${ packPath } is missing a lock file; creating a temporary lock file` ,
64- ) ;
65- await cli . packResolveDependencies ( packPath ) ;
66- createdTempLockFile = true ;
67- // Clear CLI server pack cache before installing dependencies,
68- // so that it picks up the new lock file, not the previously cached pack.
69- void extLogger . log ( "Clearing the CodeQL CLI server's pack cache" ) ;
70- await cli . clearCache ( ) ;
71- // Install dependencies.
72- void extLogger . log (
73- `Installing package dependencies for library pack ${ packPath } ` ,
74- ) ;
75- await cli . packInstall ( packPath ) ;
76- }
77- return { packPath, createdTempLockFile } ;
78- }
79-
80- async function removeTemporaryLockFile ( packPath : string ) {
81- const tempLockFilePath = resolve ( packPath , "codeql-pack.lock.yml" ) ;
82- void extLogger . log (
83- `Deleting temporary package lock file at ${ tempLockFilePath } ` ,
84- ) ;
85- // It's fine if the file doesn't exist.
86- await promises . rm ( resolve ( packPath , "codeql-pack.lock.yml" ) , {
87- force : true ,
88- } ) ;
89- }
90-
9130export async function runContextualQuery (
9231 query : string ,
9332 db : DatabaseItem ,
@@ -98,10 +37,7 @@ export async function runContextualQuery(
9837 token : CancellationToken ,
9938 templates : Record < string , string > ,
10039) : Promise < CoreCompletedQuery > {
101- const { packPath, createdTempLockFile } = await resolveContextualQuery (
102- cli ,
103- query ,
104- ) ;
40+ const { cleanup } = await createLockFileForStandardQuery ( cli , query ) ;
10541 const queryRun = qs . createQueryRun (
10642 db . databaseUri . fsPath ,
10743 { queryPath : query , quickEvalPosition : undefined } ,
@@ -120,8 +56,6 @@ export async function runContextualQuery(
12056 token ,
12157 new TeeLogger ( qs . logger , queryRun . outputDir . logPath ) ,
12258 ) ;
123- if ( createdTempLockFile ) {
124- await removeTemporaryLockFile ( packPath ) ;
125- }
59+ await cleanup ?.( ) ;
12660 return results ;
12761}
0 commit comments