Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external modules resolve by node_modules and package.json-main #3147

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
!tests/**/node_modules/
built/*
tests/cases/*.js
tests/cases/*/*.js
Expand Down
14 changes: 3 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,17 +859,9 @@ module ts {
}
let fileName: string;
let sourceFile: SourceFile;
while (true) {
fileName = normalizePath(combinePaths(searchPath, moduleName));
sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension));
if (sourceFile || isRelative) {
break;
}
let parentPath = getDirectoryPath(searchPath);
if (parentPath === searchPath) {
break;
}
searchPath = parentPath;
let resolvedName = host.resolveExternalModule(moduleName, searchPath);
if (resolvedName) {
sourceFile = host.getSourceFile(resolvedName);
}
if (sourceFile) {
if (sourceFile.symbol) {
Expand Down
75 changes: 62 additions & 13 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ module ts {
getCurrentDirectory: () => currentDirectory || (currentDirectory = sys.getCurrentDirectory()),
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
getCanonicalFileName,
getNewLine: () => newLine
getNewLine: () => newLine,
readFile: sys.readFile,
fileExists: sys.fileExists
};
}

Expand Down Expand Up @@ -155,6 +157,7 @@ module ts {
let commonSourceDirectory: string;
let diagnosticsProducingTypeChecker: TypeChecker;
let noDiagnosticsTypeChecker: TypeChecker;
let resolvedExternalModuleCache: Map<string> = {};

let start = new Date().getTime();

Expand Down Expand Up @@ -184,6 +187,7 @@ module ts {
getIdentifierCount: () => getDiagnosticsProducingTypeChecker().getIdentifierCount(),
getSymbolCount: () => getDiagnosticsProducingTypeChecker().getSymbolCount(),
getTypeCount: () => getDiagnosticsProducingTypeChecker().getTypeCount(),
resolveExternalModule
};
return program;

Expand Down Expand Up @@ -236,6 +240,60 @@ module ts {
emitTime += new Date().getTime() - start;
return emitResult;
}

function resolveExternalModule(moduleName: string, containingFile: string): string {
let cacheLookupName = moduleName + containingFile;
if (resolvedExternalModuleCache[cacheLookupName]) {
return resolvedExternalModuleCache[cacheLookupName];
}
if (resolvedExternalModuleCache[cacheLookupName] === '') {
return undefined;
}
function getNameIfExists(fileName: string): string {
if (host.fileExists(fileName)) {
return fileName;
}
}
while (true) {
// Look at files by all extensions
let found = forEach(supportedExtensions,
extension => getNameIfExists(normalizePath(combinePaths(containingFile, moduleName)) + extension));
// Also look at all files by node_modules
if (!found) {
found = forEach(supportedExtensions,
extension => getNameIfExists(normalizePath(combinePaths(combinePaths(containingFile, "node_modules"), moduleName)) + extension));
}
// Also look at package.json's main in node_modules
if (!found) {
// If we found a package.json then look at its main field
let pkgJson = getNameIfExists(normalizePath(combinePaths(combinePaths(combinePaths(containingFile, "node_modules"), moduleName), "package.json")));
if (pkgJson) {
let pkgFile = JSON.parse(host.readFile(pkgJson));
if (pkgFile.main) {
var indexFileName = removeFileExtension(combinePaths(getDirectoryPath(pkgJson), pkgFile.main));
found = forEach(supportedExtensions,
extension => getNameIfExists(indexFileName + extension))
}
}
}
// look at node_modules index
if (!found) {
found = forEach(supportedExtensions,
extension => getNameIfExists(normalizePath(combinePaths(combinePaths(combinePaths(containingFile, "node_modules"), moduleName), "index")) + extension));
}

// Finally cache and return or continue up the directory tree
if (found) {
return resolvedExternalModuleCache[cacheLookupName] = found;
}
let parentPath = getDirectoryPath(containingFile);
if (parentPath === containingFile) {
resolvedExternalModuleCache[cacheLookupName] = '';
return undefined;
}
containingFile = parentPath;
}
}

function getSourceFile(fileName: string) {
fileName = host.getCanonicalFileName(normalizeSlashes(fileName));
Expand Down Expand Up @@ -428,18 +486,9 @@ module ts {
if (moduleNameExpr && moduleNameExpr.kind === SyntaxKind.StringLiteral) {
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
if (moduleNameText) {
let searchPath = basePath;
let searchName: string;
while (true) {
searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) {
break;
}
let parentPath = getDirectoryPath(searchPath);
if (parentPath === searchPath) {
break;
}
searchPath = parentPath;
let resolvedName = resolveExternalModule(moduleNameText, basePath);
if (resolvedName) {
findModuleSourceFile(resolvedName, moduleNameExpr);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,8 @@ module ts {
* Gets a type checker that can be used to semantically analyze source fils in the program.
*/
getTypeChecker(): TypeChecker;

resolveExternalModule(moduleName: string, basePath: string): string;

/* @internal */ getCommonSourceDirectory(): string;

Expand Down Expand Up @@ -1133,6 +1135,7 @@ module ts {
export interface TypeCheckerHost {
getCompilerOptions(): CompilerOptions;

resolveExternalModule(moduleName: string, basePath: string): string;
getSourceFiles(): SourceFile[];
getSourceFile(fileName: string): SourceFile;
}
Expand Down Expand Up @@ -1878,6 +1881,8 @@ module ts {
getCanonicalFileName(fileName: string): string;
useCaseSensitiveFileNames(): boolean;
getNewLine(): string;
readFile(path: string): string;
fileExists(path: string): boolean;
}

export interface TextSpan {
Expand Down
4 changes: 4 additions & 0 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,8 @@ module FourSlash {
{
let host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFileName, content: undefined }],
(fn, contents) => fourslashJsOutput = contents,
(fn) => undefined,
(fn) => true,
ts.ScriptTarget.Latest,
ts.sys.useCaseSensitiveFileNames);

Expand All @@ -2252,6 +2254,8 @@ module FourSlash {
{ unitName: fileName, content: content }
],
(fn, contents) => result = contents,
(fn) => result,
(fn) => true,
ts.ScriptTarget.Latest,
ts.sys.useCaseSensitiveFileNames);

Expand Down
14 changes: 13 additions & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ module Harness {

export function createCompilerHost(inputFiles: { unitName: string; content: string; }[],
writeFile: (fn: string, contents: string, writeByteOrderMark: boolean) => void,
readFile: (fn: string) => string,
fileExists: (fn: string) => boolean,
scriptTarget: ts.ScriptTarget,
useCaseSensitiveFileNames: boolean,
// the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host
Expand Down Expand Up @@ -876,6 +878,8 @@ module Harness {
},
getDefaultLibFileName: options => defaultLibFileName,
writeFile,
readFile,
fileExists,
getCanonicalFileName,
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
getNewLine: () => newLine
Expand Down Expand Up @@ -1121,8 +1125,16 @@ module Harness {
var fileOutputs: GeneratedFile[] = [];

var programFiles = inputFiles.concat(includeBuiltFiles).map(file => file.unitName);
var program = ts.createProgram(programFiles, options, createCompilerHost(inputFiles.concat(includeBuiltFiles).concat(otherFiles),
var compilerHostInputFiles = inputFiles.concat(includeBuiltFiles).concat(otherFiles);
var program = ts.createProgram(programFiles, options, createCompilerHost(compilerHostInputFiles,
(fn, contents, writeByteOrderMark) => fileOutputs.push({ fileName: fn, code: contents, writeByteOrderMark: writeByteOrderMark }),
(fn) => {
var found = compilerHostInputFiles.filter(f=>f.unitName === fn)[0];
return found ? found.content : undefined;
},
(fn) => {
return !!compilerHostInputFiles.some(f=> f.unitName === fn)
},
options.target, useCaseSensitiveFileNames, currentDirectory, options.newLine));

var emitResult = program.emit();
Expand Down
2 changes: 2 additions & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ module Harness.LanguageService {
log(s: string): void { }
trace(s: string): void { }
error(s: string): void { }
fileExists = (fn: string) => this.getFilenames().some(serviceAdaptorFileName=> serviceAdaptorFileName === fn);
}

export class NativeLanugageServiceAdapter implements LanguageServiceAdapter {
Expand Down Expand Up @@ -236,6 +237,7 @@ module Harness.LanguageService {
log(s: string): void { this.nativeHost.log(s); }
trace(s: string): void { this.nativeHost.trace(s); }
error(s: string): void { this.nativeHost.error(s); }
fileExists(fn: string) { return this.nativeHost.fileExists(fn); }
}

class ClassifierShimProxy implements ts.Classifier {
Expand Down
41 changes: 38 additions & 3 deletions src/harness/projectsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ class ProjectRunner extends RunnerBase {

function compileProjectFiles(moduleKind: ts.ModuleKind, getInputFiles: ()=> string[],
getSourceFileText: (fileName: string) => string,
writeFile: (fileName: string, data: string, writeByteOrderMark: boolean) => void): CompileProjectFilesResult {
writeFile: (fileName: string, data: string, writeByteOrderMark: boolean) => void,
readFile: (fn: string) => string,
fileExists: (fn: string) => boolean
): CompileProjectFilesResult {

var program = ts.createProgram(getInputFiles(), createCompilerOptions(), createCompilerHost());
var errors = ts.getPreEmitDiagnostics(program);
Expand Down Expand Up @@ -186,6 +189,8 @@ class ProjectRunner extends RunnerBase {
getSourceFile,
getDefaultLibFileName: options => Harness.Compiler.defaultLibFileName,
writeFile,
readFile,
fileExists,
getCurrentDirectory,
getCanonicalFileName: Harness.Compiler.getCanonicalFileName,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
Expand All @@ -199,7 +204,7 @@ class ProjectRunner extends RunnerBase {

var outputFiles: BatchCompileProjectTestCaseEmittedFile[] = [];

var projectCompilerResult = compileProjectFiles(moduleKind, () => testCase.inputFiles, getSourceFileText, writeFile);
var projectCompilerResult = compileProjectFiles(moduleKind, () => testCase.inputFiles, getSourceFileText, writeFile, readFile, fileExists);
return {
moduleKind,
program: projectCompilerResult.program,
Expand Down Expand Up @@ -269,6 +274,22 @@ class ProjectRunner extends RunnerBase {

outputFiles.push({ emittedFileName: fileName, code: data, fileName: diskRelativeName, writeByteOrderMark: writeByteOrderMark });
}

function getFullDiskFileName(fileName: string): string {
return ts.isRootedDiskPath(fileName)
? fileName
: ts.normalizeSlashes(testCase.projectRoot) + "/" + ts.normalizeSlashes(fileName);
}

function readFile(fileName: string) {
fileName = getFullDiskFileName(fileName);
return ts.sys.readFile(fileName);
}

function fileExists(fileName: string) {
fileName = getFullDiskFileName(fileName);
return ts.sys.fileExists(fileName);
}
}

function compileCompileDTsFiles(compilerResult: BatchCompileProjectTestCaseResult) {
Expand Down Expand Up @@ -301,7 +322,7 @@ class ProjectRunner extends RunnerBase {
}
});

return compileProjectFiles(compilerResult.moduleKind,getInputFiles, getSourceFileText, writeFile);
return compileProjectFiles(compilerResult.moduleKind,getInputFiles, getSourceFileText, writeFile, readFile, fileExists);

function findOutpuDtsFile(fileName: string) {
return ts.forEach(compilerResult.outputFiles, outputFile => outputFile.emittedFileName === fileName ? outputFile : undefined);
Expand All @@ -315,6 +336,20 @@ class ProjectRunner extends RunnerBase {

function writeFile(fileName: string, data: string, writeByteOrderMark: boolean) {
}

function getFullDiskFileName(fileName: string): string {
return ts.isRootedDiskPath(fileName)
? fileName
: ts.normalizeSlashes(getCurrentDirectory()) + "/" + ts.normalizeSlashes(fileName);
}

function readFile(fileName: string) {
return ts.sys.readFile(getFullDiskFileName(fileName));
}

function fileExists(fileName: string) {
return ts.forEach(allInputFiles, inputFile => inputFile.emittedFileName === fileName ? true : false);
}
}

function getErrorsBaseline(compilerResult: CompileProjectFilesResult) {
Expand Down
11 changes: 9 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,9 @@ module ts {
log? (s: string): void;
trace? (s: string): void;
error? (s: string): void;

// Needed for mocking support
fileExists? (s: string): boolean;
}

//
Expand Down Expand Up @@ -1795,7 +1798,9 @@ module ts {
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getNewLine: () => (sys && sys.newLine) || "\r\n"
getNewLine: () => (sys && sys.newLine) || "\r\n",
readFile: sys.readFile,
fileExists: sys.fileExists
};

var program = createProgram([inputFileName], options, compilerHost);
Expand Down Expand Up @@ -2433,7 +2438,9 @@ module ts {
getNewLine: () => host.getNewLine ? host.getNewLine() : "\r\n",
getDefaultLibFileName: (options) => host.getDefaultLibFileName(options),
writeFile: (fileName, data, writeByteOrderMark) => { },
getCurrentDirectory: () => host.getCurrentDirectory()
readFile: (fileName) => sys.readFile(fileName),
fileExists: host.fileExists ? (fn) => host.fileExists(fn) : sys.fileExists,
getCurrentDirectory: () => host.getCurrentDirectory(),
});

// Release any files we have acquired in the old program but are
Expand Down
5 changes: 5 additions & 0 deletions src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module ts {
getDefaultLibFileName(options: string): string;
getNewLine?(): string;
getProjectVersion?(): string;
fileExists(fileName: string): boolean;
}

/** Public interface of the the of a config service shim instance.*/
Expand Down Expand Up @@ -260,6 +261,10 @@ module ts {
public error(s: string): void {
this.shimHost.error(s);
}

public fileExists(fn: string): boolean {
return this.shimHost.fileExists(fn);
}

public getProjectVersion(): string {
if (!this.shimHost.getProjectVersion) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
define(["require", "exports", "file-at-root", "somemodule/nested-file", "find-by-index", "find-by-package-main"], function (require, exports, file_at_root_1, nested_file_1, find_by_index_1, find_by_package_main_1) {
var fileAtRoot = new file_at_root_1.FileAtRoot();
fileAtRoot.fileAtRoot = '123';
var nestedFile = new nested_file_1.NestedFile();
nestedFile.nestedFile = '123';
var findByIndex = new find_by_index_1.FindByIndex();
findByIndex.findByIndex = '123';
var findByPackageMain = new find_by_package_main_1.FindByPackageMain();
findByPackageMain.findByPackageMain = '123';
});
20 changes: 20 additions & 0 deletions tests/baselines/reference/project/nodeModules/amd/nodeModules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"scenario": "nodeModules",
"projectRoot": "tests/cases/projects/nodeModules",
"inputFiles": [
"consumeNodeModules.ts"
],
"baselineCheck": true,
"runTest": true,
"resolvedInputFiles": [
"lib.d.ts",
"node_modules/file-at-root.d.ts",
"node_modules/somemodule/nested-file.d.ts",
"node_modules/find-by-index/index.d.ts",
"node_modules/find-by-package-main/dist/findByPackageMain.d.ts",
"consumeNodeModules.ts"
],
"emittedFiles": [
"consumeNodeModules.js"
]
}
Loading