Skip to content

Commit

Permalink
show config errors in svelte-check
Browse files Browse the repository at this point in the history
Only editor shows the config error of referenced project so don't push it to the configErrors
  • Loading branch information
jasonlyu123 committed Aug 21, 2024
1 parent 0b0f2a4 commit eec3b2b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
34 changes: 14 additions & 20 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const configFileForOpenFiles = new FileMap<string>();
const pendingReloads = new FileSet();
const documentRegistries = new Map<string, ts.DocumentRegistry>();
const pendingForAllServices = new Set<Promise<void>>();
const projectReferenceInfo = new FileMap<TsConfigInfo | null>();
const parsedTsConfigInfo = new FileMap<TsConfigInfo | null>();

/**
* For testing only: Reset the cache for services.
Expand All @@ -121,7 +121,7 @@ const projectReferenceInfo = new FileMap<TsConfigInfo | null>();
*/
export function __resetCache() {
services.clear();
projectReferenceInfo.clear();
parsedTsConfigInfo.clear();
serviceSizeMap.clear();
configFileForOpenFiles.clear();
}
Expand Down Expand Up @@ -153,7 +153,7 @@ export async function getService(

const fileExistsWithCache = (fileName: string) => {
return (
(projectReferenceInfo.has(fileName) && !pendingReloads.has(fileName)) ||
(parsedTsConfigInfo.has(fileName) && !pendingReloads.has(fileName)) ||
docContext.tsSystem.fileExists(fileName)
);
};
Expand Down Expand Up @@ -286,7 +286,7 @@ export async function getServiceForTsconfig(
if (reloading || !services.has(tsconfigPathOrWorkspacePath)) {
if (reloading) {
Logger.log('Reloading ts service at ', tsconfigPath, ' due to config updated');
projectReferenceInfo.delete(tsconfigPath);
parsedTsConfigInfo.delete(tsconfigPath);
} else {
Logger.log('Initialize new ts service at ', tsconfigPath);
}
Expand All @@ -313,9 +313,8 @@ async function createLanguageService(
): Promise<LanguageServiceContainer> {
const { tsSystem } = docContext;

const configErrors: ts.Diagnostic[] = [];
const projectConfig = getParsedConfig();
const { options: compilerOptions, raw } = projectConfig;
const { options: compilerOptions, raw, errors: configErrors } = projectConfig;

const getCanonicalFileName = createGetCanonicalFileName(tsSystem.useCaseSensitiveFileNames);
watchWildCardDirectories(projectConfig);
Expand Down Expand Up @@ -442,7 +441,7 @@ async function createLanguageService(
parsedCommandLine: ts.ParsedCommandLine,
configFileName: string
) {
const cached = configFileName ? projectReferenceInfo.get(configFileName) : undefined;
const cached = configFileName ? parsedTsConfigInfo.get(configFileName) : undefined;
if (cached?.snapshotManager) {
return cached.snapshotManager;
}
Expand Down Expand Up @@ -598,7 +597,7 @@ async function createLanguageService(
function scheduleProjectFileUpdate(watcherNewFiles: string[]): void {
if (!snapshotManager.areIgnoredFromNewFileWatch(watcherNewFiles)) {
scheduleUpdate();
const info = projectReferenceInfo.get(tsconfigPath);
const info = parsedTsConfigInfo.get(tsconfigPath);
if (info) {
info.pendingProjectFileUpdate = true;
}
Expand All @@ -608,7 +607,7 @@ async function createLanguageService(
return;
}
for (const ref of projectConfig.projectReferences) {
const config = projectReferenceInfo.get(ref.path);
const config = parsedTsConfigInfo.get(ref.path);
if (
config &&
// handled by the respective service
Expand All @@ -622,7 +621,7 @@ async function createLanguageService(
}

function ensureProjectFileUpdates(): void {
const info = projectReferenceInfo.get(tsconfigPath);
const info = parsedTsConfigInfo.get(tsconfigPath);
if (!info || !info.pendingProjectFileUpdate) {
return;
}
Expand Down Expand Up @@ -738,8 +737,8 @@ async function createLanguageService(
}

const svelteConfigDiagnostics = checkSvelteInput(parsedConfig);
if (docContext.reportConfigError) {
docContext.reportConfigError({
if (svelteConfigDiagnostics.length > 0) {
docContext.reportConfigError?.({
uri: pathToUrl(tsconfigPath),
diagnostics: svelteConfigDiagnostics.map((d) => ({
message: d.messageText as string,
Expand All @@ -748,7 +747,6 @@ async function createLanguageService(
source: 'svelte'
}))
});
} else {
parsedConfig.errors.push(...svelteConfigDiagnostics);
}

Expand Down Expand Up @@ -1035,15 +1033,15 @@ async function createLanguageService(
}

function ensureTsConfigInfoUpToDate(configFilePath: string) {
const cached = projectReferenceInfo.get(configFilePath);
const cached = parsedTsConfigInfo.get(configFilePath);
if (cached !== undefined) {
ensureFilesForConfigUpdates(cached);
return cached;
}

const content = tsSystem.fileExists(configFilePath) && tsSystem.readFile(configFilePath);
if (!content) {
projectReferenceInfo.set(configFilePath, null);
parsedTsConfigInfo.set(configFilePath, null);
return null;
}

Expand Down Expand Up @@ -1091,10 +1089,6 @@ async function createLanguageService(

parsedCommandLine.options.allowNonTsExtensions = true;

if (parsedCommandLine.errors.length) {
configErrors.push(...parsedCommandLine.errors);
}

const snapshotManager = createSnapshotManager(parsedCommandLine, configFilePath);

const tsconfigInfo: TsConfigInfo = {
Expand All @@ -1104,7 +1098,7 @@ async function createLanguageService(
configFilePath,
extendedConfigPaths
};
projectReferenceInfo.set(configFilePath, tsconfigInfo);
parsedTsConfigInfo.set(configFilePath, tsconfigInfo);

watchConfigFiles(extendedConfigPaths, parsedCommandLine);

Expand Down
47 changes: 30 additions & 17 deletions packages/language-server/src/svelte-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { JSOrTSDocumentSnapshot } from './plugins/typescript/DocumentSnapshot';
import { isInGeneratedCode } from './plugins/typescript/features/utils';
import { convertRange, getDiagnosticTag, mapSeverity } from './plugins/typescript/utils';
import { pathToUrl, urlToPath } from './utils';
import { groupBy } from 'lodash';

export type SvelteCheckDiagnosticSource = 'js' | 'css' | 'svelte';

Expand Down Expand Up @@ -188,10 +189,36 @@ export class SvelteCheck {

private async getDiagnosticsForTsconfig(tsconfigPath: string) {
const lsContainer = await this.getLSContainer(tsconfigPath);
const map = (diagnostic: ts.Diagnostic, range?: Range): Diagnostic => {
const file = diagnostic.file;
range ??= file
? convertRange(
{ positionAt: file.getLineAndCharacterOfPosition.bind(file) },
diagnostic
)
: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } };

const noInputsFoundError = lsContainer.configErrors?.find((e) => e.code === 18003);
if (noInputsFoundError) {
throw new Error(noInputsFoundError.messageText.toString());
return {
range: range,
severity: mapSeverity(diagnostic.category),
source: diagnostic.source,
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
code: diagnostic.code,
tags: getDiagnosticTag(diagnostic)
};
};

if (lsContainer.configErrors) {
const grouped = groupBy(
lsContainer.configErrors,
(error) => error.file?.fileName ?? tsconfigPath
);

return Object.entries(grouped).map(([filePath, errors]) => ({
filePath,
text: '',
diagnostics: errors.map((diagnostic) => map(diagnostic))
}));
}

const lang = lsContainer.getService();
Expand Down Expand Up @@ -219,20 +246,6 @@ export class SvelteCheck {
| undefined;
const isKitFile = snapshot?.kitFile ?? false;
const diagnostics: Diagnostic[] = [];
const map = (diagnostic: ts.Diagnostic, range?: Range) => ({
range:
range ??
convertRange(
{ positionAt: file.getLineAndCharacterOfPosition.bind(file) },
diagnostic
),
severity: mapSeverity(diagnostic.category),
source: diagnostic.source,
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
code: diagnostic.code,
tags: getDiagnosticTag(diagnostic)
});

if (!skipDiagnosticsForFile) {
const originalDiagnostics = [
...lang.getSyntacticDiagnostics(file.fileName),
Expand Down
21 changes: 15 additions & 6 deletions packages/svelte-check/src/writers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ export class HumanFriendlyWriter implements Writer {
`${workspaceDir}${sep}${pc.green(filename)}:${line + 1}:${character + 1}\n`
);

// Show some context around diagnostic range
const codePrevLine = this.getLine(diagnostic.range.start.line - 1, text);
const codeLine = this.getCodeLine(diagnostic, text);
const codeNextLine = this.getLine(diagnostic.range.end.line + 1, text);
const code = codePrevLine + codeLine + codeNextLine;

let msg;
if (this.isVerbose) {
const code = this.formatRelatedCode(diagnostic, text);
msg = `${diagnostic.message} ${source}\n${pc.cyan(code)}`;
} else {
msg = `${diagnostic.message} ${source}`;
Expand All @@ -80,6 +75,20 @@ export class HumanFriendlyWriter implements Writer {
});
}

private formatRelatedCode(diagnostic: Diagnostic, text: string) {
if (!text) {
return '';
}

// Show some context around diagnostic range
const codePrevLine = this.getLine(diagnostic.range.start.line - 1, text);
const codeLine = this.getCodeLine(diagnostic, text);
const codeNextLine = this.getLine(diagnostic.range.end.line + 1, text);
const code = codePrevLine + codeLine + codeNextLine;

return code;
}

private getCodeLine(diagnostic: Diagnostic, text: string) {
const startOffset = offsetAt(diagnostic.range.start, text);
const endOffset = offsetAt(diagnostic.range.end, text);
Expand Down

0 comments on commit eec3b2b

Please sign in to comment.