Skip to content

Commit fec6dd2

Browse files
committed
fix(nix): handle invalid semver versions in canonicalize-node-modules
1 parent 7ee5ba0 commit fec6dd2

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

nix/scripts/canonicalize-node-modules.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ async function isDirectory(path: string) {
1515
}
1616
}
1717

18+
function isValidSemver(v: string) {
19+
try {
20+
Bun.semver.order(v, "0.0.0")
21+
return true
22+
} catch {
23+
return false
24+
}
25+
}
26+
1827
const root = process.cwd()
1928
const bunRoot = join(root, "node_modules/.bun")
2029
const linkRoot = join(bunRoot, "node_modules")
@@ -32,15 +41,23 @@ for (const entry of directories) {
3241
continue
3342
}
3443
const list = versions.get(parsed.name) ?? []
35-
list.push({ dir: full, version: parsed.version, label: entry })
44+
list.push({ dir: full, version: parsed.version })
3645
versions.set(parsed.name, list)
3746
}
3847

3948
const selections = new Map<string, Entry>()
4049

4150
for (const [slug, list] of versions) {
42-
list.sort((a, b) => -Bun.semver.order(a.version, b.version))
43-
selections.set(slug, list[0])
51+
list.sort((a, b) => {
52+
const aValid = isValidSemver(a.version)
53+
const bValid = isValidSemver(b.version)
54+
if (aValid && bValid) return -Bun.semver.order(a.version, b.version)
55+
if (aValid) return -1
56+
if (bValid) return 1
57+
return b.version.localeCompare(a.version)
58+
})
59+
const first = list[0]
60+
if (first) selections.set(slug, first)
4461
}
4562

4663
await rm(linkRoot, { recursive: true, force: true })

0 commit comments

Comments
 (0)