Skip to content

Commit

Permalink
fix the remove unused installations cleanup
Browse files Browse the repository at this point in the history
The deactivate function does not run when closing VS Code and it has a
time limit that is to small to finish running the version cleanup logic.

Instead, unused versions should be uninstalled every time a new version
has been installed.

The sorting was also wrong.
  • Loading branch information
Techatrix committed Jan 23, 2025
1 parent 56e39b9 commit 7d6ffa1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import vscode from "vscode";

import { activate as activateZls, deactivate as deactivateZls } from "./zls";
import { deactivate as deactivateSetupZig, setupZig } from "./zigSetup";
import ZigDiagnosticsProvider from "./zigDiagnosticsProvider";
import ZigMainCodeLensProvider from "./zigMainCodeLens";
import ZigTestRunnerProvider from "./zigTestRunnerProvider";
import { registerDocumentFormatting } from "./zigFormat";
import { setupZig } from "./zigSetup";

export async function activate(context: vscode.ExtensionContext) {
await setupZig(context).finally(() => {
Expand All @@ -31,5 +31,4 @@ export async function activate(context: vscode.ExtensionContext) {

export async function deactivate() {
await deactivateZls();
await deactivateSetupZig();
}
16 changes: 14 additions & 2 deletions src/versionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ async function installFromMirror(

await chmod(exeUri.fsPath, 0o755);

try {
await removeUnusedInstallations(config);
} catch (err) {
if (err instanceof Error) {
void vscode.window.showWarningMessage(
`Failed to uninstall unused ${config.title} versions: ${err.message}`,
);
} else {
void vscode.window.showWarningMessage(`Failed to uninstall unused ${config.title} versions`);
}
}

return exeUri.fsPath;
},
);
Expand Down Expand Up @@ -249,7 +261,7 @@ async function setLastAccessTime(config: Config, version: semver.SemVer): Promis
}

/** Remove installations with the oldest last access time until at most `VersionManager.maxInstallCount` versions remain. */
export async function removeUnusedInstallations(config: Config) {
async function removeUnusedInstallations(config: Config) {
const storageDir = vscode.Uri.joinPath(config.context.globalStorageUri, config.exeName);

const keys: { key: string; installDir: vscode.Uri; lastAccessTime: number }[] = [];
Expand All @@ -275,7 +287,7 @@ export async function removeUnusedInstallations(config: Config) {
throw e;
}

keys.sort((lhs, rhs) => lhs.lastAccessTime - rhs.lastAccessTime);
keys.sort((lhs, rhs) => rhs.lastAccessTime - lhs.lastAccessTime);

for (const item of keys.slice(maxInstallCount)) {
await vscode.workspace.fs.delete(item.installDir, { recursive: true, useTrash: false });
Expand Down
4 changes: 0 additions & 4 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,3 @@ export async function setupZig(context: vscode.ExtensionContext) {

await refreshZigInstallation();
}

export async function deactivate() {
await versionManager.removeUnusedInstallations(versionManagerConfig);
}
1 change: 0 additions & 1 deletion src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,5 +460,4 @@ export async function activate(context: vscode.ExtensionContext) {

export async function deactivate(): Promise<void> {
await stopClient();
await versionManager.removeUnusedInstallations(versionManagerConfig);
}

0 comments on commit 7d6ffa1

Please sign in to comment.