Skip to content

Commit 16ff7f0

Browse files
Copilotsandersn
andauthored
Fix tsconfig/jsconfig priority for JS files when both configs coexist in same directory (#3740)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sandersn <293473+sandersn@users.noreply.github.com>
1 parent 9e69d83 commit 16ff7f0

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

internal/project/configfileregistrybuilder.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,20 @@ func (c *configFileRegistryBuilder) computeConfigFileName(fileName string, skipS
601601
}
602602
}
603603

604-
skip := skipSearchInDirectoryOfFile
604+
// When searching for ancestor of a config file, determine which config types to skip
605+
// in the starting directory. This matches TSServer's forEachConfigFileLocation behavior:
606+
// - For ancestor of tsconfig.json: skip tsconfig.json but still check jsconfig.json
607+
// - For ancestor of jsconfig.json: skip both tsconfig.json and jsconfig.json
608+
skipTsconfig := skipSearchInDirectoryOfFile
609+
skipJsconfig := skipSearchInDirectoryOfFile && !strings.HasSuffix(fileName, "/tsconfig.json")
605610
result, _ := tspath.ForEachAncestorDirectory(searchPath, func(directory string) (result string, stop bool) {
606-
if !skip {
611+
if !skipTsconfig {
607612
tsconfigPath := tspath.CombinePaths(directory, "tsconfig.json")
608613
if c.FS().FileExists(tsconfigPath) {
609614
return tsconfigPath, true
610615
}
616+
}
617+
if !skipJsconfig {
611618
jsconfigPath := tspath.CombinePaths(directory, "jsconfig.json")
612619
if c.FS().FileExists(jsconfigPath) {
613620
return jsconfigPath, true
@@ -616,7 +623,8 @@ func (c *configFileRegistryBuilder) computeConfigFileName(fileName string, skipS
616623
if strings.HasSuffix(directory, "/node_modules") {
617624
return "", true
618625
}
619-
skip = false
626+
skipTsconfig = false
627+
skipJsconfig = false
620628
return "", false
621629
})
622630
logger.Logf("computeConfigFileName:: File: %s:: Result: %s", fileName, result)

internal/project/session_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,4 +1506,43 @@ export const value = content;`,
15061506
assert.Equal(t, sourceFile.Text(), `const greeting: string = "hello";`)
15071507
})
15081508
})
1509+
1510+
t.Run("jsconfig.json used for JS files when tsconfig.json exists in same directory", func(t *testing.T) {
1511+
t.Parallel()
1512+
files := map[string]any{
1513+
"/home/projects/TS/p1/tsconfig.json": `{
1514+
"compilerOptions": {
1515+
"noLib": true,
1516+
"strict": true
1517+
}
1518+
}`,
1519+
"/home/projects/TS/p1/jsconfig.json": `{
1520+
"compilerOptions": {
1521+
"noLib": true,
1522+
"checkJs": true
1523+
}
1524+
}`,
1525+
"/home/projects/TS/p1/index.ts": `export const x: number = 1;`,
1526+
"/home/projects/TS/p1/app.js": `/** @type {number} */ var y = "not a number";`,
1527+
}
1528+
session, _ := projecttestutil.Setup(files)
1529+
1530+
// Open the JS file - it should be assigned to the jsconfig.json project, not tsconfig.json
1531+
session.DidOpenFile(context.Background(), "file:///home/projects/TS/p1/app.js", 1, files["/home/projects/TS/p1/app.js"].(string), lsproto.LanguageKindJavaScript)
1532+
1533+
snapshot := session.Snapshot()
1534+
jsURI := lsproto.DocumentUri("file:///home/projects/TS/p1/app.js")
1535+
defaultProject := snapshot.GetDefaultProject(jsURI)
1536+
assert.Assert(t, defaultProject != nil, "JS file should have a default project")
1537+
assert.Equal(t, defaultProject.Name(), "/home/projects/TS/p1/jsconfig.json", "JS file should belong to jsconfig.json project, not tsconfig.json")
1538+
1539+
// Open the TS file - it should be assigned to tsconfig.json project
1540+
session.DidOpenFile(context.Background(), "file:///home/projects/TS/p1/index.ts", 1, files["/home/projects/TS/p1/index.ts"].(string), lsproto.LanguageKindTypeScript)
1541+
1542+
snapshot = session.Snapshot()
1543+
tsURI := lsproto.DocumentUri("file:///home/projects/TS/p1/index.ts")
1544+
defaultTSProject := snapshot.GetDefaultProject(tsURI)
1545+
assert.Assert(t, defaultTSProject != nil, "TS file should have a default project")
1546+
assert.Equal(t, defaultTSProject.Name(), "/home/projects/TS/p1/tsconfig.json", "TS file should belong to tsconfig.json project")
1547+
})
15091548
}

0 commit comments

Comments
 (0)