Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more logging, better preprocessing error message #150

Merged
merged 2 commits into from Jun 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions packages/language-server/src/plugins/svelte/SveltePlugin.ts
Expand Up @@ -33,6 +33,9 @@ const DEFAULT_OPTIONS: CompileOptions = {
dev: true,
};

const scssNodeRuntimeHint =
'If you use SCSS, it may be necessary to add the path to your NODE runtime to the setting `svelte.language-server.runtime`. ';

export class SveltePlugin
implements DiagnosticsProvider, FormattingProvider, CompletionsProvider, HoverProvider {
private docManager = new Map<Document, SvelteDocument>();
Expand All @@ -46,15 +49,28 @@ export class SveltePlugin

try {
return await this.tryGetDiagnostics(document);
} catch (e) {
} catch (error) {
Logger.error('Preprocessing failed');
Logger.error(error);
// Preprocessing could fail if packages like less/sass/babel cannot be resolved
// when our fallback-version of svelte-preprocess is used.
// Add a warning about a broken svelte.configs.js/preprocessor setup
// Also add svelte-preprocess error message.
const errorMsg =
error instanceof Error && error.message.startsWith('Cannot find any of modules')
? error.message + '. '
: '';
const hint =
error instanceof Error && error.message.includes('node-sass')
? scssNodeRuntimeHint
: '';
return [
{
message:
errorMsg +
"The file cannot be parsed because script or style require a preprocessor that doesn't seem to be setup. " +
'Did you setup a `svelte.config.js`? ' +
hint +
'See https://github.com/sveltejs/language-tools/tree/master/packages/svelte-vscode#using-with-preprocessors for more info.',
range: Range.create(Position.create(0, 0), Position.create(0, 5)),
severity: DiagnosticSeverity.Warning,
Expand Down Expand Up @@ -117,7 +133,7 @@ export class SveltePlugin
} else {
diagnostic.message +=
'If you use less/SCSS with `svelte-preprocessor`, did you add `lang="scss"`/`lang="less"` to you `style` tag? ' +
'If you use SCSS, it may be necessary to add the path to your NODE runtime to the setting `svelte.language-server.runtime`. ';
scssNodeRuntimeHint;
}
diagnostic.message +=
'Did you setup a `svelte.config.js`? ' +
Expand All @@ -129,13 +145,18 @@ export class SveltePlugin
}

private async loadConfig(document: Document): Promise<SvelteConfig> {
Logger.log('loading config for', document.getFilePath());
Logger.log('Trying to load config for', document.getFilePath());
try {
const explorer = cosmiconfig('svelte', { packageProp: 'svelte-ls' });
const result = await explorer.search(document.getFilePath() || '');
const config = result?.config ?? this.useFallbackPreprocessor(document);
if (result) {
Logger.log('Found config at ', result.filepath);
}
return { ...DEFAULT_OPTIONS, ...config };
} catch (err) {
Logger.error('Error while loading config');
Logger.error(err);
return { ...DEFAULT_OPTIONS, ...this.useFallbackPreprocessor(document) };
}
}
Expand Down