Skip to content

Commit c37d41e

Browse files
committed
fix(auto-update-checker): add bun.lock handling to invalidatePackage()
- Removes package from node_modules, package.json dependencies, AND bun.lock (workspaces.dependencies + packages) - Fixes issue where 'update available' notification appeared but actual update didn't happen on restart due to bun.lock pinning old version - Added BunLockfile interface and stripTrailingCommas helper for JSON parsing 🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
1 parent 7b54c2a commit c37d41e

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/hooks/auto-update-checker/cache.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,57 @@ import * as path from "node:path"
33
import { CACHE_DIR, PACKAGE_NAME } from "./constants"
44
import { log } from "../../shared/logger"
55

6+
interface BunLockfile {
7+
workspaces?: {
8+
""?: {
9+
dependencies?: Record<string, string>
10+
}
11+
}
12+
packages?: Record<string, unknown>
13+
}
14+
15+
function stripTrailingCommas(json: string): string {
16+
return json.replace(/,(\s*[}\]])/g, "$1")
17+
}
18+
19+
function removeFromBunLock(packageName: string): boolean {
20+
const lockPath = path.join(CACHE_DIR, "bun.lock")
21+
if (!fs.existsSync(lockPath)) return false
22+
23+
try {
24+
const content = fs.readFileSync(lockPath, "utf-8")
25+
const lock = JSON.parse(stripTrailingCommas(content)) as BunLockfile
26+
let modified = false
27+
28+
if (lock.workspaces?.[""]?.dependencies?.[packageName]) {
29+
delete lock.workspaces[""].dependencies[packageName]
30+
modified = true
31+
}
32+
33+
if (lock.packages?.[packageName]) {
34+
delete lock.packages[packageName]
35+
modified = true
36+
}
37+
38+
if (modified) {
39+
fs.writeFileSync(lockPath, JSON.stringify(lock, null, 2))
40+
log(`[auto-update-checker] Removed from bun.lock: ${packageName}`)
41+
}
42+
43+
return modified
44+
} catch {
45+
return false
46+
}
47+
}
48+
649
export function invalidatePackage(packageName: string = PACKAGE_NAME): boolean {
750
try {
851
const pkgDir = path.join(CACHE_DIR, "node_modules", packageName)
952
const pkgJsonPath = path.join(CACHE_DIR, "package.json")
1053

1154
let packageRemoved = false
1255
let dependencyRemoved = false
56+
let lockRemoved = false
1357

1458
if (fs.existsSync(pkgDir)) {
1559
fs.rmSync(pkgDir, { recursive: true, force: true })
@@ -28,7 +72,9 @@ export function invalidatePackage(packageName: string = PACKAGE_NAME): boolean {
2872
}
2973
}
3074

31-
if (!packageRemoved && !dependencyRemoved) {
75+
lockRemoved = removeFromBunLock(packageName)
76+
77+
if (!packageRemoved && !dependencyRemoved && !lockRemoved) {
3278
log(`[auto-update-checker] Package not found, nothing to invalidate: ${packageName}`)
3379
return false
3480
}

0 commit comments

Comments
 (0)