MCPcopy Index your code
hub / github.com/nodejs/node / tryLoadInputFileForPath

Function tryLoadInputFileForPath

test/fixtures/snapshot/typescript.js:44171–44278  ·  view source on GitHub ↗
(finalPath, entry, packagePath, isImports)

Source from the content-addressed store, hash-verified

44169 state.host.useCaseSensitiveFileNames();
44170 }
44171 function tryLoadInputFileForPath(finalPath, entry, packagePath, isImports) {
44172 var _a, _b, _c, _d;
44173 // Replace any references to outputs for files in the program with the input files to support package self-names used with outDir
44174 // PROBLEM: We don't know how to calculate the output paths yet, because the "common source directory" we use as the base of the file structure
44175 // we reproduce into the output directory is based on the set of input files, which we're still in the process of traversing and resolving!
44176 // _Given that_, we have to guess what the base of the output directory is (obviously the user wrote the export map, so has some idea what it is!).
44177 // We are going to probe _so many_ possible paths. We limit where we'll do this to try to reduce the possibilities of false positive lookups.
44178 if ((extensions === Extensions.TypeScript || extensions === Extensions.JavaScript || extensions === Extensions.Json)
44179 && (state.compilerOptions.declarationDir || state.compilerOptions.outDir)
44180 && finalPath.indexOf("/node_modules/") === -1
44181 && (state.compilerOptions.configFile ? ts.startsWith(toAbsolutePath(state.compilerOptions.configFile.fileName), scope.packageDirectory) : true)) {
44182 // So that all means we'll only try these guesses for files outside `node_modules` in a directory where the `package.json` and `tsconfig.json` are siblings.
44183 // Even with all that, we still don't know if the root of the output file structure will be (relative to the package file)
44184 // `.`, `./src` or any other deeper directory structure. (If project references are used, it's definitely `.` by fiat, so that should be pretty common.)
44185 var getCanonicalFileName = ts.hostGetCanonicalFileName({ useCaseSensitiveFileNames: useCaseSensitiveFileNames });
44186 var commonSourceDirGuesses = [];
44187 // A `rootDir` compiler option strongly indicates the root location
44188 // A `composite` project is using project references and has it's common src dir set to `.`, so it shouldn't need to check any other locations
44189 if (state.compilerOptions.rootDir || (state.compilerOptions.composite && state.compilerOptions.configFilePath)) {
44190 var commonDir = toAbsolutePath(ts.getCommonSourceDirectory(state.compilerOptions, function () { return []; }, ((_b = (_a = state.host).getCurrentDirectory) === null || _b === void 0 ? void 0 : _b.call(_a)) || "", getCanonicalFileName));
44191 commonSourceDirGuesses.push(commonDir);
44192 }
44193 else if (state.requestContainingDirectory) {
44194 // However without either of those set we're in the dark. Let's say you have
44195 //
44196 // ./tools/index.ts
44197 // ./src/index.ts
44198 // ./dist/index.js
44199 // ./package.json <-- references ./dist/index.js
44200 // ./tsconfig.json <-- loads ./src/index.ts
44201 //
44202 // How do we know `./src` is the common src dir, and not `./tools`, given only the `./dist` out dir and `./dist/index.js` filename?
44203 // Answer: We... don't. We know we're looking for an `index.ts` input file, but we have _no clue_ which subfolder it's supposed to be loaded from
44204 // without more context.
44205 // But we do have more context! Just a tiny bit more! We're resolving an import _for some other input file_! And that input file, too
44206 // must be inside the common source directory! So we propagate that tidbit of info all the way to here via state.requestContainingDirectory
44207 var requestingFile_1 = toAbsolutePath(ts.combinePaths(state.requestContainingDirectory, "index.ts"));
44208 // And we can try every folder above the common folder for the request folder and the config/package base directory
44209 // This technically can be wrong - we may load ./src/index.ts when ./src/sub/index.ts was right because we don't
44210 // know if only `./src/sub` files were loaded by the program; but this has the best chance to be right of just about anything
44211 // else we have. And, given that we're about to load `./src/index.ts` because we choose it as likely correct, there will then
44212 // be a file outside of `./src/sub` in the program (the file we resolved to), making us de-facto right. So this fallback lookup
44213 // logic may influence what files are pulled in by self-names, which in turn influences the output path shape, but it's all
44214 // internally consistent so the paths should be stable so long as we prefer the "most general" (meaning: top-most-level directory) possible results first.
44215 var commonDir = toAbsolutePath(ts.getCommonSourceDirectory(state.compilerOptions, function () { return [requestingFile_1, toAbsolutePath(packagePath)]; }, ((_d = (_c = state.host).getCurrentDirectory) === null || _d === void 0 ? void 0 : _d.call(_c)) || "", getCanonicalFileName));
44216 commonSourceDirGuesses.push(commonDir);
44217 var fragment = ts.ensureTrailingDirectorySeparator(commonDir);
44218 while (fragment && fragment.length > 1) {
44219 var parts = ts.getPathComponents(fragment);
44220 parts.pop(); // remove a directory
44221 var commonDir_1 = ts.getPathFromPathComponents(parts);
44222 commonSourceDirGuesses.unshift(commonDir_1);
44223 fragment = ts.ensureTrailingDirectorySeparator(commonDir_1);
44224 }
44225 }
44226 if (commonSourceDirGuesses.length > 1) {
44227 state.reportDiagnostic(ts.createCompilerDiagnostic(isImports
44228 ? ts.Diagnostics.The_project_root_is_ambiguous_but_is_required_to_resolve_import_map_entry_0_in_file_1_Supply_the_rootDir_compiler_option_to_disambiguate

Callers 1

Calls 12

toAbsolutePathFunction · 0.85
toSearchResultFunction · 0.85
withPackageIdFunction · 0.85
loadJSOrExactTSFileNameFunction · 0.85
popMethod · 0.80
unshiftMethod · 0.80
sliceMethod · 0.65
indexOfMethod · 0.45
callMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…