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 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
38 changes: 32 additions & 6 deletions internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,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 @@ -249,7 +256,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 @@ -304,12 +318,24 @@ func (p *fileLoader) addProjectReferenceTasks(singleThreaded bool) {
}
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 @@ -405,7 +431,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
increaseDepth: resolved.IsExternalLibraryImport,
elideOnDepth: false,
isFromExternalLibrary: resolved.IsExternalLibraryImport,
}, false)
}, false, p)
}
}

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

Expand Down
26 changes: 16 additions & 10 deletions internal/compiler/filesparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (t *parseTask) Path() tspath.Path {

func (t *parseTask) load(loader *fileLoader) {
t.loaded = true
t.path = loader.toPath(t.normalizedFilePath)
if t.isForAutomaticTypeDirective {
t.loadAutomaticTypeDirectives(loader)
return
Expand All @@ -74,7 +73,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 @@ -83,7 +82,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 @@ -94,14 +93,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 @@ -112,21 +117,22 @@ 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)
}

type filesParser struct {
wg core.WorkGroup
tasksByFileName collections.SyncMap[string, *queuedParseTask]
tasksByFilePath collections.SyncMap[tspath.Path, *queuedParseTask]
maxDepth int
}

Expand All @@ -146,7 +152,7 @@ func (w *filesParser) start(loader *fileLoader, tasks []*parseTask, depth int, i
for i, task := range tasks {
taskIsFromExternalLibrary := isFromExternalLibrary || task.fromExternalLibrary
newTask := &queuedParseTask{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 @@ -195,7 +201,7 @@ func (w *filesParser) start(loader *fileLoader, tasks []*parseTask, depth int, i

func (w *filesParser) collect(loader *fileLoader, tasks []*parseTask, iterate func(*parseTask)) []tspath.Path {
// Mark all tasks we saw as external after the fact.
w.tasksByFileName.Range(func(key string, value *queuedParseTask) bool {
w.tasksByFilePath.Range(func(_ tspath.Path, value *queuedParseTask) bool {
if value.fromExternalLibrary {
value.task.fromExternalLibrary = true
}
Expand All @@ -216,7 +222,7 @@ func (w *filesParser) collectWorker(loader *fileLoader, tasks []*parseTask, iter
w.collectWorker(loader, subTasks, iterate, seen)
}
iterate(task)
results = append(results, loader.toPath(task.FileName()))
results = append(results, task.Path())
}
return results
}
6 changes: 3 additions & 3 deletions internal/compiler/projectreferenceparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func createProjectReferenceParseTasks(projectReferences []string) []*projectRefe
type projectReferenceParser struct {
loader *fileLoader
wg core.WorkGroup
tasksByFileName collections.SyncMap[tspath.Path, *projectReferenceParseTask]
tasksByFilePath collections.SyncMap[tspath.Path, *projectReferenceParseTask]
}

func (p *projectReferenceParser) parse(tasks []*projectReferenceParseTask) {
Expand All @@ -52,7 +52,7 @@ func (p *projectReferenceParser) parse(tasks []*projectReferenceParseTask) {
func (p *projectReferenceParser) start(tasks []*projectReferenceParseTask) {
for i, task := range tasks {
path := p.loader.toPath(task.configName)
if loadedTask, loaded := p.tasksByFileName.LoadOrStore(path, task); loaded {
if loadedTask, loaded := p.tasksByFilePath.LoadOrStore(path, task); loaded {
// dedup tasks to ensure correct file order, regardless of which task would be started first
tasks[i] = loadedTask
} else {
Expand All @@ -65,7 +65,7 @@ func (p *projectReferenceParser) start(tasks []*projectReferenceParseTask) {
}

func (p *projectReferenceParser) initMapper(tasks []*projectReferenceParseTask) {
totalReferences := p.tasksByFileName.Size() + 1
totalReferences := p.tasksByFilePath.Size() + 1
p.loader.projectReferenceFileMapper.configToProjectReference = make(map[tspath.Path]*tsoptions.ParsedCommandLine, totalReferences)
p.loader.projectReferenceFileMapper.referencesInConfigFile = make(map[tspath.Path][]tspath.Path, totalReferences)
p.loader.projectReferenceFileMapper.sourceToOutput = make(map[tspath.Path]*tsoptions.OutputDtsAndProjectReference)
Expand Down
14 changes: 7 additions & 7 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 Expand Up @@ -185,10 +185,10 @@ func (s *testSys) baselineProgram(baseline *strings.Builder, program *incrementa
for _, file := range program.GetProgram().GetSourceFiles() {
if diagnostics, ok := testingData.SemanticDiagnosticsPerFile.Load(file.Path()); ok {
if oldDiagnostics, ok := testingData.OldProgramSemanticDiagnosticsPerFile.Load(file.Path()); !ok || oldDiagnostics != diagnostics {
baseline.WriteString("*refresh* " + file.FileName() + "\n")
baseline.WriteString("*refresh* " + string(file.Path()) + "\n")
}
} else {
baseline.WriteString("*not cached* " + file.FileName() + "\n")
baseline.WriteString("*not cached* " + string(file.Path()) + "\n")
}
}

Expand All @@ -198,11 +198,11 @@ func (s *testSys) baselineProgram(baseline *strings.Builder, program *incrementa
if kind, ok := testingData.UpdatedSignatureKinds[file.Path()]; ok {
switch kind {
case incremental.SignatureUpdateKindComputedDts:
baseline.WriteString("(computed .d.ts) " + file.FileName() + "\n")
baseline.WriteString("(computed .d.ts) " + string(file.Path()) + "\n")
case incremental.SignatureUpdateKindStoredAtEmit:
baseline.WriteString("(stored at emit) " + file.FileName() + "\n")
baseline.WriteString("(stored at emit) " + string(file.Path()) + "\n")
case incremental.SignatureUpdateKindUsedVersion:
baseline.WriteString("(used version) " + file.FileName() + "\n")
baseline.WriteString("(used version) " + string(file.Path()) + "\n")
}
}
}
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