Repository navigation
Closed
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeDomain: APIRelates to the public API for TypeScriptRelates to the public API for TypeScriptWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Milestone
Description
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
Metadata
Metadata
Assignees
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeDomain: APIRelates to the public API for TypeScriptRelates to the public API for TypeScriptWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Activity
sheetalkamat commentedon Jun 19, 2017
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
And it is compiling correctly.
cc Daniel Rosenwasser (@DanielRosenwasser)
cevek commentedon Jun 19, 2017
I just copy pasted example from here https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
sheetalkamat commentedon Jun 20, 2017
Daniel Rosenwasser (@DanielRosenwasser) is updating the example
DanielRosenwasser commentedon Jun 20, 2017
Done!
sheetalkamat commentedon Jun 20, 2017
Leave a comment
cevek commentedon Jun 20, 2017
Sheetal Nandi (@sheetalkamat) Thank you