Skip to content

Commit

Permalink
SLVSCODE-1039 Refactor async start of language server
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Feb 27, 2025
1 parent 9d0d252 commit a4427cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/cfamily/ondemand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function getOnDemandAnalyzersPath() {
return path.resolve(util.extensionPath, '..', 'sonarsource.sonarlint_ondemand-analyzers');
}

export function maybeAddCFamilyJar(params: string[]) {
export async function maybeAddCFamilyJar(params: string[]) {
const expectedVersion: string = util.packageJson.jarDependencies.filter(dep => dep.artifactId === CFAMILY_PLUGIN_ID)[0].version;
const maybeCFamilyJar = path.resolve(getOnDemandAnalyzersPath(), CFAMILY_PLUGIN_ID, expectedVersion, CFAMILY_JAR);
if (fs.existsSync(maybeCFamilyJar)) {
params.push(maybeCFamilyJar);
util.extensionContext.globalState.update(lastUsedKey(CFAMILY_PLUGIN_ID, expectedVersion), DateTime.now().toMillis());
await util.extensionContext.globalState.update(lastUsedKey(CFAMILY_PLUGIN_ID, expectedVersion), DateTime.now().toMillis());
cleanupOldAnalyzersAsync();
} else {
// Async call is expected here
Expand Down
49 changes: 22 additions & 27 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,29 @@ let helpAndFeedbackTreeDataProvider: HelpAndFeedbackTreeDataProvider;
let helpAndFeedbackView: VSCode.TreeView<HelpAndFeedbackLink>;
let taintVulnerabilityCollection: VSCode.DiagnosticCollection;

function runJavaServer(context: VSCode.ExtensionContext): Promise<StreamInfo> {
return resolveRequirements(context)
.catch(error => {
//show error
VSCode.window.showErrorMessage(error.message, error.label).then(selection => {
if (error.label && error.label === selection && error.command) {
VSCode.commands.executeCommand(error.command, error.commandParam);
}
});
// rethrow to disrupt the chain.
throw error;
})
.then(requirements => {
return new Promise<StreamInfo>((resolve, reject) => {
const { command, args } = languageServerCommand(context, requirements);
logToSonarLintOutput(`Executing ${command} ${args.join(' ')}`);
const process = ChildProcess.spawn(command, args);

process.stderr.on('data', function (data) {
logWithPrefix(data, '[stderr]');
});

resolve({
reader: process.stdout,
writer: process.stdin
});
});
async function runJavaServer(context: VSCode.ExtensionContext): Promise<StreamInfo> {
try {
const requirements = await resolveRequirements(context);
const { command, args } = await languageServerCommand(context, requirements);
logToSonarLintOutput(`Executing ${command} ${args.join(' ')}`);
const process = ChildProcess.spawn(command, args);
process.stderr.on('data', function (data) {
logWithPrefix(data, '[stderr]');
});
return {
reader: process.stdout,
writer: process.stdin
}
} catch (error) {
//show error
VSCode.window.showErrorMessage(error.message, error.label).then(selection => {
if (error.label && error.label === selection && error.command) {
VSCode.commands.executeCommand(error.command, error.commandParam);
}
});
// rethrow to disrupt the chain.
throw error;
}
}

function logWithPrefix(data, prefix) {
Expand Down
4 changes: 2 additions & 2 deletions src/lsp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { maybeAddCFamilyJar } from '../cfamily/ondemand';
declare let v8debug: object;
const DEBUG = typeof v8debug === 'object' || util.startedInDebugMode(process);

export function languageServerCommand(
export async function languageServerCommand(
context: VSCode.ExtensionContext,
requirements: RequirementsData
) {
Expand Down Expand Up @@ -45,7 +45,7 @@ export function languageServerCommand(
params.push(Path.resolve(context.extensionPath, 'analyzers', 'sonartext.jar'));
params.push(Path.resolve(context.extensionPath, 'analyzers', 'sonariac.jar'));
params.push(Path.resolve(context.extensionPath, 'analyzers', 'sonarlintomnisharp.jar'));
maybeAddCFamilyJar(params);
await maybeAddCFamilyJar(params);

return { command: javaExecutablePath, args: params, transport: TransportKind.stdio };
}
Expand Down

0 comments on commit a4427cd

Please sign in to comment.