Skip to content

Something broken in language server in 2.5 #16616

Closed

Description

@cevek

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

var ts = require('typescript');
var fs = require('fs');
var files = {};
var rootFileNames = ['1.tsx'];
rootFileNames.forEach(fileName => {
    files[fileName] = { version: 0 };
});

var options = {
    "module": 1,
    "target": 1,
    "jsx": 2,
    "configFilePath": "tsconfig.json"
}
var fileName = rootFileNames[0];
const servicesHost = {
    getScriptFileNames: () => rootFileNames,
    getScriptVersion: (fileName) => files[fileName] && files[fileName].version.toString(),
    getScriptSnapshot: (fileName) => {
        if (!fs.existsSync(fileName)) {
            return undefined;
        }
        return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString());
    },
    getCurrentDirectory: () => process.cwd(),
    getCompilationSettings: () => options,
    getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
};

const services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry())
var output = services.getEmitOutput(fileName);
var diagnostics = services.getCompilerOptionsDiagnostics()
    .concat(services.getSyntacticDiagnostics(fileName))
    .concat(services.getSemanticDiagnostics(fileName));

console.log(diagnostics);

1.tsx

import * as React from 'react';
<div className="foo"></div>

Expected behavior:
no error

Actual behavior:
messageText: 'Cannot find module 'react'.',

It works with 2.4

Activity

  1. added
    Bug
    Needs More Info
    on Jun 19, 2017
  2. ghost removed
    Needs More Info
    on Jun 19, 2017
  3. sheetalkamat commented on Jun 19, 2017

    @sheetalkamat
    Member

    This is working as intended. As documented here, language service host needs to implement fileExists, readFile and readDirectory to be able to correctly resolve the modules (otherwise only ambient modules would be resolved). We were earlier using getScriptSnapshot internally to check if the file exists which was not the intended use of that function.
    I changed the servicesHost in your declaration as

    const servicesHost = {
        getScriptFileNames: () => rootFileNames,
        getScriptVersion: (fileName) => files[fileName] && files[fileName].version.toString(),
        getScriptSnapshot: (fileName) => {
            if (!fs.existsSync(fileName)) {
                return undefined;
            }
            return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString());
        },
        getCurrentDirectory: () => process.cwd(),
        getCompilationSettings: () => options,
        getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
        fileExists: ts.sys.fileExists,
        readFile: ts.sys.readFile,
        readDirectory: ts.sys.readDirectory,
    };

    And it is compiling correctly.

    cc Daniel Rosenwasser (@DanielRosenwasser)

  4. sheetalkamat commented on Jun 20, 2017

    @sheetalkamat
    Member
  5. DanielRosenwasser commented on Jun 20, 2017

    @DanielRosenwasser
    Member

    Done!

  6. added
    Working as Intended
    on Jun 20, 2017
  7. sheetalkamat commented on Jun 20, 2017

    @sheetalkamat
    Member

    Leave a comment

  8. cevek commented on Jun 20, 2017

    @cevek
    Author
  9. added
    Breaking Change
    Domain: API
    and removed
    Bug
    on Sep 25, 2017
  10. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Breaking ChangeDomain: APIWorking as Intended

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions