From 69863b08ba7872f90b00db0b748f1d8b815f5e9c Mon Sep 17 00:00:00 2001 From: uiradev <83920075+uiradev@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:27:45 -0500 Subject: [PATCH 1/2] Skip contributed bundles that the server already ships When another extension contributes a jar through contributes.javaExtensions that JDT LS already ships at the same version, the server fails to install it with "A bundle is already installed" on every start. collectJavaExtensions now leaves out a contributed jar when the server's plugins folder already has a jar with the same file name, and logs each skipped jar once per session. Fixes #4580 Signed-off-by: uiradev <83920075+uiradev@users.noreply.github.com> --- src/javaServerStarter.ts | 9 +++++- src/plugin.ts | 34 ++++++++++++++++++++-- test/standard-mode-suite/extension.test.ts | 27 +++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/javaServerStarter.ts b/src/javaServerStarter.ts index 0523d3dec..173ce623e 100644 --- a/src/javaServerStarter.ts +++ b/src/javaServerStarter.ts @@ -274,7 +274,7 @@ function prepareParams(requirements: RequirementsData, workspacePath, context: E params.push('-noverify'); } - const serverHome: string = process.env.JDT_LS_PATH || path.resolve(__dirname, '../server'); + const serverHome: string = getServerHome(); const launchersFound: Array = globSync('**/plugins/org.eclipse.equinox.launcher_*.jar', { cwd: serverHome }); if (launchersFound.length) { params.push('-jar'); params.push(path.resolve(serverHome, launchersFound[0])); @@ -302,6 +302,13 @@ function prepareParams(requirements: RequirementsData, workspacePath, context: E return params; } +/** + * The JDT LS installation folder: `JDT_LS_PATH`, or the `server` folder bundled with the extension. + */ +export function getServerHome(): string { + return process.env.JDT_LS_PATH || path.resolve(__dirname, '../server'); +} + function resolveIndexCache(context: ExtensionContext) { let enabled: string = getJavaConfiguration().get("sharedIndexes.enabled"); if (enabled === "auto") { diff --git a/src/plugin.ts b/src/plugin.ts index d35dcdaaf..7be5b1ea4 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,12 +1,17 @@ 'use strict'; +import * as fs from 'fs'; import * as vscode from 'vscode'; import * as path from 'path'; import { Commands } from './commands'; import { extensions } from 'vscode'; +import { getServerHome } from './javaServerStarter'; +import { logger } from './log'; export let existingExtensions: Array = []; export let buildFilePatterns: Array = []; +// collectJavaExtensions runs several times per session, so each skipped bundle is logged once. +const loggedSkippedBundles = new Set(); let javaShortcuts: Array | undefined; export interface IJavaShortcut { @@ -48,16 +53,32 @@ function getShortcutsRegistration(extension: vscode.Extension): Array[]): string[] { +/** + * Collects the bundles that extensions contribute through `contributes.javaExtensions`. + * + * A contributed jar is left out when the server's `plugins` folder already has a jar with the + * same file name. p2 names jars `_.jar`, so that is the same bundle at the + * same version, and installing it again fails with "A bundle is already installed". + */ +export function collectJavaExtensions(extensions: readonly vscode.Extension[], serverPluginsDir: string = path.join(getServerHome(), 'plugins')): string[] { const result = []; if (extensions && extensions.length) { + const serverJars = listJars(serverPluginsDir); for (const extension of extensions) { const contributesSection = extension.packageJSON['contributes']; if (contributesSection) { const javaExtensions = contributesSection['javaExtensions']; if (Array.isArray(javaExtensions) && javaExtensions.length) { for (const javaExtensionPath of javaExtensions) { - result.push(path.resolve(extension.extensionPath, javaExtensionPath)); + const bundlePath = path.resolve(extension.extensionPath, javaExtensionPath); + if (serverJars.has(path.basename(bundlePath))) { + if (!loggedSkippedBundles.has(bundlePath)) { + loggedSkippedBundles.add(bundlePath); + logger.info(`Skipping ${bundlePath} contributed by ${extension.id}: the server already has ${path.basename(bundlePath)}`); + } + continue; + } + result.push(bundlePath); } } } @@ -68,6 +89,15 @@ export function collectJavaExtensions(extensions: readonly vscode.Extension return result; } +function listJars(folder: string): Set { + try { + return new Set(fs.readdirSync(folder).filter(file => file.endsWith('.jar'))); + } catch { + // No server folder (for example, JDT LS started separately): skip nothing. + return new Set(); + } +} + export function collectBuildFilePattern(extensions: readonly vscode.Extension[]) { const result = []; if (extensions && extensions.length) { diff --git a/test/standard-mode-suite/extension.test.ts b/test/standard-mode-suite/extension.test.ts index 094d581e8..94ea70113 100644 --- a/test/standard-mode-suite/extension.test.ts +++ b/test/standard-mode-suite/extension.test.ts @@ -1,5 +1,6 @@ import assert from 'assert'; import * as fs from 'fs'; +import * as os from 'os'; import * as path from 'path'; import { env } from 'process'; import * as vscode from 'vscode'; @@ -201,6 +202,32 @@ suite('Java Language Extension - Standard', () => { assert(result[0].endsWith(path.normalize('./bin/java.extend.jar'))); }); + test('should skip contributed java extensions the server already ships', () => { + const serverPluginsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'server-plugins-')); + try { + fs.writeFileSync(path.join(serverPluginsDir, 'org.objectweb.asm_9.10.1.jar'), ''); + const fakedExtension = { + id: 'test', + extensionUri: null, + extensionPath: '', + isActive: true, + packageJSON: { + contributes: { + javaExtensions: ['./server/org.objectweb.asm_9.10.1.jar', './server/org.objectweb.asm.tree_9.10.1.jar'] + } + }, + exports: '', + activate: null, + extensionKind: vscode.ExtensionKind.Workspace + }; + + const result = plugin.collectJavaExtensions([fakedExtension], serverPluginsDir); + assert.deepStrictEqual(result.map(bundle => path.basename(bundle)), ['org.objectweb.asm.tree_9.10.1.jar']); + } finally { + fs.rmSync(serverPluginsDir, { recursive: true, force: true }); + } + }); + test('should parse Java version', () => { // Test boundaries assert.equal(requirements.parseMajorVersion(null), 0); From 47ae48e36a70c2c26c0992036122d887db102b8c Mon Sep 17 00:00:00 2001 From: uiradev <83920075+uiradev@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:39:36 -0500 Subject: [PATCH 2/2] Skip contributed bundles that another extension already contributes When two extensions contribute a jar through contributes.javaExtensions with the same file name, the server would fail to install the second copy with "A bundle is already installed", as it does for a jar it already ships. collectJavaExtensions now keeps the first extension's copy, in extension order, and leaves out the others, and logs each skipped copy once per session with the extension whose copy is used. Signed-off-by: uiradev <83920075+uiradev@users.noreply.github.com> --- src/plugin.ts | 35 ++++++++++++++++++---- test/standard-mode-suite/extension.test.ts | 28 +++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 7be5b1ea4..805947825 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -58,12 +58,16 @@ function getShortcutsRegistration(extension: vscode.Extension): Array_.jar`, so that is the same bundle at the - * same version, and installing it again fails with "A bundle is already installed". + * same version, and installing it again fails with "A bundle is already installed". For the same + * reason, when several extensions contribute a jar with the same file name, only the first one's + * is kept. */ export function collectJavaExtensions(extensions: readonly vscode.Extension[], serverPluginsDir: string = path.join(getServerHome(), 'plugins')): string[] { const result = []; if (extensions && extensions.length) { const serverJars = listJars(serverPluginsDir); + // The extension each collected jar's file name came from. + const contributors = new Map(); for (const extension of extensions) { const contributesSection = extension.packageJSON['contributes']; if (contributesSection) { @@ -71,13 +75,10 @@ export function collectJavaExtensions(extensions: readonly vscode.Extension if (Array.isArray(javaExtensions) && javaExtensions.length) { for (const javaExtensionPath of javaExtensions) { const bundlePath = path.resolve(extension.extensionPath, javaExtensionPath); - if (serverJars.has(path.basename(bundlePath))) { - if (!loggedSkippedBundles.has(bundlePath)) { - loggedSkippedBundles.add(bundlePath); - logger.info(`Skipping ${bundlePath} contributed by ${extension.id}: the server already has ${path.basename(bundlePath)}`); - } + if (isDuplicateBundle(bundlePath, extension.id, serverJars, contributors)) { continue; } + contributors.set(path.basename(bundlePath), extension.id); result.push(bundlePath); } } @@ -89,6 +90,28 @@ export function collectJavaExtensions(extensions: readonly vscode.Extension return result; } +/** + * Whether the server or an earlier extension (`contributors`: file name to extension ID) already + * has a jar with this bundle's file name. Each skipped path is logged once per session, naming the + * server or the extension whose copy is used; that copy itself isn't logged. + */ +function isDuplicateBundle(bundlePath: string, extensionId: string, serverJars: Set, contributors: Map): boolean { + const fileName = path.basename(bundlePath); + let reason: string; + if (serverJars.has(fileName)) { + reason = `the server already has ${fileName}`; + } else if (contributors.has(fileName)) { + reason = `${contributors.get(fileName)} already contributes ${fileName}`; + } else { + return false; + } + if (!loggedSkippedBundles.has(bundlePath)) { + loggedSkippedBundles.add(bundlePath); + logger.info(`Skipping ${bundlePath} contributed by ${extensionId}: ${reason}`); + } + return true; +} + function listJars(folder: string): Set { try { return new Set(fs.readdirSync(folder).filter(file => file.endsWith('.jar'))); diff --git a/test/standard-mode-suite/extension.test.ts b/test/standard-mode-suite/extension.test.ts index 94ea70113..85cf003c6 100644 --- a/test/standard-mode-suite/extension.test.ts +++ b/test/standard-mode-suite/extension.test.ts @@ -228,6 +228,34 @@ suite('Java Language Extension - Standard', () => { } }); + test('should keep one copy of a java extension that several extensions contribute', () => { + const serverPluginsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'server-plugins-')); + try { + const fakeExtension = (id: string, javaExtensions: string[]) => ({ + id, + extensionUri: null, + extensionPath: path.join(serverPluginsDir, id), + isActive: true, + packageJSON: { + contributes: { javaExtensions } + }, + exports: '', + activate: null, + extensionKind: vscode.ExtensionKind.Workspace + }); + const first = fakeExtension('first', ['./server/com.example.shared_1.0.0.jar']); + const second = fakeExtension('second', ['./server/com.example.shared_1.0.0.jar', './server/com.example.own_1.0.0.jar']); + + const result = plugin.collectJavaExtensions([first, second], serverPluginsDir); + assert.deepStrictEqual(result, [ + path.join(serverPluginsDir, 'first', 'server', 'com.example.shared_1.0.0.jar'), + path.join(serverPluginsDir, 'second', 'server', 'com.example.own_1.0.0.jar') + ]); + } finally { + fs.rmSync(serverPluginsDir, { recursive: true, force: true }); + } + }); + test('should parse Java version', () => { // Test boundaries assert.equal(requirements.parseMajorVersion(null), 0);