1
1
import type { CancellationToken } from "vscode" ;
2
- import { Uri , window } from "vscode" ;
3
- import { join , sep , basename , relative } from "path" ;
4
- import { dump , load } from "js-yaml" ;
2
+ import { window } from "vscode" ;
3
+ import { join , basename , relative } from "path" ;
4
+ import { dump } from "js-yaml" ;
5
5
import { copy , writeFile , readFile , mkdirp , remove } from "fs-extra" ;
6
6
import { nanoid } from "nanoid" ;
7
7
import { tmpDir } from "../tmp-dir" ;
@@ -27,13 +27,7 @@ import {
27
27
} from "./repository-selection" ;
28
28
import type { Repository } from "./shared/repository" ;
29
29
import type { DbManager } from "../databases/db-manager" ;
30
- import {
31
- getQlPackFilePath ,
32
- FALLBACK_QLPACK_FILENAME ,
33
- QLPACK_FILENAMES ,
34
- QLPACK_LOCK_FILENAMES ,
35
- } from "../common/ql" ;
36
- import type { QlPackFile } from "../packaging/qlpack-file" ;
30
+ import { FALLBACK_QLPACK_FILENAME } from "../common/ql" ;
37
31
import { expandShortPaths } from "../common/short-paths" ;
38
32
import type { QlPackDetails } from "./ql-pack-details" ;
39
33
import type { ModelPackDetails } from "../common/model-pack-details" ;
@@ -68,8 +62,6 @@ async function generateQueryPack(
68
62
) ;
69
63
70
64
const mustSynthesizePack = qlPackDetails . qlPackFilePath === undefined ;
71
- const cliSupportsMrvaPackCreate =
72
- await cliServer . cliConstraints . supportsMrvaPackCreate ( ) ;
73
65
74
66
let targetPackPath : string ;
75
67
let needsInstall : boolean ;
@@ -87,15 +79,6 @@ async function generateQueryPack(
87
79
88
80
// Install packs, since we just synthesized a dependency on the language's standard library.
89
81
needsInstall = true ;
90
- } else if ( ! cliSupportsMrvaPackCreate ) {
91
- // We need to copy the query pack to a temporary directory and then fix it up to work with MRVA.
92
- targetPackPath = tmpDir . queryPackDir ;
93
- await copyExistingQueryPack ( cliServer , qlPackDetails , targetPackPath ) ;
94
-
95
- // We should already have all the dependencies available, but these older versions of the CLI
96
- // have a bug where they will not search `--additional-packs` during validation in `codeql pack bundle`.
97
- // Installing the packs will ensure that any extension packs get put in the right place.
98
- needsInstall = true ;
99
82
} else {
100
83
// The CLI supports creating a MRVA query pack directly from the source pack.
101
84
targetPackPath = qlPackDetails . qlPackRootPath ;
@@ -113,27 +96,18 @@ async function generateQueryPack(
113
96
await cliServer . clearCache ( ) ;
114
97
}
115
98
116
- let precompilationOpts : string [ ] ;
117
- if ( cliSupportsMrvaPackCreate ) {
118
- const queryOpts = qlPackDetails . queryFiles . flatMap ( ( q ) => [
119
- "--query" ,
120
- join ( targetPackPath , relative ( qlPackDetails . qlPackRootPath , q ) ) ,
121
- ] ) ;
122
-
123
- precompilationOpts = [
124
- "--mrva" ,
125
- ...queryOpts ,
126
- // We need to specify the extension packs as dependencies so that they are included in the MRVA pack.
127
- // The version range doesn't matter, since they'll always be found by source lookup.
128
- ...extensionPacks . map ( ( p ) => `--extension-pack=${ p . name } @*` ) ,
129
- ] ;
130
- } else {
131
- precompilationOpts = [ "--qlx" ] ;
132
-
133
- if ( extensionPacks . length > 0 ) {
134
- await addExtensionPacksAsDependencies ( targetPackPath , extensionPacks ) ;
135
- }
136
- }
99
+ const queryOpts = qlPackDetails . queryFiles . flatMap ( ( q ) => [
100
+ "--query" ,
101
+ join ( targetPackPath , relative ( qlPackDetails . qlPackRootPath , q ) ) ,
102
+ ] ) ;
103
+
104
+ const precompilationOpts = [
105
+ "--mrva" ,
106
+ ...queryOpts ,
107
+ // We need to specify the extension packs as dependencies so that they are included in the MRVA pack.
108
+ // The version range doesn't matter, since they'll always be found by source lookup.
109
+ ...extensionPacks . map ( ( p ) => `--extension-pack=${ p . name } @*` ) ,
110
+ ] ;
137
111
138
112
const bundlePath = tmpDir . bundleFile ;
139
113
void extLogger . log (
@@ -181,59 +155,6 @@ async function createNewQueryPack(
181
155
) ;
182
156
}
183
157
184
- async function copyExistingQueryPack (
185
- cliServer : CodeQLCliServer ,
186
- qlPackDetails : QlPackDetails ,
187
- targetPackPath : string ,
188
- ) {
189
- const toCopy = await cliServer . packPacklist (
190
- qlPackDetails . qlPackRootPath ,
191
- false ,
192
- ) ;
193
-
194
- // Also include query files that contain extensible predicates. These query files are not
195
- // needed for the query to run, but they are needed for the query pack to pass deep validation
196
- // of data extensions.
197
- const metadata = await cliServer . generateExtensiblePredicateMetadata (
198
- qlPackDetails . qlPackRootPath ,
199
- ) ;
200
- metadata . extensible_predicates . forEach ( ( predicate ) => {
201
- if ( predicate . path . endsWith ( ".ql" ) ) {
202
- toCopy . push ( join ( qlPackDetails . qlPackRootPath , predicate . path ) ) ;
203
- }
204
- } ) ;
205
-
206
- [
207
- // also copy the lock file (either new name or old name) and the query file itself. These are not included in the packlist.
208
- ...QLPACK_LOCK_FILENAMES . map ( ( f ) => join ( qlPackDetails . qlPackRootPath , f ) ) ,
209
- ...qlPackDetails . queryFiles ,
210
- ] . forEach ( ( absolutePath ) => {
211
- if ( absolutePath ) {
212
- toCopy . push ( absolutePath ) ;
213
- }
214
- } ) ;
215
-
216
- let copiedCount = 0 ;
217
- await copy ( qlPackDetails . qlPackRootPath , targetPackPath , {
218
- filter : ( file : string ) =>
219
- // copy file if it is in the packlist, or it is a parent directory of a file in the packlist
220
- ! ! toCopy . find ( ( f ) => {
221
- // Normalized paths ensure that Windows drive letters are capitalized consistently.
222
- const normalizedPath = Uri . file ( f ) . fsPath ;
223
- const matches =
224
- normalizedPath === file || normalizedPath . startsWith ( file + sep ) ;
225
- if ( matches ) {
226
- copiedCount ++ ;
227
- }
228
- return matches ;
229
- } ) ,
230
- } ) ;
231
-
232
- void extLogger . log ( `Copied ${ copiedCount } files to ${ targetPackPath } ` ) ;
233
-
234
- await fixPackFile ( targetPackPath , qlPackDetails ) ;
235
- }
236
-
237
158
interface RemoteQueryTempDir {
238
159
remoteQueryDir : string ;
239
160
queryPackDir : string ;
@@ -351,42 +272,6 @@ export async function prepareRemoteQueryRun(
351
272
} ;
352
273
}
353
274
354
- /**
355
- * Fixes the qlpack.yml or codeql-pack.yml file to be correct in the context of the MRVA request.
356
- *
357
- * Performs the following fixes:
358
- *
359
- * - Updates the default suite of the query pack. This is used to ensure
360
- * only the specified query is run.
361
- * - Ensures the query pack name is set to the name expected by the server.
362
- * - Removes any `${workspace}` version references from the qlpack.yml or codeql-pack.yml file. Converts them
363
- * to `*` versions.
364
- *
365
- * @param targetPackPath The path to the directory containing the target pack
366
- * @param qlPackDetails The details of the original QL pack
367
- */
368
- async function fixPackFile (
369
- targetPackPath : string ,
370
- qlPackDetails : QlPackDetails ,
371
- ) : Promise < void > {
372
- const packPath = await getQlPackFilePath ( targetPackPath ) ;
373
-
374
- // This should not happen since we create the pack ourselves.
375
- if ( ! packPath ) {
376
- throw new Error (
377
- `Could not find ${ QLPACK_FILENAMES . join (
378
- " or " ,
379
- ) } file in '${ targetPackPath } '`,
380
- ) ;
381
- }
382
- const qlpack = load ( await readFile ( packPath , "utf8" ) ) as QlPackFile ;
383
-
384
- updateDefaultSuite ( qlpack , qlPackDetails ) ;
385
- removeWorkspaceRefs ( qlpack ) ;
386
-
387
- await writeFile ( packPath , dump ( qlpack ) ) ;
388
- }
389
-
390
275
async function getExtensionPacksToInject (
391
276
cliServer : CodeQLCliServer ,
392
277
workspaceFolders : string [ ] ,
@@ -415,41 +300,6 @@ async function getExtensionPacksToInject(
415
300
return result ;
416
301
}
417
302
418
- async function addExtensionPacksAsDependencies (
419
- queryPackDir : string ,
420
- extensionPacks : ModelPackDetails [ ] ,
421
- ) : Promise < void > {
422
- const qlpackFile = await getQlPackFilePath ( queryPackDir ) ;
423
- if ( ! qlpackFile ) {
424
- throw new Error (
425
- `Could not find ${ QLPACK_FILENAMES . join (
426
- " or " ,
427
- ) } file in '${ queryPackDir } '`,
428
- ) ;
429
- }
430
-
431
- const syntheticQueryPack = load (
432
- await readFile ( qlpackFile , "utf8" ) ,
433
- ) as QlPackFile ;
434
-
435
- const dependencies = syntheticQueryPack . dependencies ?? { } ;
436
- extensionPacks . forEach ( ( { name } ) => {
437
- // Add this extension pack as a dependency. It doesn't matter which
438
- // version we specify, since we are guaranteed that the extension pack
439
- // is resolved from source at the given path.
440
- dependencies [ name ] = "*" ;
441
- } ) ;
442
-
443
- syntheticQueryPack . dependencies = dependencies ;
444
-
445
- await writeFile ( qlpackFile , dump ( syntheticQueryPack ) ) ;
446
- }
447
-
448
- function updateDefaultSuite ( qlpack : QlPackFile , qlPackDetails : QlPackDetails ) {
449
- delete qlpack . defaultSuiteFile ;
450
- qlpack . defaultSuite = generateDefaultSuite ( qlPackDetails ) ;
451
- }
452
-
453
303
function generateDefaultSuite ( qlPackDetails : QlPackDetails ) {
454
304
const queries = qlPackDetails . queryFiles . map ( ( query ) => {
455
305
const relativePath = relative ( qlPackDetails . qlPackRootPath , query ) ;
@@ -550,15 +400,3 @@ async function getControllerRepoFromApi(
550
400
}
551
401
}
552
402
}
553
-
554
- function removeWorkspaceRefs ( qlpack : QlPackFile ) {
555
- if ( ! qlpack . dependencies ) {
556
- return ;
557
- }
558
-
559
- for ( const [ key , value ] of Object . entries ( qlpack . dependencies ) ) {
560
- if ( value === "${workspace}" ) {
561
- qlpack . dependencies [ key ] = "*" ;
562
- }
563
- }
564
- }
0 commit comments