Skip to content
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
20 changes: 6 additions & 14 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@ import * as vscode from 'vscode'
import type { FormatDiagnosticsHost } from 'typescript'

import { log } from './logger'
import { findClosestTsconfig } from './tsconfigSelection'

export async function getTsconfigFile(path: string) {
let files = await vscode.workspace.findFiles('**/tsconfig.json', '**​/node_modules/**')
files = files.sort((a, b) => b.fsPath.length - a.fsPath.length)
.filter(p => !p.fsPath.includes('node_modules'))
export async function getTsconfigFile(filePath: string) {
const files = await vscode.workspace.findFiles('**/tsconfig.json', '**​/node_modules/**')
const tsconfigPath = findClosestTsconfig(filePath, files.map(file => file.fsPath))

log({ path, files })
log({ path: filePath, files, tsconfigPath })

const pathSegments = path.split('/')
for (let i = pathSegments.length; i > 0; i--) {
const path = pathSegments.slice(0, i).join('/')
const tsConfigFile = files.find(file => file.fsPath.startsWith(path))
if (tsConfigFile)
return tsConfigFile
}

return files[0]
return files.find(file => file.fsPath === tsconfigPath) ?? files[0]
}

const formatDiagnosticsHost: FormatDiagnosticsHost = {
Expand Down
21 changes: 21 additions & 0 deletions src/tsconfigSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'node:path'

export function findClosestTsconfig(filePath: string, tsconfigPaths: string[]): string | undefined {
const normalizedFilePath = normalizePath(filePath)
const candidates = tsconfigPaths
.filter(tsconfigPath => !isNodeModulesPath(tsconfigPath))
.sort((a, b) => b.length - a.length)

return candidates.find((tsconfigPath) => {
const configDirectory = normalizePath(path.dirname(tsconfigPath))
return normalizedFilePath === configDirectory || normalizedFilePath.startsWith(`${configDirectory}/`)
}) ?? candidates[0]
}

function normalizePath(filePath: string) {
return filePath.replaceAll('\\', '/').replace(/\/+$/, '')
}

function isNodeModulesPath(filePath: string) {
return normalizePath(filePath).split('/').includes('node_modules')
}
45 changes: 45 additions & 0 deletions test/tsconfigSelection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it } from 'vitest'

import { findClosestTsconfig } from '../src/tsconfigSelection'

describe('findClosestTsconfig', () => {
it('selects the nearest tsconfig that contains the target file', () => {
expect(findClosestTsconfig(
'/repo/packages/app/src/index.ts',
[
'/repo/tsconfig.json',
'/repo/packages/app/tsconfig.json',
],
)).toBe('/repo/packages/app/tsconfig.json')
})

it('does not select a sibling package tsconfig for a package without one', () => {
expect(findClosestTsconfig(
'/repo/packages/app-b/src/index.ts',
[
'/repo/tsconfig.json',
'/repo/packages/app-a/tsconfig.json',
],
)).toBe('/repo/tsconfig.json')
})

it('ignores node_modules tsconfig files', () => {
expect(findClosestTsconfig(
'/repo/packages/app/src/index.ts',
[
'/repo/packages/app/node_modules/some-package/tsconfig.json',
'/repo/tsconfig.json',
],
)).toBe('/repo/tsconfig.json')
})

it('handles Windows-style paths', () => {
expect(findClosestTsconfig(
'C:\\repo\\packages\\app\\src\\index.ts',
[
'C:\\repo\\tsconfig.json',
'C:\\repo\\packages\\app\\tsconfig.json',
],
)).toBe('C:\\repo\\packages\\app\\tsconfig.json')
})
})