Skip to content

Improve error logging from the Language Service #253

@mizdra

Description

@mizdra

Summary

I use Volar.js to implement the TypeScript Language Service Plugin. To do this, I have created an object that satisfies import('@volar/language-core').LanguagePlugin<string>. However, I have written code in the LanguagePlugin.getLanguageId function that causes a runtime error.

I would have expected the error to be contained in the tsserver.log. However, it was not actually contained. This made it difficult to determine why the TypeScript Language Service Plugin was not working.

I would like the error to be contained in tsserver.log for debugging.

Reproduction

repository: https://github.com/mizdra/repro-volar-get-language-id-error

/// <reference types="@volar/typescript" />

const ts = require('typescript/lib/tsserverlibrary');

/**
 * @returns {import('@volar/language-core').LanguagePlugin<string>}
 */
exports.createPngLanguagePlugin = function createPngLanguagePlugin() {
  return {
    getLanguageId(scriptId) {
      throw new Error('something wrong'); // cause a runtime error
      if (scriptId.endsWith('.png')) return 'png';
      return undefined;
    },
    createVirtualCode(scriptId, languageId) {
      if (languageId !== 'png') return undefined;
      const dtsContent = `
declare const I_PNG: { src: string, width: number, height: number };
export default I_PNG;
      `.trim();
      return {
        id: 'main',
        languageId: 'png',
        snapshot: {
          getText: (start, end) => dtsContent.slice(start, end),
          getLength: () => dtsContent.length,
          getChangeRange: () => undefined,
        },
        mappings: [],
      };
    },
    typescript: {
      extraFileExtensions: [
        {
          extension: 'png',
          isMixedContent: true,
          scriptKind: ts.ScriptKind.TS,
        },
      ],
      getServiceScript(root) {
        return {
          code: root,
          extension: ts.Extension.Ts,
          scriptKind: ts.ScriptKind.TS,
        };
      },
    },
  };
}

Expected behavior

tsserver.log contains something wrong.

Actual behavior

tsserver.log does not contain something wrong.

Additional context

Functions such as LanguagePlugin.createVirtualCode, LanguagePlugin.typescript.getServiceScript seem to cause similar problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions