Server initialization failed when injecting custom service #733
-
Hi guys, I'm having troubles with custom service, the server won't initialize when I inject the custom service. I'm trying to implement a custom service (for code generation), and it currently looks like the following. import { OutputChannel, window } from 'vscode';
import { AstNode, LangiumParser } from "langium";
import { MediatorServices } from "./mediator-module";
import { isProgram, Program } from './generated/ast';
export interface Generator {
generate(program: string | AstNode): string | undefined;
}
/**
* Generate nuXmv model from Mediator program
*/
export class MediatorGenerator implements Generator {
private readonly parser: LangiumParser;
constructor(services: MediatorServices) {
this.parser = services.parser.LangiumParser;
}
generate(program: string | AstNode): string | undefined {
const astNode = (typeof (program) == 'string' ? this.parser.parse(program).value : program);
const out = window.createOutputChannel("test running");
out.show();
if (isProgram(astNode)) {
this.walkProgram(astNode, out);
} else {
out.appendLine(astNode.$type);
}
return astNode.$type;
}
walkProgram(program: Program, out: OutputChannel): void {
// a program contains: type aliases, const definitions, functions, automatons, systems
for (let typedef of program.typedefs) {
out.appendLine("type alias: " + typedef.alias);
}
}
} The code for service injection is as follows (basically the same as the generated template). export type MediatorAddedServices = {
generation: {
MediatorGenerator: Generator
}
}
export type MediatorServices = LangiumServices & MediatorAddedServices
export const MediatorModule: Module<MediatorServices, PartialLangiumServices & MediatorAddedServices> = {
generation: {
MediatorGenerator: (services) => new MediatorGenerator(services)
}
}; And when I launch the extension, it reports three errors:
The most strange thing is that, I implemented this custom yesterday and tested it, it was fine. But today it reports the errors before any change to the code (I did not upgrade vscode, the version is 1.72.2). I tried to understand how it was going wrong, and I found that as soon as I removed the injected service, there would be no error. #509 reported a similar issue, but it was for langium v0.3, and I am using v0.5. Also, googling the reported errors does not seem to provide much helpful information. I guess there must be wrong with my custom service implementation, and I was just lucky yesterday so no errors popped up. So, how can I fix this problem, and is there something that I should know when implementing custom service? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @luan-xiaokun it's not necessarily related to your implementation, but rather what you're importing. You seem to be importing from
It's only available in the extension host process. Since language servers run in a separate process, the |
Beta Was this translation helpful? Give feedback.
Hey @luan-xiaokun
it's not necessarily related to your implementation, but rather what you're importing. You seem to be importing from
vscode
which is the source of the error, see:It's only available in the extension host process. Since language servers run in a separate process, the
vscode
dependency isn't actually exposed to the node.js runtime. Be aware that thevscode
import isn't a "real" import/package, but is rather injected into the process by vscode.