@@ -6,6 +6,56 @@ const { glob } = require('glob');
66const { promisify } = require ( 'util' ) ;
77const execFile = promisify ( childProcess . execFile ) ;
88
9+ const PACKAGE_LOCK_PATH = path . join ( __dirname , '..' , 'package-lock.json' ) ;
10+
11+ /**
12+ * "node_modules/@vscode/vsce-sign" package which is a dev dependency used for
13+ * publishing extension declares platform specific optionalDependencies, namely
14+ * the following:
15+ * - "@vscode/vsce-sign-alpine-arm64"
16+ * - "@vscode/vsce-sign-alpine-x64"
17+ * - "@vscode/vsce-sign-darwin-arm64"
18+ * - "@vscode/vsce-sign-darwin-x64"
19+ * - "@vscode/vsce-sign-linux-arm"
20+ * - "@vscode/vsce-sign-linux-arm64"
21+ * - "@vscode/vsce-sign-linux-x64"
22+ * - "@vscode/vsce-sign-win32-arm64"
23+ * - "@vscode/vsce-sign-win32-x64"
24+ *
25+ * Snyk requires what is declared in package-lock.json to be also present in
26+ * installed node_modules but this will never happen because for any platform,
27+ * other platform specific deps will always be missing which means Snyk will
28+ * always fail in this case.
29+ *
30+ * Because we always install with `npm ci --omit=optional`, with this method we
31+ * try to remove these identified problematic optionalDependencies before
32+ * running the Snyk tests and once the tests are finished, we restore the
33+ * original state back using npm hooks.
34+ */
35+ async function removeProblematicOptionalDepsFromPackageLock ( ) {
36+ const packageLockContent = JSON . parse (
37+ await fs . readFile ( PACKAGE_LOCK_PATH , 'utf-8' ) ,
38+ ) ;
39+
40+ const vsceSignPackage =
41+ packageLockContent . packages ?. [ 'node_modules/@vscode/vsce-sign' ] ;
42+
43+ if ( ! vsceSignPackage || ! vsceSignPackage . optionalDependencies ) {
44+ console . info ( 'No problematic optional dependencies to fix' ) ;
45+ return ;
46+ }
47+
48+ // Temporarily remove the optional dependencies
49+ vsceSignPackage [ 'optionalDependencies' ] = { } ;
50+
51+ // We write the actual package-lock path but restoring of the original file is
52+ // handled by npm hooks.
53+ await fs . writeFile (
54+ PACKAGE_LOCK_PATH ,
55+ JSON . stringify ( packageLockContent , null , 2 ) ,
56+ ) ;
57+ }
58+
959async function snykTest ( cwd ) {
1060 const tmpPath = path . join ( os . tmpdir ( ) , 'tempfile-' + Date . now ( ) ) ;
1161
@@ -17,9 +67,8 @@ async function snykTest(cwd) {
1767 await execFile (
1868 'npx' ,
1969 [
20- 'snyk' ,
70+ 'snyk@latest ' ,
2171 'test' ,
22- '--all-projects' ,
2372 '--severity-threshold=low' ,
2473 '--dev' ,
2574 `--json-file-output=${ tmpPath } ` ,
@@ -47,6 +96,7 @@ async function snykTest(cwd) {
4796async function main ( ) {
4897 const rootPath = path . resolve ( __dirname , '..' ) ;
4998 await fs . mkdir ( path . join ( rootPath , `.sbom` ) , { recursive : true } ) ;
99+ await removeProblematicOptionalDepsFromPackageLock ( ) ;
50100 const results = await snykTest ( rootPath ) ;
51101
52102 await fs . writeFile (
0 commit comments