Skip to content

LanguageServiceHost.getDefaultLibFileName is confusing #35318

Closed
@SimonSiefke

Description

@SimonSiefke

TypeScript Version: 3.7.2

Search Terms:
LanguageServiceHost, getDefaultLibFileName

Code

import * as ts from 'typescript';

let compilerOptions: ts.CompilerOptions = {
  allowNonTsExtensions: true,
  target: ts.ScriptTarget.Latest,
  module: ts.ModuleKind.ESNext,
  moduleResolution: ts.ModuleResolutionKind.NodeJs,
  allowJs: true
};

const serviceHost: ts.LanguageServiceHost = {
  getScriptFileNames: () => [],
  getScriptVersion: () => '0',
  getScriptSnapshot: path => ts.ScriptSnapshot.fromString(ts.sys.readFile(path)),
  getCurrentDirectory: () => '',
  getCompilationSettings: () => compilerOptions,
  getDefaultLibFileName: ts.getDefaultLibFileName, // this doesn't work
  fileExists: ts.sys.fileExists,
  readFile: ts.sys.readFile,
  readDirectory: ts.sys.readDirectory
};

Expected behavior:
I can pass ts.getDefaultLibFileName for the property getDefaultLibFileName of serviceHost.

Actual behavior:
I have to pass ts.getDefaultLibFilePath for the property getDefaultLibFileName.
When I pass ts.getDefaultLibFileName, getScriptSnapshot only has the fileName (e.g. lib.esnext.full.d.ts) as an argument instead of the full path (e.g. /home/simon/folder/node_modules/typescript/lib/lib.esnext.full.d.ts) and when getScriptSnapshot is called, it fails to read the file.

Maybe the getDefaultLibFileName property could be replaced with a getDefaultLibFilePath property, currently it is a bit confusing :/

Wiki Link: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services

Metadata

Metadata

Assignees

No one assigned

    Labels

    UnactionableThere isn't something we can do with this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions