Skip to content

please clarify language service import/reference resolution and set-of-files handling #1786

@clausreinke

Description

@clausreinke

I'm in the process of updating a language service client and re-provider (provides access to language services from the commandline, for editor plugins) from codeplex to github versions of typescript, and once again I'm struggling with the sparse documentation.

The variety of interfaces is confusing. One the one hand, there are the compiler vs language service differences, eg, CompilerHost vs ServiceHost, Program vs DocumentRegistry, on the other hand, there seem to be way too many ways of storing sets of file (with associated information).

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API helps a little bit, but inline docs and a concept map would be helpful, explaining why each of those is necessary and how they relate to each other.

It would be great to have a place to discuss language services API design and usage.

My first concrete question is about import/reference resolution: how do I integrate that with a language service client, without duplicating code?

There used to be a Resolver in the compiler sources that could be used to do nothing but close a set of filenames under import, reference, and filename normalization. That was easy to use.

Now, that functionality seems to be hidden in createProgram. createLanguageService internally uses createProgram, without exposing the Program, but it seems to have no less than four sources of truth about files in the project: the documentRegistry, which seems to be an externally visible copy of the internal sourceFilesByName, in addition to which there are host and hostCache (and program, which is read-only-connected to sourceFilesByName via its compilerHost).

That leaves me with two problems:

  1. after createProgram, program.getSourceFiles might have more files than sourceFilesByName (added through import/reference resolution) and I don't see how this information is propagated?

    if I get this right, synchronizeHostData should nearly always do unnecessary work, and will not bring program.getSourceFiles in sync with sourceFilesByName; the language service API functions then use program.getSourceFiles, which is a superset of sourceFilesByName, which should be correct if possibly inefficient (since each of them calls synchronizeHostData)

  2. once I have a language service, how do I modify its set of files, taking import/reference resolution into account, without recreating the whole thing from scratch?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions