Skip to content

Commit b5fc9f8

Browse files
fix: canonicalize tsconfig directories for physical source IDs (#351)
Co-authored-by: Kevin Bot <[email protected]>
1 parent 184cec3 commit b5fc9f8

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎src/options.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { realpathSync } from 'node:fs'
12
import path from 'node:path'
23
import process from 'node:process'
34
import { isDeepStrictEqual } from 'node:util'
@@ -444,6 +445,15 @@ export function resolveOptions({
444445
} else {
445446
tsconfig = undefined
446447
}
448+
if (tsconfig) {
449+
// Rolldown module IDs are real paths, so resolve a symlinked directory for
450+
// the compiler to match them. Keep the file name: a symlinked tsconfig
451+
// still resolves `include` from where it is linked.
452+
tsconfig = path.join(
453+
realpathSync(path.dirname(tsconfig)),
454+
path.basename(tsconfig),
455+
)
456+
}
447457

448458
compilerOptions = {
449459
...resolvedTsconfig?.compilerOptions,

‎tests/index.test.ts‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
1+
import {
2+
cp,
3+
mkdir,
4+
mkdtemp,
5+
readFile,
6+
realpath,
7+
rm,
8+
symlink,
9+
writeFile,
10+
} from 'node:fs/promises'
211
import os from 'node:os'
312
import path from 'node:path'
413
import { fileURLToPath } from 'node:url'
514
import { normalizePath, rolldownBuild } from '@sxzz/test-utils'
615
import { describe, expect, test, vi } from 'vitest'
7-
import { dts, resolveOptions, type Generator } from '../src/index.ts'
16+
import {
17+
dts,
18+
resolveOptions,
19+
type Generator,
20+
type Options,
21+
} from '../src/index.ts'
822
import type { Plugin, TransformResult } from 'rolldown'
923

1024
const dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -95,6 +109,52 @@ describe('resolve paths', () => {
95109
})
96110
})
97111

112+
test.each<[string, Options]>([
113+
['tsc', { generator: 'tsc', tsc: { newContext: true } }],
114+
['tsc build', { generator: 'tsc', tsc: { build: true } }],
115+
[
116+
'tsgo',
117+
{
118+
generator: 'tsgo',
119+
tsgo: { moduleUrl: import.meta.resolve('typescript-next') },
120+
},
121+
],
122+
])('%s: tsconfig in a symlinked directory', async (_, options) => {
123+
const dir = await realpath(
124+
await mkdtemp(path.join(os.tmpdir(), 'rolldown-plugin-dts-')),
125+
)
126+
try {
127+
const project = path.join(dir, 'project')
128+
const alias = path.join(dir, 'alias')
129+
await mkdir(project)
130+
await writeFile(
131+
path.join(project, 'index.ts'),
132+
'export const value: number = 1',
133+
)
134+
// `files` must still resolve from `project`, not from the link target.
135+
await writeFile(
136+
path.join(dir, 'shared.json'),
137+
JSON.stringify({ files: ['index.ts'] }),
138+
)
139+
await symlink(
140+
path.join(dir, 'shared.json'),
141+
path.join(project, 'tsconfig.json'),
142+
)
143+
await symlink(project, alias, 'junction')
144+
145+
const { snapshot } = await rolldownBuild(path.join(project, 'index.ts'), [
146+
dts({
147+
...options,
148+
tsconfig: path.join(alias, 'tsconfig.json'),
149+
dtsOnly: true,
150+
}),
151+
])
152+
expect(snapshot).toContain('value: number')
153+
} finally {
154+
await rm(dir, { recursive: true, force: true })
155+
}
156+
})
157+
98158
// Test alias mapping based on rolldown input option
99159
test('input alias', async () => {
100160
const root = path.resolve(dirname, 'fixtures/alias')

0 commit comments

Comments
 (0)