Skip to content

Commit f8979a6

Browse files
authored
Fix for delete cache error on windows (#245)
* Fix for delete cache error on windows * Added error handling * Declared constant for windows os label * Refactored code to use await and try catch pattern * Fixed indent
1 parent e4b4427 commit f8979a6

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

vscode/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ export const OPEN_JDK_VERSION_DOWNLOAD_LINKS: { [key: string]: string } = {
2424
"22": "https://download.java.net/java/GA/jdk22.0.1/c7ec1332f7bb44aeba2eb341ae18aca4/8/GPL/openjdk-22.0.1",
2525
"21": "https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2"
2626
};
27+
28+
export const NODE_WINDOWS_LABEL = "Windows_NT";

vscode/src/extension.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import { initializeRunConfiguration, runConfigurationProvider, runConfigurationN
6767
import { InputStep, MultiStepInput } from './utils';
6868
import { PropertiesView } from './propertiesView/propertiesView';
6969
import { openJDKSelectionView } from './jdkDownloader';
70-
70+
import { NODE_WINDOWS_LABEL } from './constants';
7171
const API_VERSION : string = "1.0";
7272
const SERVER_NAME : string = "Oracle Java SE Language Server";
7373
export const COMMAND_PREFIX : string = "jdk";
@@ -78,7 +78,7 @@ let nbProcess : ChildProcess | null = null;
7878
let debugPort: number = -1;
7979
let debugHash: string | undefined;
8080
let consoleLog: boolean = !!process.env['ENABLE_CONSOLE_LOG'];
81-
81+
let deactivated:boolean = true;
8282
export class NbLanguageClient extends LanguageClient {
8383
private _treeViewService: TreeViewService;
8484

@@ -330,8 +330,8 @@ class InitialPromise extends Promise<NbLanguageClient> {
330330
}
331331

332332
export function activate(context: ExtensionContext): VSNetBeansAPI {
333+
deactivated=false;
333334
let log = vscode.window.createOutputChannel(SERVER_NAME);
334-
335335
var clientResolve : (x : NbLanguageClient) => void;
336336
var clientReject : (err : any) => void;
337337

@@ -513,19 +513,26 @@ export function activate(context: ExtensionContext): VSNetBeansAPI {
513513

514514
context.subscriptions.push(vscode.commands.registerCommand(COMMAND_PREFIX + ".delete.cache", async () => {
515515
const storagePath = context.storageUri?.fsPath;
516-
if(!storagePath){
516+
if (!storagePath) {
517517
vscode.window.showErrorMessage('Cannot find workspace path');
518518
return;
519519
}
520+
520521
const userDir = path.join(storagePath, "userdir");
521522
if (userDir && fs.existsSync(userDir)) {
522-
const confirmation = await vscode.window.showInformationMessage('Are you sure you want to delete cache for this workspace?',
523+
const confirmation = await vscode.window.showInformationMessage('Are you sure you want to delete cache for this workspace and reload the window ?',
523524
'Yes', 'Cancel');
524525
if (confirmation === 'Yes') {
525-
await fs.promises.rmdir(userDir, {recursive : true});
526-
const res = await vscode.window.showInformationMessage('Cache cleared successfully for this workspace', 'Reload window');
527-
if (res === 'Reload window') {
528-
await vscode.commands.executeCommand('workbench.action.reloadWindow');
526+
try {
527+
await stopClient(client);
528+
deactivated = true;
529+
await killNbProcess(false, log);
530+
await fs.promises.rmdir(userDir, { recursive: true });
531+
await vscode.window.showInformationMessage("Cache deleted successfully", 'Reload window');
532+
} catch (err) {
533+
await vscode.window.showErrorMessage('Error deleting the cache', 'Reload window');
534+
} finally {
535+
vscode.commands.executeCommand("workbench.action.reloadWindow");
529536
}
530537
}
531538
} else {
@@ -960,7 +967,7 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
960967
if (p == nbProcess && code != 0 && code) {
961968
vscode.window.showWarningMessage(`${SERVER_NAME} exited with ` + code);
962969
}
963-
if (stdErr?.match(/Cannot find java/) || os.type() === "Windows_NT" ) {
970+
if (stdErr?.match(/Cannot find java/) || (os.type() === NODE_WINDOWS_LABEL && !deactivated) ) {
964971
vscode.window.showInformationMessage(
965972
"No JDK found!",
966973
"Download JDK and setup automatically"

0 commit comments

Comments
 (0)