Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix Snyk scan by renaming bun's aliased yarn.lock keys (#79646)

Romeo Van Snickfc7d75b01fe5
diff --git a/.github/scripts/fix-yarn-lock-aliases.ts b/.github/scripts/fix-yarn-lock-aliases.ts
new file mode 100644
index 00000000000..6e01b0e5334
--- /dev/null
+++ b/.github/scripts/fix-yarn-lock-aliases.ts
@@ -0,0 +1,55 @@
+/**
+ * Renames aliased dependency keys in a bun-generated yarn.lock.
+ *
+ * Yarn names an alias entry after the alias, for example
+ * `"typescript7@npm:[email protected]"`. Bun names it after the aliased package
+ * instead, for example `"typescript@npm:[email protected]"`. Snyk follows the
+ * yarn convention and fails to resolve the dependency.
+ */
+
+const packageJsonPath = `${process.cwd()}/package.json`;
+const lockPath = `${process.cwd()}/yarn.lock`;
+
+const packageJson = await Bun.file(packageJsonPath).json();
+
+const dependencies = Object.entries<string>(
+ Object.assign(
+ {},
+ packageJson.dependencies,
+ packageJson.devDependencies,
+ packageJson.optionalDependencies,
+ ),
+);
+
+let lock = await Bun.file(lockPath).text();
+const renamedKeys: string[] = [];
+
+for (const [alias, specifier] of dependencies) {
+ if (!specifier.startsWith("npm:")) {
+ continue;
+ }
+
+ const aliasedPackage = specifier.slice("npm:".length).replace(/@[^@]*$/, "");
+
+ if (aliasedPackage === alias) {
+ continue;
+ }
+
+ const bunKey = `"${aliasedPackage}@${specifier}"`;
+ const yarnKey = `"${alias}@${specifier}"`;
+
+ if (lock.includes(bunKey)) {
+ lock = lock.replaceAll(bunKey, yarnKey);
+ renamedKeys.push(`${bunKey} -> ${yarnKey}`);
+ }
+}
+
+if (renamedKeys.length > 0) {
+ await Bun.write(lockPath, lock);
+}
+
+process.stdout.write(
+ renamedKeys.length > 0
+ ? `Renamed alias keys in yarn.lock:\n${renamedKeys.join("\n")}\n`
+ : "No alias keys to rename in yarn.lock\n",
+);
diff --git a/.github/workflows/snyk.yml b/.github/workflows/snyk.yml
index a97902fe347..d57800a06aa 100644
--- a/.github/workflows/snyk.yml
+++ b/.github/workflows/snyk.yml
@@ -10,6 +10,7 @@ on:
- "**/package.json"
- ".github/workflows/snyk.yml"
- ".github/scripts/write-poms.sh"
+ - ".github/scripts/fix-yarn-lock-aliases.ts"
schedule:
- cron: "0 5 * * *"
@@ -58,18 +59,22 @@ jobs:
# Snyk doesn't support bun.lock yet, so we generate yarn.lock and scan that.
# Bun has a bug where it normalizes "< 3.0.0" to "< 3" in alias entries
# but not in dependency specs, causing Snyk to fail matching dependencies.
- # The sed commands fix this inconsistency. They continue on error to let
- # Snyk report parsing issues directly rather than failing on the workaround.
+ # The sed commands fix this inconsistency. Bun also names aliased entries
+ # after the aliased package instead of the alias, which the script fixes.
+ # Both continue on error to let Snyk report parsing issues directly rather
+ # than failing on the workaround.
- name: Generate yarn.lock
if: matrix.project.file == 'yarn.lock'
run: |
bun install --yarn
sed -i -E 's/< ([0-9]+)\.0\.0"/< \1"/g; s/< ([0-9]+)\.0"/< \1"/g' yarn.lock || true
+ bun ${{ github.workspace }}/.github/scripts/fix-yarn-lock-aliases.ts || true
- name: Generate release/yarn.lock
if: matrix.project.file == 'release/yarn.lock'
run: |
bun install --yarn
sed -i -E 's/< ([0-9]+)\.0\.0"/< \1"/g; s/< ([0-9]+)\.0"/< \1"/g' yarn.lock || true
+ bun ${{ github.workspace }}/.github/scripts/fix-yarn-lock-aliases.ts || true
working-directory: release
- uses: snyk/actions/setup@9adf32b1121593767fc3c057af55b55db032dc04 # v1.0.0
- name: Run snyk test