Skip to content

Commit 9b3df7a

Browse files
authored
fix: move @types/node checking to loader (#18)
1 parent 158136c commit 9b3df7a

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

DENO_LOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Deno Log
22

3+
### 2025.12.19 / @dsherret
4+
5+
- Improved automatic lib.node.d.ts injection by deferring `/// <reference lib="node" />` from being injected.
6+
- This now waits until after everything has loaded in order to decide whether to inject.
7+
38
### 2025.12.18 / @dsherret
49

510
- Added a `resolveJsxImportSource` method to the resolver for resolving the jsxImportSource based on the referrer.

internal/compiler/fileloader.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type fileLoader struct {
4949

5050
pathForLibFileCache collections.SyncMap[string, *LibFile]
5151
pathForLibFileResolutions collections.SyncMap[tspath.Path, *libResolution]
52+
53+
// deno: flags for lib.node.d.ts handling
54+
shouldLoadNodeTypes atomic.Bool
55+
foundNodeTypes atomic.Bool
5256
}
5357

5458
type processedFiles struct {
@@ -108,10 +112,6 @@ func processAllProgramFiles(
108112
loader.addRootTask(rootFile, nil, &FileIncludeReason{kind: fileIncludeKindRootFile, data: index})
109113
}
110114

111-
// deno: cause the sub tasks to be loaded which will tell us if there's a @types/node package
112-
loader.filesParser.parse(&loader, loader.rootTasks)
113-
rootTasksBeforeLibs := len(loader.rootTasks)
114-
115115
if len(rootFiles) > 0 && compilerOptions.NoLib.IsFalseOrUnknown() {
116116
if compilerOptions.Lib == nil {
117117
name := tsoptions.GetDefaultLibFileName(compilerOptions)
@@ -122,10 +122,6 @@ func processAllProgramFiles(
122122
for index, lib := range compilerOptions.Lib {
123123
if name, ok := tsoptions.GetLibFileName(lib); ok {
124124
libFile := loader.pathForLibFile(name)
125-
// deno: we skip loading the lib.node.d.ts file if the @types/node package has been loaded
126-
if libFile.Name == "lib.node.d.ts" && loader.hasTypesNodePackage() {
127-
continue
128-
}
129125
loader.addRootTask(libFile.path, libFile, &FileIncludeReason{kind: fileIncludeKindLibFile, data: index})
130126
}
131127
// !!! error on unknown name
@@ -137,9 +133,26 @@ func processAllProgramFiles(
137133
loader.addAutomaticTypeDirectiveTasks()
138134
}
139135

140-
// deno: now load the lib and type directive tasks
141-
loader.filesParser.wg = core.NewWorkGroup(singleThreaded)
142-
loader.filesParser.parse(&loader, loader.rootTasks[rootTasksBeforeLibs:])
136+
loader.filesParser.parse(&loader, loader.rootTasks)
137+
138+
// deno: now load the built-in node types if they were previously attempted
139+
// to be loaded and there's no @types/node package found in the loader
140+
if loader.foundNodeTypes.Load() && !loader.hasTypesNodePackage() {
141+
loader.shouldLoadNodeTypes.Store(true)
142+
libFile := loader.pathForLibFile("lib.node.d.ts")
143+
// Remove the existing task data entirely so it will be reprocessed fresh
144+
// This includes all subtasks that might have been skipped
145+
loader.filesParser.taskDataByPath.Delete(loader.toPath(libFile.path))
146+
libIndex := 0
147+
if compilerOptions.Lib != nil {
148+
libIndex = len(compilerOptions.Lib)
149+
}
150+
loader.addRootTask(libFile.path, libFile, &FileIncludeReason{kind: fileIncludeKindLibFile, data: libIndex})
151+
152+
// parse and load only the newly added lib.node.d.ts task
153+
loader.filesParser.wg = core.NewWorkGroup(singleThreaded)
154+
loader.filesParser.parse(&loader, loader.rootTasks[len(loader.rootTasks)-1:])
155+
}
143156

144157
// Clear out loader and host to ensure its not used post program creation
145158
loader.projectReferenceFileMapper.loader = nil

internal/compiler/filesparser.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func (t *parseTask) Path() tspath.Path {
5252
}
5353

5454
func (t *parseTask) load(loader *fileLoader) {
55+
// deno: we skip loading the lib.node.d.ts file if the @types/node package has been loaded
56+
// This handles cases where lib.node.d.ts is referenced via /// <reference lib="node" />
57+
// Check this BEFORE setting loaded=true so the file isn't parsed at all
58+
if t.normalizedFilePath == "asset:///lib.node.d.ts" && !loader.shouldLoadNodeTypes.Load() {
59+
loader.foundNodeTypes.Store(true)
60+
return
61+
}
62+
5563
t.loaded = true
5664
if t.isForAutomaticTypeDirective {
5765
t.loadAutomaticTypeDirectives(loader)

0 commit comments

Comments
 (0)