diff --git a/src/extension.ts b/src/extension.ts index 8e86d8283..612d3e0af 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,7 +14,7 @@ import { ClientErrorHandler } from './clientErrorHandler'; import { Commands, CommandTitle } from './commands'; import { ClientStatus, ExtensionAPI, TraceEvent } from './extension.api'; import * as fileEventHandler from './fileEventHandler'; -import { getSharedIndexCache, HEAP_DUMP_LOCATION, prepareExecutable, removeEquinoxFragmentOnDarwinX64 } from './javaServerStarter'; +import { getSharedIndexCache, HEAP_DUMP_LOCATION, prepareExecutable, removeEquinoxFragmentOnDarwinX64, startedFromSources } from './javaServerStarter'; import { initializeLogFile, logger } from './log'; import { cleanupLombokCache } from "./lombokSupport"; import { markdownPreviewProvider } from "./markdownPreviewProvider"; @@ -30,7 +30,7 @@ import { snippetCompletionProvider } from './snippetCompletionProvider'; import { JavaClassEditorProvider } from './javaClassEditor'; import { StandardLanguageClient } from './standardLanguageClient'; import { SyntaxLanguageClient } from './syntaxLanguageClient'; -import { convertToGlob, deleteClientLog, deleteDirectory, ensureExists, getBuildFilePatterns, getExclusionGlob, getInclusionPatternsFromNegatedExclusion, getJavaConfig, getJavaConfiguration, hasBuildToolConflicts, resolveActualCause } from './utils'; +import { convertToGlob, deleteClientLog, deleteDirectory, ensureExists, getBuildFilePatterns, getExclusionGlob, getInclusionPatternsFromNegatedExclusion, getJavaConfig, getJavaConfiguration, hasBuildToolConflicts, resolveActualCause, getVersion } from './utils'; import glob = require('glob'); import { Telemetry } from './telemetry'; import { getMessage } from './errorUtils'; @@ -159,6 +159,10 @@ export async function activate(context: ExtensionContext): Promise cleanJavaWorkspaceStorage(); + if (!startedFromSources()) { // Dev mode: version may not match package.json, deleting the in-use folder + cleanOldGlobalStorage(context); + } + // https://github.com/redhat-developer/vscode-java/issues/3484 if (process.platform === 'darwin' && process.arch === 'x64') { try { @@ -1101,6 +1105,26 @@ async function cleanJavaWorkspaceStorage() { } } +async function cleanOldGlobalStorage(context: ExtensionContext) { + const currentVersion = getVersion(context.extensionPath); + const globalStoragePath = context.globalStorageUri?.fsPath; // .../Code/User/globalStorage/redhat.java + + ensureExists(globalStoragePath); + + // delete folders in .../User/globalStorage/redhat.java that are not named the current version + fs.promises.readdir(globalStoragePath).then(async (files) => { + await Promise.all(files.map(async (file) => { + const currentPath = path.join(globalStoragePath, file); + const stat = await fs.promises.stat(currentPath); + + if (stat.isDirectory() && file !== currentVersion) { + logger.info(`Removing old folder in globalStorage : ${file}`); + deleteDirectory(currentPath); + } + })); + }); +} + export function registerCodeCompletionTelemetryListener() { apiManager.getApiInstance().onDidRequestEnd((traceEvent: TraceEvent) => { if (traceEvent.type === CompletionRequest.method) { diff --git a/src/javaServerStarter.ts b/src/javaServerStarter.ts index 679097379..b8886d05c 100644 --- a/src/javaServerStarter.ts +++ b/src/javaServerStarter.ts @@ -11,7 +11,7 @@ import { logger } from './log'; import { addLombokParam, isLombokSupportEnabled } from './lombokSupport'; import { RequirementsData } from './requirements'; import { IS_WORKSPACE_VMARGS_ALLOWED, getJavaEncoding, getJavaagentFlag, getKey, isInWorkspaceFolder } from './settings'; -import { deleteDirectory, ensureExists, getJavaConfiguration, getTimestamp } from './utils'; +import { deleteDirectory, ensureExists, getJavaConfiguration, getTimestamp, getVersion } from './utils'; import { log } from 'console'; // eslint-disable-next-line no-var @@ -254,15 +254,7 @@ export function getSharedIndexCache(context: ExtensionContext): string { function resolveConfiguration(context, configDir) { ensureExists(context.globalStoragePath); - const extensionPath = path.resolve(context.extensionPath, "package.json"); - const packageFile = JSON.parse(fs.readFileSync(extensionPath, 'utf8')); - let version; - if (packageFile) { - version = packageFile.version; - } - else { - version = '0.0.0'; - } + const version = getVersion(context.extensionPath); let configuration = path.resolve(context.globalStoragePath, version); ensureExists(configuration); configuration = path.resolve(configuration, configDir); @@ -288,7 +280,7 @@ function startedInDebugMode(): boolean { return hasDebugFlag(args); } -function startedFromSources(): boolean { +export function startedFromSources(): boolean { return process.env['DEBUG_VSCODE_JAVA'] === 'true'; } diff --git a/src/utils.ts b/src/utils.ts index c3ff4805f..8d342b48f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -316,3 +316,13 @@ export function resolveActualCause(callstack: any): any { return callstack; } + +export function getVersion(extensionPath: string): string { + const packagePath = path.resolve(extensionPath, "package.json"); + const packageFile = JSON.parse(fs.readFileSync(packagePath, 'utf8')); + if (packageFile) { + return packageFile.version; + } + + return '0.0.0'; +}