Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/javaServerStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = globSync('**/plugins/org.eclipse.equinox.launcher_*.jar', { cwd: serverHome });
if (launchersFound.length) {
params.push('-jar'); params.push(path.resolve(serverHome, launchersFound[0]));
Expand Down Expand Up @@ -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") {
Expand Down
57 changes: 55 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -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<string> = [];
export let buildFilePatterns: Array<string> = [];
// collectJavaExtensions runs several times per session, so each skipped bundle is logged once.
const loggedSkippedBundles = new Set<string>();

let javaShortcuts: Array<IJavaShortcut> | undefined;
export interface IJavaShortcut {
Expand Down Expand Up @@ -48,16 +53,33 @@ function getShortcutsRegistration(extension: vscode.Extension<any>): Array<IJava
return undefined;
}

export function collectJavaExtensions(extensions: readonly vscode.Extension<any>[]): 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 `<symbolicName>_<version>.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<any>[], serverPluginsDir: string = path.join(getServerHome(), 'plugins')): string[] {
const result = [];
if (extensions && extensions.length) {
const serverJars = listJars(serverPluginsDir);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works pretty good if an extension to vscode-java ships a jar that overlaps with the jars shipped by vscode-java itself, but what if two extensions ship the same jar?

I think it might be worth addressing this case in this PR. If you don't have time for that though, just let me know; I'm happy to merge the PR as is, since it addresses the linked bug.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Two or more extensions shipping the same jar would fail in the same way. I pushed 47ae48e: when multiple extensions contribute a jar with the same file name (bundle and version), the first extension's copy is kept, and the rest are skipped. Each skipped copy is logged along with the extension whose copy is used. There's a test for this alongside the existing one.

// The extension each collected jar's file name came from.
const contributors = new Map<string, string>();
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);
}
}
}
Expand All @@ -68,6 +90,37 @@ export function collectJavaExtensions(extensions: readonly vscode.Extension<any>
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<string>, contributors: Map<string, string>): 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<string> {
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<any>[]) {
const result = [];
if (extensions && extensions.length) {
Expand Down
55 changes: 55 additions & 0 deletions test/standard-mode-suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down