From 1a9abba43f28aea29e9374b380f5c5a695b97f3f Mon Sep 17 00:00:00 2001 From: cplieger <917744+cplieger@users.noreply.github.com> Date: Mon, 5 Oct 2026 09:56:56 +0200 Subject: [PATCH 1/2] Restart a file's imports when a later arrival lowers its node_modules depth --- tsc/internal/compiler/filesparser.go | 5 +-- tsc/internal/compiler/filesparser_test.go | 42 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tsc/internal/compiler/filesparser_test.go diff --git a/tsc/internal/compiler/filesparser.go b/tsc/internal/compiler/filesparser.go index 03a21cbf71961..5947c5fbb3a40 100644 --- a/tsc/internal/compiler/filesparser.go +++ b/tsc/internal/compiler/filesparser.go @@ -302,7 +302,8 @@ func (w *filesParser) start(loader *fileLoader, tasks []*parseTask, depth int) { } currentDepth := core.IfElse(task.increaseDepth, depth+1, depth) - if currentDepth < data.lowestDepth { + lowered := currentDepth < data.lowestDepth + if lowered { // If we're seeing this task at a lower depth than before, // reprocess its subtasks to ensure they are loaded. data.lowestDepth = currentDepth @@ -324,7 +325,7 @@ func (w *filesParser) start(loader *fileLoader, tasks []*parseTask, depth int) { data.startedSubTasks = true } } - if !taskByFileName.startedSubTasks && loadSubTasks { + if loadSubTasks && (lowered || !taskByFileName.startedSubTasks) { taskByFileName.startedSubTasks = true w.start(loader, taskByFileName.subTasks, data.lowestDepth) } diff --git a/tsc/internal/compiler/filesparser_test.go b/tsc/internal/compiler/filesparser_test.go new file mode 100644 index 0000000000000..7fc1f8ee3799f --- /dev/null +++ b/tsc/internal/compiler/filesparser_test.go @@ -0,0 +1,42 @@ +package compiler + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/core" + "github.com/microsoft/TypeScript/tsc/internal/tsoptions" + "github.com/microsoft/TypeScript/tsc/internal/tspath" + "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" + "gotest.tools/v3/assert" +) + +func TestExternalLibraryDepthLoweredByLaterArrival(t *testing.T) { + t.Parallel() + for name, roots := range map[string][]tspath.RootedFilePath{ + "rootsAThenB": {"/src/a.ts", "/src/b.ts"}, + "rootsBThenA": {"/src/b.ts", "/src/a.ts"}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + fs := vfstest.FromMap(map[string]any{ + "/src/a.ts": `import { util } from "./lib/index"; export const a = util;`, + "/src/b.ts": `import { util } from "lib"; export const b = util;`, + "/src/node_modules/lib": vfstest.Symlink("/src/lib"), + "/src/lib/package.json": `{"name":"lib","types":"index.ts"}`, + "/src/lib/index.ts": `export { util } from "./util";`, + "/src/lib/util.ts": `export const util = 1;`, + }, tspath.CaseSensitive) + opts := &core.CompilerOptions{NoLib: core.TSTrue, Types: []string{}} + program := NewProgram(ProgramOptions{ + Config: tsoptions.NewParsedCommandLine(opts, roots, nil, "/src", fs.CaseSensitivity()), + Host: NewCompilerHost(fs, "/", nil, nil, nil), + SingleThreaded: core.TSTrue, + }) + for _, fileName := range []tspath.RootedFilePath{"/src/lib/index.ts", "/src/lib/util.ts"} { + file := program.GetSourceFile(fileName) + assert.Assert(t, file != nil, "%s is not in the program", fileName) + assert.Assert(t, !program.IsSourceFileFromExternalLibrary(file), "%s is reachable from a root file without node_modules", fileName) + } + }) + } +} From 3e62a5fdcd4b58a3e4b7e44e9db7a20d8f51568d Mon Sep 17 00:00:00 2001 From: cplieger <917744+cplieger@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:59:13 +0200 Subject: [PATCH 2/2] Replace the unit test with a compiler test --- tsc/internal/compiler/filesparser_test.go | 42 ------------------ ...geReachedRelativelyIsNotExternalLibrary.js | 43 +++++++++++++++++++ ...geReachedRelativelyIsNotExternalLibrary.ts | 27 ++++++++++++ 3 files changed, 70 insertions(+), 42 deletions(-) delete mode 100644 tsc/internal/compiler/filesparser_test.go create mode 100644 tsc/testdata/baselines/reference/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.js create mode 100644 tsc/testdata/tests/cases/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.ts diff --git a/tsc/internal/compiler/filesparser_test.go b/tsc/internal/compiler/filesparser_test.go deleted file mode 100644 index 7fc1f8ee3799f..0000000000000 --- a/tsc/internal/compiler/filesparser_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package compiler - -import ( - "testing" - - "github.com/microsoft/TypeScript/tsc/internal/core" - "github.com/microsoft/TypeScript/tsc/internal/tsoptions" - "github.com/microsoft/TypeScript/tsc/internal/tspath" - "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" - "gotest.tools/v3/assert" -) - -func TestExternalLibraryDepthLoweredByLaterArrival(t *testing.T) { - t.Parallel() - for name, roots := range map[string][]tspath.RootedFilePath{ - "rootsAThenB": {"/src/a.ts", "/src/b.ts"}, - "rootsBThenA": {"/src/b.ts", "/src/a.ts"}, - } { - t.Run(name, func(t *testing.T) { - t.Parallel() - fs := vfstest.FromMap(map[string]any{ - "/src/a.ts": `import { util } from "./lib/index"; export const a = util;`, - "/src/b.ts": `import { util } from "lib"; export const b = util;`, - "/src/node_modules/lib": vfstest.Symlink("/src/lib"), - "/src/lib/package.json": `{"name":"lib","types":"index.ts"}`, - "/src/lib/index.ts": `export { util } from "./util";`, - "/src/lib/util.ts": `export const util = 1;`, - }, tspath.CaseSensitive) - opts := &core.CompilerOptions{NoLib: core.TSTrue, Types: []string{}} - program := NewProgram(ProgramOptions{ - Config: tsoptions.NewParsedCommandLine(opts, roots, nil, "/src", fs.CaseSensitivity()), - Host: NewCompilerHost(fs, "/", nil, nil, nil), - SingleThreaded: core.TSTrue, - }) - for _, fileName := range []tspath.RootedFilePath{"/src/lib/index.ts", "/src/lib/util.ts"} { - file := program.GetSourceFile(fileName) - assert.Assert(t, file != nil, "%s is not in the program", fileName) - assert.Assert(t, !program.IsSourceFileFromExternalLibrary(file), "%s is reachable from a root file without node_modules", fileName) - } - }) - } -} diff --git a/tsc/testdata/baselines/reference/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.js b/tsc/testdata/baselines/reference/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.js new file mode 100644 index 0000000000000..0129b214e47bc --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.js @@ -0,0 +1,43 @@ +//// [tests/cases/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.ts] //// + +//// [package.json] +{ "name": "lib", "types": "index.ts" } + +//// [index.ts] +export { util } from "./util"; + +//// [util.ts] +export const util = 1; + +//// [a.ts] +import { util } from "./lib/index"; +export const a = util; + +//// [b.ts] +import { util } from "lib"; +export const b = util; + + +//// [util.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.util = void 0; +exports.util = 1; +//// [index.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.util = void 0; +var util_1 = require("./util"); +Object.defineProperty(exports, "util", { enumerable: true, get: function () { return util_1.util; } }); +//// [a.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.a = void 0; +const index_1 = require("./lib/index"); +exports.a = index_1.util; +//// [b.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.b = void 0; +const lib_1 = require("lib"); +exports.b = lib_1.util; diff --git a/tsc/testdata/tests/cases/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.ts b/tsc/testdata/tests/cases/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.ts new file mode 100644 index 0000000000000..eaaea09be8cc3 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/symlinkedPackageReachedRelativelyIsNotExternalLibrary.ts @@ -0,0 +1,27 @@ +// @noTypesAndSymbols: true + +// @filename: /src/tsconfig.json +{ + "compilerOptions": { "outDir": "out", "module": "commonjs", "target": "es2015" }, + "files": ["a.ts", "b.ts"] +} + +// @filename: /src/a.ts +import { util } from "./lib/index"; +export const a = util; + +// @filename: /src/b.ts +import { util } from "lib"; +export const b = util; + +// @filename: /src/lib/package.json +// @symlink: /src/node_modules/lib/package.json +{ "name": "lib", "types": "index.ts" } + +// @filename: /src/lib/index.ts +// @symlink: /src/node_modules/lib/index.ts +export { util } from "./util"; + +// @filename: /src/lib/util.ts +// @symlink: /src/node_modules/lib/util.ts +export const util = 1;