Skip to content

Commit

Permalink
Fix for delete cache error on windows (#245)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shivam71 authored Sep 5, 2024
1 parent e4b4427 commit f8979a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions vscode/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export const OPEN_JDK_VERSION_DOWNLOAD_LINKS: { [key: string]: string } = {
"22": "https://download.java.net/java/GA/jdk22.0.1/c7ec1332f7bb44aeba2eb341ae18aca4/8/GPL/openjdk-22.0.1",
"21": "https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2"
};

export const NODE_WINDOWS_LABEL = "Windows_NT";
27 changes: 17 additions & 10 deletions vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import { initializeRunConfiguration, runConfigurationProvider, runConfigurationN
import { InputStep, MultiStepInput } from './utils';
import { PropertiesView } from './propertiesView/propertiesView';
import { openJDKSelectionView } from './jdkDownloader';

import { NODE_WINDOWS_LABEL } from './constants';
const API_VERSION : string = "1.0";
const SERVER_NAME : string = "Oracle Java SE Language Server";
export const COMMAND_PREFIX : string = "jdk";
Expand All @@ -78,7 +78,7 @@ let nbProcess : ChildProcess | null = null;
let debugPort: number = -1;
let debugHash: string | undefined;
let consoleLog: boolean = !!process.env['ENABLE_CONSOLE_LOG'];

let deactivated:boolean = true;
export class NbLanguageClient extends LanguageClient {
private _treeViewService: TreeViewService;

Expand Down Expand Up @@ -330,8 +330,8 @@ class InitialPromise extends Promise<NbLanguageClient> {
}

export function activate(context: ExtensionContext): VSNetBeansAPI {
deactivated=false;
let log = vscode.window.createOutputChannel(SERVER_NAME);

var clientResolve : (x : NbLanguageClient) => void;
var clientReject : (err : any) => void;

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

context.subscriptions.push(vscode.commands.registerCommand(COMMAND_PREFIX + ".delete.cache", async () => {
const storagePath = context.storageUri?.fsPath;
if(!storagePath){
if (!storagePath) {
vscode.window.showErrorMessage('Cannot find workspace path');
return;
}

const userDir = path.join(storagePath, "userdir");
if (userDir && fs.existsSync(userDir)) {
const confirmation = await vscode.window.showInformationMessage('Are you sure you want to delete cache for this workspace?',
const confirmation = await vscode.window.showInformationMessage('Are you sure you want to delete cache for this workspace and reload the window ?',
'Yes', 'Cancel');
if (confirmation === 'Yes') {
await fs.promises.rmdir(userDir, {recursive : true});
const res = await vscode.window.showInformationMessage('Cache cleared successfully for this workspace', 'Reload window');
if (res === 'Reload window') {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
try {
await stopClient(client);
deactivated = true;
await killNbProcess(false, log);
await fs.promises.rmdir(userDir, { recursive: true });
await vscode.window.showInformationMessage("Cache deleted successfully", 'Reload window');
} catch (err) {
await vscode.window.showErrorMessage('Error deleting the cache', 'Reload window');
} finally {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
}
} else {
Expand Down Expand Up @@ -960,7 +967,7 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
if (p == nbProcess && code != 0 && code) {
vscode.window.showWarningMessage(`${SERVER_NAME} exited with ` + code);
}
if (stdErr?.match(/Cannot find java/) || os.type() === "Windows_NT" ) {
if (stdErr?.match(/Cannot find java/) || (os.type() === NODE_WINDOWS_LABEL && !deactivated) ) {
vscode.window.showInformationMessage(
"No JDK found!",
"Download JDK and setup automatically"
Expand Down

0 comments on commit f8979a6

Please sign in to comment.