-
Notifications
You must be signed in to change notification settings - Fork 658
Implement tryLoadInputFileForPath, file loader diags #1302
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
Changes from all commits
6108eb8
11eac90
76104bb
9b5ddfe
916c1ac
0cbc00e
fb51fbd
9bb8923
0247b42
8be0f5a
3c51428
3f76e96
cef3523
2808146
051dcbd
b937bb6
7f94fbc
2b5d361
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,10 @@ func continueSearching() *resolved { | |
return nil | ||
} | ||
|
||
func unresolved() *resolved { | ||
return &resolved{} | ||
} | ||
|
||
type resolutionKindSpecificLoader = func(extensions extensions, candidate string, onlyRecordFailures bool) *resolved | ||
|
||
type resolutionState struct { | ||
|
@@ -54,7 +58,7 @@ type resolutionState struct { | |
resolvedPackageDirectory bool | ||
failedLookupLocations []string | ||
affectingLocations []string | ||
diagnostics []ast.Diagnostic | ||
diagnostics []*ast.Diagnostic | ||
} | ||
|
||
func newResolutionState( | ||
|
@@ -748,10 +752,104 @@ func (r *resolutionState) loadModuleFromTargetExportOrImport(extensions extensio | |
} | ||
|
||
func (r *resolutionState) tryLoadInputFileForPath(finalPath string, entry string, packagePath string, isImports bool) *resolved { | ||
// !!! | ||
// Replace any references to outputs for files in the program with the input files to support package self-names used with outDir | ||
if !r.isConfigLookup && | ||
(r.compilerOptions.DeclarationDir != "" || r.compilerOptions.OutDir != "") && | ||
!strings.Contains(finalPath, "/node_modules/") && | ||
(r.compilerOptions.ConfigFilePath == "" || tspath.ContainsPath( | ||
tspath.GetDirectoryPath(packagePath), | ||
r.compilerOptions.ConfigFilePath, | ||
tspath.ComparePathsOptions{ | ||
UseCaseSensitiveFileNames: r.resolver.host.FS().UseCaseSensitiveFileNames(), | ||
CurrentDirectory: r.resolver.host.GetCurrentDirectory(), | ||
}, | ||
)) { | ||
|
||
// Note: this differs from Strada's tryLoadInputFileForPath in that it | ||
// does not attempt to perform "guesses", instead requring a clear root indicator. | ||
|
||
var rootDir string | ||
if r.compilerOptions.RootDir != "" { | ||
// A `rootDir` compiler option strongly indicates the root location | ||
rootDir = r.compilerOptions.RootDir | ||
} else if r.compilerOptions.Composite.IsTrue() && r.compilerOptions.ConfigFilePath != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to microsoft/TypeScript#54500, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I do that now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just going to let this merge and address it separately if needed, given this will unbreak people |
||
// 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 | ||
rootDir = r.compilerOptions.ConfigFilePath | ||
} else { | ||
diagnostic := ast.NewDiagnostic( | ||
nil, | ||
core.TextRange{}, | ||
core.IfElse(isImports, | ||
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, | ||
diagnostics.The_project_root_is_ambiguous_but_is_required_to_resolve_export_map_entry_0_in_file_1_Supply_the_rootDir_compiler_option_to_disambiguate, | ||
), | ||
core.IfElse(entry == "", ".", entry), // replace empty string with `.` - the reverse of the operation done when entries are built - so main entrypoint errors don't look weird | ||
packagePath, | ||
) | ||
r.diagnostics = append(r.diagnostics, diagnostic) | ||
return unresolved() | ||
} | ||
|
||
candidateDirectories := r.getOutputDirectoriesForBaseDirectory(rootDir) | ||
for _, candidateDir := range candidateDirectories { | ||
if tspath.ContainsPath(candidateDir, finalPath, tspath.ComparePathsOptions{ | ||
UseCaseSensitiveFileNames: r.resolver.host.FS().UseCaseSensitiveFileNames(), | ||
CurrentDirectory: r.resolver.host.GetCurrentDirectory(), | ||
}) { | ||
// The matched export is looking up something in either the out declaration or js dir, now map the written path back into the source dir and source extension | ||
pathFragment := finalPath[len(candidateDir)+1:] // +1 to also remove directory separator | ||
possibleInputBase := tspath.CombinePaths(rootDir, pathFragment) | ||
jsAndDtsExtensions := []string{tspath.ExtensionMjs, tspath.ExtensionCjs, tspath.ExtensionJs, tspath.ExtensionJson, tspath.ExtensionDmts, tspath.ExtensionDcts, tspath.ExtensionDts} | ||
for _, ext := range jsAndDtsExtensions { | ||
if tspath.FileExtensionIs(possibleInputBase, ext) { | ||
inputExts := r.getPossibleOriginalInputExtensionForExtension(possibleInputBase) | ||
for _, possibleExt := range inputExts { | ||
if !extensionIsOk(r.extensions, possibleExt) { | ||
continue | ||
} | ||
possibleInputWithInputExtension := tspath.ChangeExtension(possibleInputBase, possibleExt) | ||
if r.resolver.host.FS().FileExists(possibleInputWithInputExtension) { | ||
resolved := r.loadFileNameFromPackageJSONField(r.extensions, possibleInputWithInputExtension, "", false) | ||
if !resolved.shouldContinueSearching() { | ||
return resolved | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return continueSearching() | ||
} | ||
|
||
func (r *resolutionState) getOutputDirectoriesForBaseDirectory(commonSourceDirGuess string) []string { | ||
// Config file output paths are processed to be relative to the host's current directory, while | ||
// otherwise the paths are resolved relative to the common source dir the compiler puts together | ||
currentDir := core.IfElse(r.compilerOptions.ConfigFilePath != "", r.resolver.host.GetCurrentDirectory(), commonSourceDirGuess) | ||
var candidateDirectories []string | ||
if r.compilerOptions.DeclarationDir != "" { | ||
candidateDirectories = append(candidateDirectories, tspath.GetNormalizedAbsolutePath(tspath.CombinePaths(currentDir, r.compilerOptions.DeclarationDir), r.resolver.host.GetCurrentDirectory())) | ||
} | ||
if r.compilerOptions.OutDir != "" && r.compilerOptions.OutDir != r.compilerOptions.DeclarationDir { | ||
candidateDirectories = append(candidateDirectories, tspath.GetNormalizedAbsolutePath(tspath.CombinePaths(currentDir, r.compilerOptions.OutDir), r.resolver.host.GetCurrentDirectory())) | ||
} | ||
return candidateDirectories | ||
} | ||
|
||
func (r *resolutionState) getPossibleOriginalInputExtensionForExtension(path string) []string { | ||
if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDmts, tspath.ExtensionMjs, tspath.ExtensionMts}) { | ||
return []string{tspath.ExtensionMts, tspath.ExtensionMjs} | ||
} | ||
if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDcts, tspath.ExtensionCjs, tspath.ExtensionCts}) { | ||
return []string{tspath.ExtensionCts, tspath.ExtensionCjs} | ||
} | ||
if tspath.FileExtensionIs(path, ".d.json.ts") { | ||
return []string{tspath.ExtensionJson} | ||
} | ||
return []string{tspath.ExtensionTsx, tspath.ExtensionTs, tspath.ExtensionJsx, tspath.ExtensionJs} | ||
} | ||
|
||
func (r *resolutionState) loadModuleFromNearestNodeModulesDirectory(typesScopeOnly bool) *resolved { | ||
mode := core.ResolutionModeCommonJS | ||
if r.esmMode || r.conditionMatches("import") { | ||
|
@@ -1377,7 +1475,7 @@ func (r *resolutionState) loadFileNameFromPackageJSONField(extensions extensions | |
return &resolved{ | ||
path: path, | ||
extension: extension, | ||
resolvedUsingTsExtension: !strings.HasSuffix(packageJSONValue, extension), | ||
resolvedUsingTsExtension: packageJSONValue != "" && !strings.HasSuffix(packageJSONValue, extension), | ||
} | ||
} | ||
return continueSearching() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//// [tests/cases/compiler/subpathImportsJS.ts] //// | ||
|
||
//// [package.json] | ||
{ | ||
"name": "pkg", | ||
"type": "module", | ||
"imports": { | ||
"#subpath": "./dist/subpath.js" | ||
} | ||
} | ||
|
||
//// [subpath.ts] | ||
export const foo = "foo"; | ||
|
||
//// [index.ts] | ||
import { foo } from "#subpath"; | ||
foo; | ||
|
||
|
||
//// [subpath.js] | ||
export const foo = "foo"; | ||
//// [index.js] | ||
import { foo } from "#subpath"; | ||
foo; | ||
|
||
|
||
//// [subpath.d.ts] | ||
export declare const foo = "foo"; | ||
//// [index.d.ts] | ||
export {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//// [tests/cases/compiler/subpathImportsJS.ts] //// | ||
|
||
=== /src/subpath.ts === | ||
export const foo = "foo"; | ||
>foo : Symbol(foo, Decl(subpath.ts, 0, 12)) | ||
|
||
=== /src/index.ts === | ||
import { foo } from "#subpath"; | ||
>foo : Symbol(foo, Decl(index.ts, 0, 8)) | ||
|
||
foo; | ||
>foo : Symbol(foo, Decl(index.ts, 0, 8)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//// [tests/cases/compiler/subpathImportsJS.ts] //// | ||
|
||
=== /src/subpath.ts === | ||
export const foo = "foo"; | ||
>foo : "foo" | ||
>"foo" : "foo" | ||
|
||
=== /src/index.ts === | ||
import { foo } from "#subpath"; | ||
>foo : "foo" | ||
|
||
foo; | ||
>foo : "foo" | ||
|
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.