Skip to content

Commit 2214687

Browse files
authored
fix(tsgo): resolve API subpaths from an aliased moduleUrl (#357)
1 parent 5b422f0 commit 2214687

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎src/require.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createRequire } from 'node:module'
2+
import path from 'node:path'
23
import { createDebug } from 'obug'
34

45
const nodeRequire: NodeJS.Require = createRequire(import.meta.url)
@@ -30,6 +31,18 @@ export function tryResolve(
3031
} catch {}
3132
}
3233

34+
// Unlike `require.resolve(id, { paths })`, this honors the self-reference of the
35+
// package containing `from`, so aliased packages resolve their own subpaths.
36+
export function tryResolveFrom(id: string, from: string): string | undefined {
37+
try {
38+
// Mocked resolvers in tests only understand the `paths` option.
39+
if (import.meta.TEST && requireImpl !== nodeRequire) {
40+
return requireImpl.resolve(id, { paths: [path.dirname(from)] })
41+
}
42+
return createRequire(from).resolve(id)
43+
} catch {}
44+
}
45+
3346
let _ts: typeof import('typescript') | undefined
3447

3548
export interface PackageInfo {

‎src/tsgo.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import path from 'node:path'
21
import { fileURLToPath, pathToFileURL } from 'node:url'
3-
import { getPackageInfo, require, tryRequire, tryResolve } from './require.ts'
2+
import {
3+
getPackageInfo,
4+
require,
5+
tryRequire,
6+
tryResolve,
7+
tryResolveFrom,
8+
} from './require.ts'
49
import type { Logger } from './options.ts'
510
import type * as TsgoApiModule from 'typescript-next/async'
611
import type * as TsgoFsModule from 'typescript-next/fs'
@@ -19,8 +24,7 @@ function resolveSubpath(
1924
subpath: string,
2025
): string | undefined {
2126
const packageName = PACKAGE_NAMES[compiler]
22-
const paths = [path.dirname(modulePath)]
23-
return tryResolve(`${packageName}/${subpath}`, { paths })
27+
return tryResolveFrom(`${packageName}/${subpath}`, modulePath)
2428
}
2529

2630
export function getDefaultTsgoModuleUrl(

‎tests/tsgo.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import fs from 'node:fs'
2-
import { mkdir, readFile, rm, symlink } from 'node:fs/promises'
2+
import {
3+
mkdir,
4+
mkdtemp,
5+
readFile,
6+
rm,
7+
symlink,
8+
writeFile,
9+
} from 'node:fs/promises'
310
import module, { createRequire } from 'node:module'
11+
import { tmpdir } from 'node:os'
412
import path from 'node:path'
513
import url, { fileURLToPath } from 'node:url'
614
import { rolldownBuild } from '@sxzz/test-utils'
@@ -74,6 +82,45 @@ test.each([
7482
).rejects.toThrow(message)
7583
})
7684

85+
test('loads the API from an aliased moduleUrl next to another typescript', async () => {
86+
const dir = await mkdtemp(path.join(tmpdir(), 'rolldown-plugin-dts-'))
87+
try {
88+
const writePackage = async (
89+
name: string,
90+
files: Record<string, string>,
91+
) => {
92+
for (const [file, content] of Object.entries(files)) {
93+
const filePath = path.join(dir, 'node_modules', name, file)
94+
await mkdir(path.dirname(filePath), { recursive: true })
95+
await writeFile(filePath, content)
96+
}
97+
}
98+
await writePackage('typescript', {
99+
'package.json': JSON.stringify({ name: 'typescript', version: '5.9.3' }),
100+
'async.js': 'module.exports = { Program: class {} }',
101+
})
102+
await writePackage('typescript-next', {
103+
'package.json': JSON.stringify({
104+
name: 'typescript',
105+
version: '7.1.0',
106+
exports: { '.': './lib/version.cjs', './async': './async.js' },
107+
}),
108+
'lib/version.cjs': '',
109+
'async.js':
110+
'module.exports = { Program: class { getDeclarationEmit() {} } }',
111+
})
112+
113+
const aliasedModuleUrl = url.pathToFileURL(
114+
path.join(dir, 'node_modules/typescript-next/lib/version.cjs'),
115+
).href
116+
expect(
117+
loadTsgoApi(aliasedModuleUrl).Program.prototype.getDeclarationEmit,
118+
).toBeTypeOf('function')
119+
} finally {
120+
await rm(dir, { recursive: true, force: true })
121+
}
122+
})
123+
77124
test('concurrent disk emits share configured project membership until the next build', async () => {
78125
const { Program, Snapshot } = loadTsgoApi(moduleUrl)
79126
const getSourceFileNames = vi.spyOn(Program.prototype, 'getSourceFileNames')

0 commit comments

Comments
 (0)