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..805947825 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,33 @@ 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". 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) { 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 (isDuplicateBundle(bundlePath, extension.id, serverJars, contributors)) { + continue; + } + contributors.set(path.basename(bundlePath), extension.id); + result.push(bundlePath); } } } @@ -68,6 +90,37 @@ 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'))); + } 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..85cf003c6 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,60 @@ 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 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);