Skip to content

Store tasks by path instead of file name #1535

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
40 changes: 33 additions & 7 deletions internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,14 @@ func (p *fileLoader) addRootTasks(files []string, isLib bool) {
for _, fileName := range files {
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: absPath, isLib: isLib, root: true})
p.rootTasks = append(
p.rootTasks,
&parseTask{
normalizedFilePath: absPath,
isLib: isLib,
root: true,
path: p.toPath(absPath),
})
}
}
}
Expand All @@ -253,7 +260,14 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
containingDirectory = p.opts.Host.GetCurrentDirectory()
}
containingFileName := tspath.CombinePaths(containingDirectory, module.InferredTypesContainingFile)
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: containingFileName, isLib: false, isForAutomaticTypeDirective: true})
p.rootTasks = append(
p.rootTasks,
&parseTask{
normalizedFilePath: containingFileName,
isLib: false,
isForAutomaticTypeDirective: true,
path: p.toPath(containingFileName),
})
}

func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
Expand Down Expand Up @@ -290,7 +304,7 @@ func (p *fileLoader) addProjectReferenceTasks() {
return
}

rootTasks := createProjectReferenceParseTasks(projectReferences)
rootTasks := createProjectReferenceParseTasks(projectReferences, p)
p.projectReferenceParseTasks.runAndWait(p, rootTasks)
p.projectReferenceFileMapper.init(p, rootTasks)

Expand All @@ -305,12 +319,24 @@ func (p *fileLoader) addProjectReferenceTasks() {
}
if p.opts.canUseProjectReferenceSource() {
for _, fileName := range resolved.FileNames() {
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: fileName, isLib: false})
p.rootTasks = append(
p.rootTasks,
&parseTask{
normalizedFilePath: fileName,
isLib: false,
path: p.toPath(fileName),
})
}
} else {
for outputDts := range resolved.GetOutputDeclarationFileNames() {
if outputDts != "" {
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: outputDts, isLib: false})
p.rootTasks = append(
p.rootTasks,
&parseTask{
normalizedFilePath: outputDts,
isLib: false,
path: p.toPath(outputDts),
})
}
}
}
Expand Down Expand Up @@ -406,7 +432,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
increaseDepth: resolved.IsExternalLibraryImport,
elideOnDepth: false,
isFromExternalLibrary: resolved.IsExternalLibraryImport,
}, false)
}, false, p)
}
}

Expand Down Expand Up @@ -497,7 +523,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
increaseDepth: resolvedModule.IsExternalLibraryImport,
elideOnDepth: isJsFileFromNodeModules,
isFromExternalLibrary: resolvedModule.IsExternalLibraryImport,
}, false)
}, false, p)
}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/compiler/fileloadertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type fileLoaderWorkerTask[T any] interface {
comparable
FileName() string
Path() tspath.Path
isLoaded() bool
load(loader *fileLoader)
getSubTasks() []T
Expand All @@ -24,7 +24,7 @@ type fileLoaderWorkerTask[T any] interface {

type fileLoaderWorker[K fileLoaderWorkerTask[K]] struct {
wg core.WorkGroup
tasksByFileName collections.SyncMap[string, *queuedTask[K]]
tasksByFilePath collections.SyncMap[tspath.Path, *queuedTask[K]]
maxDepth int
}

Expand All @@ -44,7 +44,7 @@ func (w *fileLoaderWorker[K]) start(loader *fileLoader, tasks []K, depth int, is
for i, task := range tasks {
taskIsFromExternalLibrary := isFromExternalLibrary || task.isFromExternalLibrary()
newTask := &queuedTask[K]{task: task, lowestDepth: math.MaxInt}
loadedTask, loaded := w.tasksByFileName.LoadOrStore(task.FileName(), newTask)
loadedTask, loaded := w.tasksByFilePath.LoadOrStore(task.Path(), newTask)
task = loadedTask.task
if loaded {
tasks[i] = task
Expand Down Expand Up @@ -93,7 +93,7 @@ func (w *fileLoaderWorker[K]) start(loader *fileLoader, tasks []K, depth int, is

func (w *fileLoaderWorker[K]) collect(loader *fileLoader, tasks []K, iterate func(K, []tspath.Path)) []tspath.Path {
// Mark all tasks we saw as external after the fact.
w.tasksByFileName.Range(func(key string, value *queuedTask[K]) bool {
w.tasksByFilePath.Range(func(_ tspath.Path, value *queuedTask[K]) bool {
if value.fromExternalLibrary {
value.task.markFromExternalLibrary()
}
Expand All @@ -115,7 +115,7 @@ func (w *fileLoaderWorker[K]) collectWorker(loader *fileLoader, tasks []K, itera
subResults = w.collectWorker(loader, subTasks, iterate, seen)
}
iterate(task, subResults)
results = append(results, loader.toPath(task.FileName()))
results = append(results, task.Path())
}
return results
}
21 changes: 16 additions & 5 deletions internal/compiler/parsetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (t *parseTask) FileName() string {
return t.normalizedFilePath
}

func (t *parseTask) FilePath() tspath.Path {
return t.path
}

func (t *parseTask) Path() tspath.Path {
return t.path
}
Expand Down Expand Up @@ -70,7 +74,7 @@ func (t *parseTask) load(loader *fileLoader) {

for _, ref := range file.ReferencedFiles {
resolvedPath := loader.resolveTripleslashPathReference(ref.FileName, file.FileName())
t.addSubTask(resolvedPath, false)
t.addSubTask(resolvedPath, false, loader)
}

compilerOptions := loader.opts.Config.CompilerOptions()
Expand All @@ -79,7 +83,7 @@ func (t *parseTask) load(loader *fileLoader) {
if compilerOptions.NoLib != core.TSTrue {
for _, lib := range file.LibReferenceDirectives {
if name, ok := tsoptions.GetLibFileName(lib.FileName); ok {
t.addSubTask(resolvedRef{fileName: loader.pathForLibFile(name)}, true)
t.addSubTask(resolvedRef{fileName: loader.pathForLibFile(name)}, true, loader)
}
}
}
Expand All @@ -90,14 +94,20 @@ func (t *parseTask) load(loader *fileLoader) {
func (t *parseTask) redirect(loader *fileLoader, fileName string) {
t.isRedirected = true
// increaseDepth and elideOnDepth are not copied to redirects, otherwise their depth would be double counted.
t.subTasks = []*parseTask{{normalizedFilePath: tspath.NormalizePath(fileName), isLib: t.isLib, fromExternalLibrary: t.fromExternalLibrary}}
normalizedFilePath := tspath.NormalizePath(fileName)
t.subTasks = []*parseTask{{
normalizedFilePath: normalizedFilePath,
isLib: t.isLib,
fromExternalLibrary: t.fromExternalLibrary,
path: loader.toPath(normalizedFilePath),
}}
}

func (t *parseTask) loadAutomaticTypeDirectives(loader *fileLoader) {
toParseTypeRefs, typeResolutionsInFile := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
t.typeResolutionsInFile = typeResolutionsInFile
for _, typeResolution := range toParseTypeRefs {
t.addSubTask(typeResolution, false)
t.addSubTask(typeResolution, false, loader)
}
}

Expand All @@ -108,14 +118,15 @@ type resolvedRef struct {
isFromExternalLibrary bool
}

func (t *parseTask) addSubTask(ref resolvedRef, isLib bool) {
func (t *parseTask) addSubTask(ref resolvedRef, isLib bool, loader *fileLoader) {
normalizedFilePath := tspath.NormalizePath(ref.fileName)
subTask := &parseTask{
normalizedFilePath: normalizedFilePath,
isLib: isLib,
increaseDepth: ref.increaseDepth,
elideOnDepth: ref.elideOnDepth,
fromExternalLibrary: ref.isFromExternalLibrary,
path: loader.toPath(normalizedFilePath),
}
t.subTasks = append(t.subTasks, subTask)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/projectreferencefilemapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type projectReferenceFileMapper struct {
}

func (mapper *projectReferenceFileMapper) init(loader *fileLoader, rootTasks []*projectReferenceParseTask) {
totalReferences := loader.projectReferenceParseTasks.tasksByFileName.Size() + 1
totalReferences := loader.projectReferenceParseTasks.tasksByFilePath.Size() + 1
mapper.loader = loader
mapper.configToProjectReference = make(map[tspath.Path]*tsoptions.ParsedCommandLine, totalReferences)
mapper.referencesInConfigFile = make(map[tspath.Path][]tspath.Path, totalReferences)
Expand All @@ -36,7 +36,7 @@ func (mapper *projectReferenceFileMapper) init(loader *fileLoader, rootTasks []*
loader,
rootTasks,
func(task *projectReferenceParseTask, referencesInConfig []tspath.Path) {
path := loader.toPath(task.configName)
path := task.Path()
mapper.configToProjectReference[path] = task.resolved
if task.resolved == nil || mapper.opts.Config.ConfigFile == task.resolved.ConfigFile {
return
Expand Down
13 changes: 8 additions & 5 deletions internal/compiler/projectreferenceparsetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package compiler
import (
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
)

type projectReferenceParseTask struct {
loaded bool
configPath tspath.Path
configName string
resolved *tsoptions.ParsedCommandLine
subTasks []*projectReferenceParseTask
}

func (t *projectReferenceParseTask) FileName() string {
return t.configName
func (t *projectReferenceParseTask) Path() tspath.Path {
return t.configPath
}

func (t *projectReferenceParseTask) load(loader *fileLoader) {
t.loaded = true

t.resolved = loader.opts.Host.GetResolvedProjectReference(t.configName, loader.toPath(t.configName))
t.resolved = loader.opts.Host.GetResolvedProjectReference(t.configName, t.configPath)
if t.resolved == nil {
return
}
Expand All @@ -32,7 +34,7 @@ func (t *projectReferenceParseTask) load(loader *fileLoader) {
if len(subReferences) == 0 {
return
}
t.subTasks = createProjectReferenceParseTasks(subReferences)
t.subTasks = createProjectReferenceParseTasks(subReferences, loader)
}

func (t *projectReferenceParseTask) getSubTasks() []*projectReferenceParseTask {
Expand Down Expand Up @@ -61,10 +63,11 @@ func (t *projectReferenceParseTask) isFromExternalLibrary() bool {

func (t *projectReferenceParseTask) markFromExternalLibrary() {}

func createProjectReferenceParseTasks(projectReferences []string) []*projectReferenceParseTask {
func createProjectReferenceParseTasks(projectReferences []string, loader *fileLoader) []*projectReferenceParseTask {
return core.Map(projectReferences, func(configName string) *projectReferenceParseTask {
return &projectReferenceParseTask{
configName: configName,
configPath: loader.toPath(configName),
}
})
}
4 changes: 2 additions & 2 deletions internal/execute/testsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ interface Symbol {
declare const console: { log(msg: any): void; };
`)

func newTestSys(fileOrFolderList FileMap, cwd string) *testSys {
func newTestSys(fileOrFolderList FileMap, cwd string, useCaseSensitiveFileNames bool) *testSys {
if cwd == "" {
cwd = "/home/src/workspaces/project"
}
sys := &testSys{
fs: &incrementaltestutil.FsHandlingBuildInfo{
FS: &testFs{
FS: vfstest.FromMap(fileOrFolderList, true /*useCaseSensitiveFileNames*/),
FS: vfstest.FromMap(fileOrFolderList, useCaseSensitiveFileNames),
},
},
defaultLibraryPath: tscLibPath,
Expand Down
48 changes: 48 additions & 0 deletions internal/execute/tsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,54 @@ func TestTscCommandline(t *testing.T) {
subScenario: "Parse watch interval option without tsconfig.json",
commandLineArgs: []string{"-w", "--watchInterval", "1000"},
},
{
subScenario: "Compile incremental with case insensitive file names",
commandLineArgs: []string{"-p", "."},
files: FileMap{
"/home/project/tsconfig.json": stringtestutil.Dedent(`
{
"compilerOptions": {
"incremental": true
},
}`),
"/home/project/src/index.ts": stringtestutil.Dedent(`
import type { Foo1 } from 'lib1';
import type { Foo2 } from 'lib2';
export const foo1: Foo1 = { foo: "a" };
export const foo2: Foo2 = { foo: "b" };`),
"/home/node_modules/lib1/index.d.ts": stringtestutil.Dedent(`
import type { Foo } from 'someLib';
export type { Foo as Foo1 };`),
"/home/node_modules/lib1/package.json": stringtestutil.Dedent(`
{
"name": "lib1"
}`),
"/home/node_modules/lib2/index.d.ts": stringtestutil.Dedent(`
import type { Foo } from 'somelib';
export type { Foo as Foo2 };
export declare const foo2: Foo;`),
"/home/node_modules/lib2/package.json": stringtestutil.Dedent(`
{
"name": "lib2"
}
`),
"/home/node_modules/someLib/index.d.ts": stringtestutil.Dedent(`
import type { Str } from 'otherLib';
export type Foo = { foo: Str; };`),
"/home/node_modules/someLib/package.json": stringtestutil.Dedent(`
{
"name": "somelib"
}`),
"/home/node_modules/otherLib/index.d.ts": stringtestutil.Dedent(`
export type Str = string;`),
"/home/node_modules/otherLib/package.json": stringtestutil.Dedent(`
{
"name": "otherlib"
}`),
},
cwd: "/home/project",
ignoreCase: true,
},
}

for _, testCase := range testCases {
Expand Down
5 changes: 3 additions & 2 deletions internal/execute/tsctestrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type tscInput struct {
files FileMap
cwd string
edits []*testTscEdit
ignoreCase bool
}

func (test *tscInput) executeCommand(sys *testSys, baselineBuilder *strings.Builder, commandLineArgs []string) execute.CommandLineResult {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (test *tscInput) run(t *testing.T, scenario string) {
t.Parallel()
// initial test tsc compile
baselineBuilder := &strings.Builder{}
sys := newTestSys(test.files, test.cwd)
sys := newTestSys(test.files, test.cwd, !test.ignoreCase)
fmt.Fprint(
baselineBuilder,
"currentDirectory::",
Expand Down Expand Up @@ -101,7 +102,7 @@ func (test *tscInput) run(t *testing.T, scenario string) {
})
wg.Queue(func() {
// Compute build with all the edits
nonIncrementalSys = newTestSys(test.files, test.cwd)
nonIncrementalSys = newTestSys(test.files, test.cwd, !test.ignoreCase)
for i := range index + 1 {
if test.edits[i].edit != nil {
test.edits[i].edit(nonIncrementalSys)
Expand Down
Loading
Loading