Skip to content

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

Merged
merged 18 commits into from
Jul 3, 2025
Merged
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
25 changes: 25 additions & 0 deletions internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type processedFiles struct {
// List of present unsupported extensions
unsupportedExtensions []string
sourceFilesFoundSearchingNodeModules collections.Set[tspath.Path]
fileLoadDiagnostics *ast.DiagnosticsCollection
}

type jsxRuntimeImportSpecifier struct {
Expand Down Expand Up @@ -132,6 +133,7 @@ func processAllProgramFiles(
var unsupportedExtensions []string
var sourceFilesFoundSearchingNodeModules collections.Set[tspath.Path]
var libFileSet collections.Set[tspath.Path]
fileLoadDiagnostics := &ast.DiagnosticsCollection{}

loader.parseTasks.collect(&loader, loader.rootTasks, func(task *parseTask, _ []tspath.Path) {
if task.isRedirected {
Expand Down Expand Up @@ -159,6 +161,7 @@ func processAllProgramFiles(
resolvedModules[path] = task.resolutionsInFile
typeResolutionsInFile[path] = task.typeResolutionsInFile
sourceFileMetaDatas[path] = task.metadata

if task.jsxRuntimeImportSpecifier != nil {
if jsxRuntimeImportSpecifiers == nil {
jsxRuntimeImportSpecifiers = make(map[tspath.Path]*jsxRuntimeImportSpecifier, totalFileCount)
Expand All @@ -183,8 +186,28 @@ func processAllProgramFiles(

allFiles := append(libFiles, files...)

for _, resolutions := range resolvedModules {
for _, resolvedModule := range resolutions {
for _, diag := range resolvedModule.ResolutionDiagnostics {
fileLoadDiagnostics.Add(diag)
}
}
}
for _, typeResolutions := range typeResolutionsInFile {
for _, resolvedTypeRef := range typeResolutions {
for _, diag := range resolvedTypeRef.ResolutionDiagnostics {
fileLoadDiagnostics.Add(diag)
}
}
}

loader.pathForLibFileResolutions.Range(func(key tspath.Path, value module.ModeAwareCache[*module.ResolvedModule]) bool {
resolvedModules[key] = value
for _, resolvedModule := range value {
for _, diag := range resolvedModule.ResolutionDiagnostics {
fileLoadDiagnostics.Add(diag)
}
}
return true
})

Expand All @@ -201,6 +224,7 @@ func processAllProgramFiles(
unsupportedExtensions: unsupportedExtensions,
sourceFilesFoundSearchingNodeModules: sourceFilesFoundSearchingNodeModules,
libFiles: libFileSet,
fileLoadDiagnostics: fileLoadDiagnostics,
}
}

Expand Down Expand Up @@ -372,6 +396,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
resolutionMode := getModeForTypeReferenceDirectiveInFile(ref, file, meta, module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect))
resolved := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, redirect)
typeResolutionsInFile[module.ModeAwareCacheKey{Name: ref.FileName, Mode: resolutionMode}] = resolved

if resolved.IsResolved() {
t.addSubTask(resolvedRef{
fileName: resolved.ResolvedFileName,
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/parsetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type parseTask struct {
metadata ast.SourceFileMetaData
resolutionsInFile module.ModeAwareCache[*module.ResolvedModule]
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective]
resolutionDiagnostics []*ast.Diagnostic
importHelpersImportSpecifier *ast.Node
jsxRuntimeImportSpecifier *jsxRuntimeImportSpecifier
increaseDepth bool
Expand Down
5 changes: 5 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ func (p *Program) GetSuggestionDiagnostics(ctx context.Context, sourceFile *ast.
return p.getDiagnosticsHelper(ctx, sourceFile, true /*ensureBound*/, true /*ensureChecked*/, p.getSuggestionDiagnosticsForFile)
}

func (p *Program) GetProgramDiagnostics() []*ast.Diagnostic {
// !!!
return SortAndDeduplicateDiagnostics(p.fileLoadDiagnostics.GetDiagnostics())
}

func (p *Program) GetGlobalDiagnostics(ctx context.Context) []*ast.Diagnostic {
var globalDiagnostics []*ast.Diagnostic
checkers, done := p.checkerPool.GetAllCheckers(ctx)
Expand Down
1 change: 1 addition & 0 deletions internal/execute/tsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func emitFilesAndReportErrors(sys System, program *compiler.Program, reportDiagn
configFileParsingDiagnosticsLength := len(allDiagnostics)

allDiagnostics = append(allDiagnostics, program.GetSyntacticDiagnostics(ctx, nil)...)
allDiagnostics = append(allDiagnostics, program.GetProgramDiagnostics()...)

if len(allDiagnostics) == configFileParsingDiagnosticsLength {
// Options diagnostics include global diagnostics (even though we collect them separately),
Expand Down
104 changes: 101 additions & 3 deletions internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -54,7 +58,7 @@ type resolutionState struct {
resolvedPackageDirectory bool
failedLookupLocations []string
affectingLocations []string
diagnostics []ast.Diagnostic
diagnostics []*ast.Diagnostic
}

func newResolutionState(
Expand Down Expand Up @@ -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 != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to microsoft/TypeScript#54500, the --composite check will go away in 6.0, making the error below only possible for non-tsconfig builds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I do that now?

Copy link
Member Author

Choose a reason for hiding this comment

The 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") {
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions internal/module/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@ var skip = []string{
"moduleResolutionWithSymlinks_referenceTypes.ts",
"moduleResolutionWithSymlinks_withOutDir.ts",
"moduleResolutionWithSymlinks.ts",
"nodeAllowJsPackageSelfName(module=node16).ts",
"nodeAllowJsPackageSelfName(module=nodenext).ts",
"nodeAllowJsPackageSelfName2.ts",
"nodeModulesAllowJsConditionalPackageExports(module=node16).ts",
"nodeModulesAllowJsConditionalPackageExports(module=nodenext).ts",
"nodeModulesAllowJsPackageExports(module=node16).ts",
"nodeModulesAllowJsPackageExports(module=nodenext).ts",
"nodeModulesAllowJsPackageImports(module=node16).ts",
"nodeModulesAllowJsPackageImports(module=nodenext).ts",
"nodeModulesAllowJsPackagePatternExports(module=node16).ts",
"nodeModulesAllowJsPackagePatternExports(module=nodenext).ts",
"nodeModulesAllowJsPackagePatternExportsTrailers(module=node16).ts",
"nodeModulesAllowJsPackagePatternExportsTrailers(module=nodenext).ts",
"nodeModulesConditionalPackageExports(module=node16).ts",
"nodeModulesConditionalPackageExports(module=nodenext).ts",
"nodeModulesDeclarationEmitWithPackageExports(module=node16).ts",
"nodeModulesDeclarationEmitWithPackageExports(module=nodenext).ts",
"nodeModulesExportsBlocksTypesVersions(module=node16).ts",
Expand Down
2 changes: 1 addition & 1 deletion internal/module/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *PackageId) PackageName() string {
type LookupLocations struct {
FailedLookupLocations []string
AffectingLocations []string
ResolutionDiagnostics []ast.Diagnostic
ResolutionDiagnostics []*ast.Diagnostic
}

type ResolvedModule struct {
Expand Down
1 change: 1 addition & 0 deletions internal/testutil/harnessutil/harnessutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ func compileFilesWithHost(
ctx := context.Background()
program := createProgram(host, config)
var diagnostics []*ast.Diagnostic
diagnostics = append(diagnostics, program.GetProgramDiagnostics()...)
diagnostics = append(diagnostics, program.GetSyntacticDiagnostics(ctx, nil)...)
diagnostics = append(diagnostics, program.GetSemanticDiagnostics(ctx, nil)...)
diagnostics = append(diagnostics, program.GetGlobalDiagnostics(ctx)...)
Expand Down
30 changes: 30 additions & 0 deletions testdata/baselines/reference/compiler/subpathImportsJS.js
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 {};
13 changes: 13 additions & 0 deletions testdata/baselines/reference/compiler/subpathImportsJS.symbols
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))

14 changes: 14 additions & 0 deletions testdata/baselines/reference/compiler/subpathImportsJS.types
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.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import * as me from "#dep";
>me : Symbol(me, Decl(index.ts, 0, 6))

me.thing();
>me.thing : Symbol(thing, Decl(index.ts, 2, 11))
>me : Symbol(me, Decl(index.ts, 0, 6))
>thing : Symbol(thing, Decl(index.ts, 2, 11))

export function thing(): void {}
>thing : Symbol(thing, Decl(index.ts, 2, 11))
Expand Down
Loading