-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(start): prevent false positives for transitively resolved imports… #6722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,943
−1,653
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,5 @@ port-*.txt | |
| .tanstack | ||
|
|
||
| webserver-*.log | ||
| error-build-result.json | ||
| error-build.log | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
e2e/react-start/import-protection/src/routes/beforeload-leak.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| import { getSessionFromRequest } from '../violations/beforeload-server-leak' | ||
|
|
||
| export const Route = createFileRoute('/beforeload-leak')({ | ||
| // beforeLoad is NOT stripped by the compiler on the client side. | ||
| // It is not in splitRouteIdentNodes or deleteNodes, so this import | ||
| // chain survives: beforeload-leak.tsx -> beforeload-server-leak.ts | ||
| // -> @tanstack/react-start/server | ||
| // This is a TRUE POSITIVE violation in the client environment. | ||
| beforeLoad: () => { | ||
| const session = getSessionFromRequest() | ||
| return { session } | ||
| }, | ||
| component: BeforeloadLeakRoute, | ||
| }) | ||
|
|
||
| function BeforeloadLeakRoute() { | ||
| return ( | ||
| <div> | ||
| <h1 data-testid="beforeload-leak-heading">Beforeload Leak</h1> | ||
| <p data-testid="beforeload-leak-status">Route loaded</p> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
e2e/react-start/import-protection/src/violations/beforeload-server-leak.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { getRequest } from '@tanstack/react-start/server' | ||
|
|
||
| // This utility wraps a denied server import and is used in a route's | ||
| // `beforeLoad` hook. `beforeLoad` is NOT in the compiler's | ||
| // splitRouteIdentNodes or deleteNodes lists, so it survives on the client. | ||
| // Using this module in `beforeLoad` is therefore a TRUE POSITIVE violation. | ||
| export function getSessionFromRequest() { | ||
| const req = getRequest() | ||
| return { sessionId: req.headers.get('x-session-id') } | ||
| } |
9 changes: 9 additions & 0 deletions
9
e2e/react-start/import-protection/src/violations/cross-boundary-leak/leaky-consumer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { getSharedData } from './shared-util' | ||
|
|
||
| // Leaky: uses the shared utility OUTSIDE any compiler boundary. | ||
| // This must still trigger a violation in the client environment | ||
| // even if safe-consumer.ts already loaded shared-util.ts via | ||
| // a fetchModule chain that silenced its resolveId. | ||
| export function leakyGetSharedData() { | ||
| return getSharedData() | ||
| } |
9 changes: 9 additions & 0 deletions
9
e2e/react-start/import-protection/src/violations/cross-boundary-leak/safe-consumer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { createServerFn } from '@tanstack/react-start' | ||
| import { getSharedData } from './shared-util' | ||
|
|
||
| // Safe: uses the shared utility ONLY inside a compiler boundary. | ||
| // The compiler strips this from the client; fetchModule adds shared-util.ts | ||
| // to the serverFnLookupModules set. | ||
| export const safeFn = createServerFn().handler(async () => { | ||
| return getSharedData() | ||
| }) |
10 changes: 10 additions & 0 deletions
10
e2e/react-start/import-protection/src/violations/cross-boundary-leak/shared-util.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { getRequest } from '@tanstack/react-start/server' | ||
|
|
||
| // Utility that wraps a denied server import. It is consumed by BOTH a | ||
| // safe consumer (inside compiler boundaries) AND a leaky consumer (outside | ||
| // any boundary). The leaky consumer must still trigger a violation even | ||
| // if the safe consumer's fetchModule chain silences the initial resolve. | ||
| export function getSharedData() { | ||
| const req = getRequest() | ||
| return { method: req.method } | ||
| } |
15 changes: 15 additions & 0 deletions
15
e2e/react-start/import-protection/src/violations/cross-boundary-safe/auth-wrapper.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { createMiddleware, createServerFn } from '@tanstack/react-start' | ||
| import { getSessionData } from './session-util' | ||
|
|
||
| // This middleware uses the session utility inside a compiler boundary. | ||
| // The compiler should strip the import of session-util from the client. | ||
| const authMiddleware = createMiddleware({ type: 'function' }).server( | ||
| ({ next }) => { | ||
| const data = getSessionData() | ||
| return next({ context: { session: data } }) | ||
| }, | ||
| ) | ||
|
|
||
| // Exports a pre-configured server fn with the middleware attached. | ||
| // This mirrors the real-world `createAuthServerFn` pattern. | ||
| export const createAuthServerFn = createServerFn().middleware([authMiddleware]) |
13 changes: 13 additions & 0 deletions
13
e2e/react-start/import-protection/src/violations/cross-boundary-safe/session-util.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { getRequest } from '@tanstack/react-start/server' | ||
|
|
||
| // This utility wraps a denied server import but does NOT contain any | ||
| // compiler boundaries. All consumers use it ONLY inside compiler | ||
| // boundaries (createServerFn().handler, createMiddleware().server, etc.) | ||
| // so the compiler should prune this import from the client bundle. | ||
| // | ||
| // This mirrors the real-world pattern of a session utility that wraps | ||
| // `useSession` from `@tanstack/react-start/server`. | ||
| export function getSessionData() { | ||
| const req = getRequest() | ||
| return { method: req.method } | ||
| } |
18 changes: 18 additions & 0 deletions
18
e2e/react-start/import-protection/src/violations/cross-boundary-safe/usage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { createServerFn } from '@tanstack/react-start' | ||
| import { getSessionData } from './session-util' | ||
| import { createAuthServerFn } from './auth-wrapper' | ||
|
|
||
| // Pattern 1: Direct use of session utility inside a server fn handler. | ||
| // This mirrors login.tsx importing useAppSession and using it in | ||
| // createServerFn().handler(). | ||
| export const crossBoundarySafeServerFn = createServerFn().handler(async () => { | ||
| return getSessionData() | ||
| }) | ||
|
|
||
| // Pattern 2: Using the pre-configured server fn from auth-wrapper. | ||
| // This mirrors user.tsx importing createAuthServerFn().handler(). | ||
| export const crossBoundarySafeWithAuth = createAuthServerFn().handler( | ||
| async () => { | ||
| return { ok: true } | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/router
Length of output: 2594
🏁 Script executed:
Repository: TanStack/router
Length of output: 884
🏁 Script executed:
Repository: TanStack/router
Length of output: 1400
🏁 Script executed:
Repository: TanStack/router
Length of output: 547
🏁 Script executed:
Repository: TanStack/router
Length of output: 1453
🏁 Script executed:
Repository: TanStack/router
Length of output: 451
🏁 Script executed:
Repository: TanStack/router
Length of output: 2190
🏁 Script executed:
Repository: TanStack/router
Length of output: 415
Replace
anyin the catch block with proper type narrowing to match strict mode requirements.With
strict: trueenabled in tsconfig, the catch parameter should useunknownand be narrowed to the appropriate error type. ExecSyncException hasstdoutandstderrasBuffer | string, so they need type checking before use.♻️ Suggested refactor
📝 Committable suggestion
🤖 Prompt for AI Agents