Skip to content

Commit 99fc3d3

Browse files
authored
Make inferred project root files stable ordered (#1638)
1 parent a94299b commit 99fc3d3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

internal/project/projectcollectionbuilder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ func (b *projectCollectionBuilder) DidChangeFiles(summary FileChangeSummary, log
258258
b.deleteConfiguredProject(p, logger)
259259
}
260260
}
261+
slices.Sort(inferredProjectFiles)
261262
b.updateInferredProjectRoots(inferredProjectFiles, logger)
262263
b.configFileRegistryBuilder.Cleanup()
263264
}

internal/project/projectcollectionbuilder_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,36 @@ func TestProjectCollectionBuilder(t *testing.T) {
439439
// triggering a crash.
440440
session.DidOpenFile(context.Background(), "file:///project/index.ts", 1, files["/project/index.ts"].(string), lsproto.LanguageKindTypeScript)
441441
})
442+
443+
t.Run("inferred project root files are in stable order", func(t *testing.T) {
444+
t.Parallel()
445+
files := map[string]any{
446+
"/project/a.ts": `export const a = 1;`,
447+
"/project/b.ts": `export const b = 1;`,
448+
"/project/c.ts": `export const c = 1;`,
449+
}
450+
451+
session, _ := projecttestutil.Setup(files)
452+
453+
// b, c, a
454+
session.DidOpenFile(context.Background(), "file:///project/b.ts", 1, files["/project/b.ts"].(string), lsproto.LanguageKindTypeScript)
455+
session.DidOpenFile(context.Background(), "file:///project/c.ts", 1, files["/project/c.ts"].(string), lsproto.LanguageKindTypeScript)
456+
session.DidOpenFile(context.Background(), "file:///project/a.ts", 1, files["/project/a.ts"].(string), lsproto.LanguageKindTypeScript)
457+
458+
snapshot, release := session.Snapshot()
459+
defer release()
460+
assert.Equal(t, len(snapshot.ProjectCollection.Projects()), 1)
461+
inferredProject := snapshot.ProjectCollection.InferredProject()
462+
assert.Assert(t, inferredProject != nil)
463+
// It's more bookkeeping to maintain order of opening, since any file can move into or out of
464+
// the inferred project due to changes in other projects. Order shouldn't matter for correctness,
465+
// we just want it to be consistent, in case there are observable type ordering issues.
466+
assert.DeepEqual(t, inferredProject.Program.CommandLine().FileNames(), []string{
467+
"/project/a.ts",
468+
"/project/b.ts",
469+
"/project/c.ts",
470+
})
471+
})
442472
}
443473

444474
func filesForSolutionConfigFile(solutionRefs []string, compilerOptions string, ownFiles []string) map[string]any {

0 commit comments

Comments
 (0)