Skip to content

Commit 755b1ee

Browse files
fix: Invalidate detected Package Manager cache when a lock file is updated, created or deleted
This way it is possible to guarantee changes in the Package Manager that may have occurred (eg. changed from NPM to PNPM)
1 parent c704175 commit 755b1ee

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/Diagnostic.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getPackageManager,
3333
getPackagesAdvisories,
3434
PackageManager,
35+
packageManagerCaches,
3536
PackagesAdvisories,
3637
packagesInstalledCaches,
3738
} from "./PackageManager"
@@ -82,19 +83,24 @@ export const diagnosticSubscribe = (
8283
)
8384

8485
// Trigger when any file in the workspace is modified.
85-
// Our interest here is to know about package-lock.json.
86-
context.subscriptions.push(
87-
workspace
88-
.createFileSystemWatcher("**/{package-lock.json,pnpm-lock.yaml}")
89-
.onDidChange((uri: Uri) => {
90-
packagesInstalledCaches.get(getWorkspacePath(uri))?.invalidate()
91-
92-
window.visibleTextEditors.forEach((editor) =>
93-
handleChange(editor.document)
94-
)
95-
})
86+
// Our interest here is to know about package-lock.json or pnpm-lock.yaml.
87+
const lockerWatcher = workspace.createFileSystemWatcher(
88+
"**/{package-lock.json,pnpm-lock.yaml}"
9689
)
9790

91+
const lockerUpdated = (uri: Uri) => {
92+
const workspacePath = getWorkspacePath(uri)
93+
94+
packageManagerCaches.get(workspacePath)?.invalidate()
95+
packagesInstalledCaches.get(workspacePath)?.invalidate()
96+
97+
window.visibleTextEditors.forEach((editor) => handleChange(editor.document))
98+
}
99+
100+
context.subscriptions.push(lockerWatcher.onDidCreate(lockerUpdated))
101+
context.subscriptions.push(lockerWatcher.onDidChange(lockerUpdated))
102+
context.subscriptions.push(lockerWatcher.onDidDelete(lockerUpdated))
103+
98104
// Trigger when the active document is closed, removing the current document from the diagnostic collection.
99105
context.subscriptions.push(
100106
workspace.onDidCloseTextDocument((document: TextDocument) => {

src/PackageManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const supportsPackageManager = async (
111111
})
112112
}
113113

114-
const packageManagerCaches = new Map<
114+
export const packageManagerCaches = new Map<
115115
string,
116116
Cache<PackageManager | undefined>
117117
>()

0 commit comments

Comments
 (0)