Skip to content

Commit 9f9f371

Browse files
Sheraffautofix-ci[bot]schiller-manuel
authored
fix: revert plugin changes, createCsrfMiddleware compilation, fix HMR tests (#7400)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Manuel Schiller <[email protected]>
1 parent 1f7b318 commit 9f9f371

9 files changed

Lines changed: 435 additions & 119 deletions

File tree

‎.changeset/strong-trains-act.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
'@tanstack/start-client-core': minor
32
'@tanstack/start-plugin-core': patch
43
'@tanstack/start-server-core': patch
54
'@tanstack/start-fn-stubs': patch

‎e2e/react-start/hmr/package.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
"vite": "^8.0.0"
3636
},
3737
"nx": {
38+
"targets": {
39+
"test:e2e": {
40+
"parallelism": false
41+
},
42+
"test:e2e--vite-ssr": {
43+
"parallelism": false
44+
},
45+
"test:e2e--rsbuild-ssr": {
46+
"parallelism": false
47+
}
48+
},
3849
"metadata": {
3950
"playwrightModes": [
4051
{

‎packages/start-client-core/src/createCsrfMiddleware.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type CreateCsrfMiddleware = <TRegister, TMiddlewares>(
7979

8080
const innerCreateCsrfMiddleware: CreateCsrfMiddleware = (opts = {}) => {
8181
const middleware = createMiddleware().server(async (ctx) => {
82-
const csrfCtx = ctx as RequestServerOptions<any, any> & typeof ctx
82+
const csrfCtx = ctx as RequestServerOptions<any, any>
8383

8484
if (opts.filter && !(await opts.filter(csrfCtx))) {
8585
return ctx.next()

‎packages/start-plugin-core/src/start-compiler/compiler.ts‎

Lines changed: 168 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type Binding =
4040
resolvedKind?: Kind
4141
}
4242

43+
type ImportBinding = Extract<Binding, { type: 'import' }>
44+
4345
type Kind = 'None' | `Root` | `Builder` | LookupKind
4446

4547
export type BuiltInLookupKind =
@@ -270,6 +272,12 @@ export type LookupConfig = {
270272
kind: LookupKind | 'Root' // 'Root' for builder pattern, LookupKind for direct call
271273
}
272274

275+
interface ExportResolution {
276+
moduleInfo: ModuleInfo
277+
localName: string
278+
binding: Binding
279+
}
280+
273281
interface ModuleInfo {
274282
id: string
275283
bindings: Map<string, Binding>
@@ -463,7 +471,7 @@ export class StartCompiler {
463471
private resolveIdCache = new Map<string, string | null>()
464472
private exportResolutionCache = new Map<
465473
string,
466-
Map<string, { moduleInfo: ModuleInfo; binding: Binding } | null>
474+
Map<string, ExportResolution | null>
467475
>()
468476
// Fast lookup for direct imports from known libraries (e.g., '@tanstack/react-start')
469477
// Maps: libName → (exportName → Kind)
@@ -702,9 +710,12 @@ export class StartCompiler {
702710
this.knownRootImports.set(
703711
'@tanstack/start-client-core',
704712
new Map<string, Kind>([
713+
['createServerFn', 'Root'],
705714
['createIsomorphicFn', 'IsomorphicFn'],
706715
['createServerOnlyFn', 'ServerOnlyFn'],
707716
['createClientOnlyFn', 'ClientOnlyFn'],
717+
['createMiddleware', 'Middleware'],
718+
['createStart', 'Root'],
708719
]),
709720
)
710721

@@ -1455,7 +1466,7 @@ export class StartCompiler {
14551466

14561467
// TODO improve cycle detection? should we throw here instead of returning 'None'?
14571468
// prevent cycles
1458-
const vKey = `${id}:${ident}`
1469+
const vKey = `${cleanId(id)}:${ident}`
14591470
if (visited.has(vKey)) {
14601471
return 'None'
14611472
}
@@ -1474,7 +1485,7 @@ export class StartCompiler {
14741485
moduleInfo: ModuleInfo,
14751486
exportName: string,
14761487
visitedModules = new Set<string>(),
1477-
): Promise<{ moduleInfo: ModuleInfo; binding: Binding } | undefined> {
1488+
): Promise<ExportResolution | undefined> {
14781489
const isBuildMode = this.mode === 'build'
14791490

14801491
// Check cache first (only for top-level calls in build mode)
@@ -1499,7 +1510,7 @@ export class StartCompiler {
14991510
if (localBindingName) {
15001511
const binding = moduleInfo.bindings.get(localBindingName)
15011512
if (binding) {
1502-
const result = { moduleInfo, binding }
1513+
const result = { moduleInfo, localName: localBindingName, binding }
15031514
// Cache the result (build mode only)
15041515
if (isBuildMode) {
15051516
this.getExportResolutionCache(moduleInfo.id).set(exportName, result)
@@ -1548,58 +1559,165 @@ export class StartCompiler {
15481559
return undefined
15491560
}
15501561

1551-
private async resolveBindingKind(
1552-
binding: Binding,
1553-
fileId: string,
1562+
private async resolveBindingTarget(
1563+
resolution: ExportResolution,
15541564
visited = new Set<string>(),
1565+
): Promise<ExportResolution | undefined> {
1566+
const key = `${cleanId(resolution.moduleInfo.id)}:${resolution.localName}`
1567+
if (visited.has(key)) {
1568+
return undefined
1569+
}
1570+
visited.add(key)
1571+
1572+
if (resolution.binding.type !== 'import') {
1573+
return resolution
1574+
}
1575+
1576+
const target = await this.resolveIdCached(
1577+
resolution.binding.source,
1578+
resolution.moduleInfo.id,
1579+
)
1580+
if (!target) {
1581+
return undefined
1582+
}
1583+
1584+
const importedModule = await this.getModuleInfo(target)
1585+
const found = await this.findExportInModule(
1586+
importedModule,
1587+
resolution.binding.importedName,
1588+
)
1589+
if (!found) {
1590+
return undefined
1591+
}
1592+
1593+
return this.resolveBindingTarget(found, visited)
1594+
}
1595+
1596+
private async resolveKnownImportKind(
1597+
binding: ImportBinding,
1598+
resolved?: ExportResolution,
15551599
): Promise<Kind> {
1556-
if (binding.resolvedKind) {
1557-
return binding.resolvedKind
1600+
const directKind =
1601+
this.knownRootImports.get(binding.source)?.get(binding.importedName) ??
1602+
'None'
1603+
if (directKind !== 'None') {
1604+
return directKind
15581605
}
1559-
if (binding.type === 'import') {
1560-
// Fast path: check if this is a direct import from a known library
1561-
// (e.g., import { createServerFn } from '@tanstack/react-start')
1562-
// This avoids async resolveId calls for the common case
1563-
const knownExports = this.knownRootImports.get(binding.source)
1564-
if (knownExports) {
1565-
const kind = knownExports.get(binding.importedName)
1566-
if (kind) {
1567-
binding.resolvedKind = kind
1568-
return kind
1569-
}
1606+
1607+
if (!resolved) {
1608+
return 'None'
1609+
}
1610+
1611+
for (const [source, exports] of this.knownRootImports) {
1612+
const kind = exports.get(binding.importedName)
1613+
if (!kind) {
1614+
continue
15701615
}
15711616

1572-
// Slow path: resolve through the module graph
1573-
const target = await this.resolveIdCached(binding.source, fileId)
1574-
if (!target) {
1575-
return 'None'
1617+
let targetId: string | null
1618+
try {
1619+
targetId = await this.resolveIdCached(source, resolved.moduleInfo.id)
1620+
} catch {
1621+
continue
15761622
}
15771623

1578-
const importedModule = await this.getModuleInfo(target)
1624+
if (!targetId) {
1625+
continue
1626+
}
15791627

1580-
// Find the export, recursively searching through export * from chains
1581-
const found = await this.findExportInModule(
1582-
importedModule,
1583-
binding.importedName,
1584-
)
1628+
try {
1629+
const rootModule = await this.getModuleInfo(targetId)
1630+
const found = await this.findExportInModule(
1631+
rootModule,
1632+
binding.importedName,
1633+
)
1634+
const target = found
1635+
? ((await this.resolveBindingTarget(found)) ?? found)
1636+
: undefined
15851637

1586-
if (!found) {
1587-
return 'None'
1638+
// Match by resolved binding identity, not by export name alone.
1639+
if (
1640+
target &&
1641+
cleanId(resolved.moduleInfo.id) === cleanId(target.moduleInfo.id) &&
1642+
resolved.localName === target.localName
1643+
) {
1644+
return kind
1645+
}
1646+
} catch {
1647+
continue
15881648
}
1649+
}
15891650

1590-
const { moduleInfo: foundModule, binding: foundBinding } = found
1651+
return 'None'
1652+
}
15911653

1592-
if (foundBinding.resolvedKind) {
1593-
return foundBinding.resolvedKind
1594-
}
1654+
private async resolveImportKind(
1655+
binding: ImportBinding,
1656+
fileId: string,
1657+
visited: Set<string>,
1658+
): Promise<Kind> {
1659+
const directKnownKind = await this.resolveKnownImportKind(binding)
1660+
if (directKnownKind !== 'None') {
1661+
binding.resolvedKind = directKnownKind
1662+
return directKnownKind
1663+
}
15951664

1596-
const resolvedKind = await this.resolveBindingKind(
1597-
foundBinding,
1598-
foundModule.id,
1599-
visited,
1600-
)
1601-
foundBinding.resolvedKind = resolvedKind
1602-
return resolvedKind
1665+
if (binding.importedName === '*') {
1666+
return 'None'
1667+
}
1668+
1669+
const target = await this.resolveIdCached(binding.source, fileId)
1670+
if (!target) {
1671+
return 'None'
1672+
}
1673+
1674+
const importedModule = await this.getModuleInfo(target)
1675+
const found = await this.findExportInModule(
1676+
importedModule,
1677+
binding.importedName,
1678+
)
1679+
if (!found) {
1680+
return 'None'
1681+
}
1682+
1683+
const knownKind = await this.resolveKnownImportKind(binding, found)
1684+
if (knownKind !== 'None') {
1685+
found.binding.resolvedKind = knownKind
1686+
binding.resolvedKind = knownKind
1687+
return knownKind
1688+
}
1689+
1690+
if (found.binding.resolvedKind) {
1691+
return found.binding.resolvedKind
1692+
}
1693+
1694+
// Import aliases can form cycles, e.g. A re-exports from B while B
1695+
// re-exports from A. Track the exported binding before following it.
1696+
const vKey = `${cleanId(found.moduleInfo.id)}:${found.localName}`
1697+
if (visited.has(vKey)) {
1698+
return 'None'
1699+
}
1700+
visited.add(vKey)
1701+
1702+
const resolvedKind = await this.resolveBindingKind(
1703+
found.binding,
1704+
found.moduleInfo.id,
1705+
visited,
1706+
)
1707+
found.binding.resolvedKind = resolvedKind
1708+
return resolvedKind
1709+
}
1710+
1711+
private async resolveBindingKind(
1712+
binding: Binding,
1713+
fileId: string,
1714+
visited = new Set<string>(),
1715+
): Promise<Kind> {
1716+
if (binding.resolvedKind) {
1717+
return binding.resolvedKind
1718+
}
1719+
if (binding.type === 'import') {
1720+
return this.resolveImportKind(binding, fileId, visited)
16031721
}
16041722

16051723
const resolvedKind = await this.resolveExprKind(
@@ -1743,36 +1861,15 @@ export class StartCompiler {
17431861
binding.type === 'import' &&
17441862
binding.importedName === '*'
17451863
) {
1746-
const knownExports = this.knownRootImports.get(binding.source)
1747-
const knownKind = knownExports?.get(callee.property.name)
1748-
if (knownKind) {
1749-
return knownKind
1750-
}
1751-
1752-
// resolve the property from the target module
1753-
const targetModuleId = await this.resolveIdCached(
1754-
binding.source,
1864+
return this.resolveImportKind(
1865+
{
1866+
type: 'import',
1867+
source: binding.source,
1868+
importedName: callee.property.name,
1869+
},
17551870
fileId,
1871+
visited,
17561872
)
1757-
if (targetModuleId) {
1758-
const targetModule = await this.getModuleInfo(targetModuleId)
1759-
const localBindingName = targetModule.exports.get(
1760-
callee.property.name,
1761-
)
1762-
if (localBindingName) {
1763-
const exportedBinding =
1764-
targetModule.bindings.get(localBindingName)
1765-
if (exportedBinding) {
1766-
return await this.resolveBindingKind(
1767-
exportedBinding,
1768-
targetModule.id,
1769-
visited,
1770-
)
1771-
}
1772-
}
1773-
} else {
1774-
return 'None'
1775-
}
17761873
}
17771874
}
17781875
return this.resolveExprKind(callee.object, fileId, visited)

0 commit comments

Comments
 (0)