Skip to content
Merged
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
10 changes: 8 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
const requireStandardServer = (serverMode !== ServerMode.lightWeight) && (!isDebugModeByClientPort || !!process.env['JDTLS_CLIENT_PORT']);
let initFailureReported: boolean = false;

const javaConfig = await getJavaConfig(requirementsData.java_home);
const javaConfig = await getJavaConfig(requirementsData.java_home, true);
javaConfigDeferred.resolve(javaConfig);

// Options to control the language client
Expand Down Expand Up @@ -790,6 +790,12 @@ async function doStartStandardServer(context: ExtensionContext, requirements: re
await standardClient.initialize(context, requirements, clientOptions, workspacePath, jdtEventEmitter);
standardClient.start().then(async () => {
standardClient.registerLanguageClientActions(context, await fse.pathExists(path.join(workspacePath, ".metadata", ".plugins")), jdtEventEmitter);
// Send auto-detected JDKs after server startup to avoid blocking initialization
standardClient.getClient().sendNotification(DidChangeConfigurationNotification.type, {
settings: {
java: await getJavaConfig(requirements.java_home),
}
});
});
serverStatusBarProvider.setBusy("Activating...");
return standardClient.getClient();
Expand Down Expand Up @@ -1269,7 +1275,7 @@ async function cleanJavaWorkspaceStorage() {
}

async function cleanOldGlobalStorage(context: ExtensionContext) {
const currentVersion = getVersion(context.extensionPath);
const currentVersion = getVersion(context);
const globalStoragePath = context.globalStorageUri?.fsPath; // .../Code/User/globalStorage/redhat.java

ensureExists(globalStoragePath);
Expand Down
4 changes: 2 additions & 2 deletions src/javaServerStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function prepareParams(requirements: RequirementsData, workspacePath, context: E
}

const hasJDWP = params.find((param: string) => param.includes('jdwp')) !== undefined;
const extVersion = getVersion(context.extensionPath);
const extVersion = getVersion(context);
const globalStoragePath = path.resolve(context.globalStorageUri?.fsPath, extVersion); // .../Code/User/globalStorage/redhat.java/1.42.0/
const appCDSMode = workspace.getConfiguration().get('java.jdt.ls.appcds.enabled');
const useAppCDS = (appCDSMode === 'on') || (appCDSMode === 'auto' && (isPrereleaseOrInsiderVersion(context)));
Expand Down Expand Up @@ -396,7 +396,7 @@ export function getSharedIndexCache(context: ExtensionContext): string {

function resolveConfiguration(context, configDir) {
ensureExists(context.globalStoragePath);
const version = getVersion(context.extensionPath);
const version = getVersion(context);
let configuration = path.resolve(context.globalStoragePath, version);
ensureExists(configuration);
configuration = path.resolve(configuration, configDir);
Expand Down
5 changes: 2 additions & 3 deletions src/jdkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { existsSync } from 'fs';
import { IJavaRuntime, findRuntimes, getSources } from 'jdk-utils';
import { join } from 'path';
import { ExtensionContext, Uri, workspace } from 'vscode';
import { ExtensionContext } from 'vscode';

let cachedJdks: IJavaRuntime[];
let cachedJreNames: string[];
Expand All @@ -14,8 +14,7 @@ export async function loadSupportedJreNames(context: ExtensionContext): Promise<
}

async function getContributesConfiguration(context: ExtensionContext, configId: string): Promise<any> {
const buffer = await workspace.fs.readFile(Uri.file(context.asAbsolutePath("package.json")));
const packageJson = JSON.parse(buffer.toString());
const packageJson = context.extension.packageJSON;
/**
* contributes.configuration can either be a single object,
* representing a single category of settings, or an array
Expand Down
14 changes: 5 additions & 9 deletions src/requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import expandHomeDir from 'expand-home-dir';
import * as fse from 'fs-extra';
import { getRuntime, getSources, JAVAC_FILENAME, JAVA_FILENAME } from 'jdk-utils';
import { getRuntime, JAVAC_FILENAME, JAVA_FILENAME } from 'jdk-utils';
import * as path from 'path';
import { env, ExtensionContext, Uri, window, workspace } from 'vscode';
import { Commands } from './commands';
Expand Down Expand Up @@ -75,10 +75,10 @@ export async function resolveRequirements(context: ExtensionContext): Promise<Re
}
}

// search valid JDKs from env.JAVA_HOME, env.PATH, SDKMAN, jEnv, jabba, Common directories
const javaRuntimes = await listJdks();
if (!toolingJre) { // universal version
// search valid JDKs from env.JAVA_HOME, env.PATH, SDKMAN, jEnv, jabba, Common directories
// as latest version as possible.
const javaRuntimes = await listJdks();
sortJdksByVersion(javaRuntimes);
const validJdks = javaRuntimes.filter(r => r.version.major >= REQUIRED_JDK_VERSION);
if (validJdks.length > 0) {
Expand All @@ -100,16 +100,12 @@ export async function resolveRequirements(context: ExtensionContext): Promise<Re
*/
if (javaHome) {
logger.info(`Use the JDK from '${preferenceName}' setting as the initial default project JDK.`);
} else if (javaRuntimes.length) {
sortJdksBySource(javaRuntimes);
javaHome = javaRuntimes[0].homedir;
javaVersion = javaRuntimes[0].version?.major;
logger.info(`Use the JDK from '${getSources(javaRuntimes[0])}' as the initial default project JDK.`);
} else if (javaHome = await findDefaultRuntimeFromSettings()) {
javaVersion = await getMajorVersion(javaHome);
logger.info("Use the JDK from 'java.configuration.runtimes' as the initial default project JDK.");
} else {
openJDKDownload(reject, "Please download and install a JDK to compile your project. You can configure your projects with different JDKs by the setting ['java.configuration.runtimes'](https://github.com/redhat-developer/vscode-java/wiki/JDK-Requirements#java.configuration.runtimes)");
javaHome = toolingJre;
javaVersion = toolingJreVersion;
}
}

Expand Down
23 changes: 8 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function getDirectoriesByBuildFile(inclusions: string[], exclusions: string[], f

const detectJdksAtStart: boolean = getJavaConfiguration().get<boolean>('configuration.detectJdksAtStart');

export async function getJavaConfig(javaHome: string) {
export async function getJavaConfig(javaHome: string, skipAutoDetection?: boolean) {
const origConfig = getJavaConfiguration();
const javaConfig = JSON.parse(JSON.stringify(origConfig));
javaConfig.home = javaHome;
Expand Down Expand Up @@ -274,7 +274,7 @@ export async function getJavaConfig(javaHome: string) {
}

javaConfig.telemetry = { enabled: workspace.getConfiguration('redhat.telemetry').get('enabled', false) };
if (detectJdksAtStart) {
if (detectJdksAtStart && !skipAutoDetection) {
const userConfiguredJREs: any[] = javaConfig.configuration.runtimes;
javaConfig.configuration.runtimes = await addAutoDetectedJdks(userConfiguredJREs);
}
Expand Down Expand Up @@ -344,14 +344,8 @@ 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';
export function getVersion(context: ExtensionContext): string {
return context.extension.packageJSON?.version || '0.0.0';
}

export function getVSCodeVariablesMap(): any {
Expand All @@ -368,19 +362,18 @@ export function getVSCodeVariablesMap(): any {
* @param context The extension context or extension path
* @returns true if the version is a pre-release version or running an insider editor
*/
export function isPrereleaseOrInsiderVersion(context: ExtensionContext | string): boolean {
export function isPrereleaseOrInsiderVersion(context: ExtensionContext): boolean {
return isInsiderEditor() || isPreReleaseVersion(context);
}

/**
* Check if the extension version is a pre-release version.
* Pre-release versions follow the pattern: major.minor.timestamp (e.g., 1.47.1234567890)
* @param context The extension context or extension path
* @param context The extension context
* @returns true if the version is a pre-release version
*/
export function isPreReleaseVersion(context: ExtensionContext | string): boolean {
const extensionPath = typeof context === 'string' ? context : context.extensionPath;
const extVersion = getVersion(extensionPath);
export function isPreReleaseVersion(context: ExtensionContext): boolean {
const extVersion = getVersion(context);
return /^\d+\.\d+\.\d{10}/.test(extVersion);
}

Expand Down
Loading