Skip to content

Commit 815cdf6

Browse files
authored
Merge pull request #206 from udecode/codex/fix-start-init-bunx-parse
2 parents 0931185 + 7edbb5e commit 815cdf6

6 files changed

Lines changed: 102 additions & 7 deletions

File tree

.changeset/ten-bees-swim.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"kitcn": patch
3+
---
4+
5+
## Patches
6+
7+
- Fix `bunx --bun kitcn init -t start --yes` so Bun-native parse-time imports
8+
no longer bypass project aliases and crash first-run codegen on scaffolded
9+
Start files.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Task
2+
3+
Fix the remaining published-package `bunx --bun kitcn@latest init -t start --yes`
4+
bootstrap parse failure where scaffolded `messages.ts`/`http.ts` still resolve
5+
through Bun's install cache and crash on `convex/server`, while `kitcn dev`
6+
already works.
7+
8+
## Source Of Truth
9+
10+
- User repro from published `kitcn@latest`
11+
- `docs/solutions/integration-issues/bunx-kitcn-self-resolution-must-not-break-scaffold-codegen-20260407.md`
12+
- Current parser + init scaffolding in `packages/kitcn/src/cli/**`
13+
14+
## Likely Seam
15+
16+
- `packages/kitcn/src/cli/utils/project-jiti.ts`
17+
- `packages/kitcn/src/cli/codegen.ts`
18+
- Published/package-intent regression coverage if the bug only reproduces from
19+
the packed artifact path
20+
21+
## Plan
22+
23+
1. Reproduce against a real temp app with published `kitcn`.
24+
2. Compare the real start scaffold import graph vs existing codegen regressions.
25+
3. Add a failing targeted regression at the parser/package seam.
26+
4. Fix the import-resolution seam, not each scaffold file.
27+
5. Verify with targeted tests, package build, and the real published-style path.
28+
29+
## Completion Checks
30+
31+
- `typecheck` if changed `.ts` files require it
32+
- `lint:fix`
33+
- targeted tests for the failing seam
34+
- `bun --cwd packages/kitcn build`
35+
- update active unreleased changeset if package code changes
36+
- evaluate `ce-compound` only after the fix is verified

docs/solutions/integration-issues/bunx-kitcn-self-resolution-must-not-break-scaffold-codegen-20260407.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: bunx kitcn self-resolution must not break scaffold codegen
33
date: 2026-04-07
4-
last_updated: 2026-04-07
4+
last_updated: 2026-04-11
55
category: integration-issues
66
module: cli-codegen
77
problem_type: integration_issue
@@ -49,6 +49,8 @@ The failing files were the stock scaffold backend demo files, not user code.
4949
- relying on source-level cache-path tests alone; the published bundled
5050
`backend-core` artifact can still miss a direct-import rewrite that never
5151
shows up in the raw TypeScript path
52+
- relying on entry-file rewrites alone; Bun-native imports can bypass Jiti
53+
aliases for transitive files like the generated server placeholder
5254
- treating local deployment detection as only `local:*` or `anonymous:*`; plain
5355
`anonymous-agent` now shows up too
5456

@@ -59,10 +61,12 @@ Patch the loader seam, not each scaffold file:
5961
1. create a project-aware `jiti` helper for CLI parsing
6062
2. alias `kitcn/server` to a tiny parser shim so scaffold parsing does not need
6163
the real runtime package graph during bootstrap
62-
3. rewrite direct `from "kitcn/server"` imports to the project shim path before
64+
3. force `tryNative: false` so Bun never takes over parse-time project imports
65+
and skips the Jiti alias table for transitive files
66+
4. rewrite direct `from "kitcn/server"` imports to the project shim path before
6367
the bundled parser imports the module
64-
4. keep local `convex` export aliases so project-local resolution still works
65-
5. treat plain `anonymous-agent` as a local deployment and preserve
68+
5. keep local `convex` export aliases so project-local resolution still works
69+
6. treat plain `anonymous-agent` as a local deployment and preserve
6670
`CONVEX_AGENT_MODE=anonymous` in `dev`
6771

6872
## Why This Works
@@ -77,9 +81,15 @@ install-cache copy of `kitcn`, which then re-entered `dist/api-entry-*.js` and
7781
crashed on `convex/server` even though the generated app had already installed
7882
the right dependencies.
7983

80-
Rewriting direct `kitcn/server` imports to the absolute project shim path
81-
removes bare-specifier resolution from that parse step, so the bundled parser
82-
never falls back to the Bun cache copy.
84+
The missing piece was Bun-native import. `createProjectJiti` still allowed
85+
`tryNative`, so the entry module could be rewritten but transitive files were
86+
still imported by Bun directly. Once Bun owned that transitive import chain,
87+
Jiti aliases no longer applied, and the generated server placeholder could fall
88+
back to the packaged `kitcn/server` bundle in Bun's temp install cache.
89+
90+
Turning native import off keeps parse-time project modules inside Jiti end to
91+
end. That makes the project shim and local package aliases apply to the whole
92+
import graph instead of only the root file.
8393

8494
The `anonymous-agent` follow-up bug was separate but adjacent: local deployment
8595
classification missed the plain `anonymous-agent` value, so `dev` failed to
@@ -89,8 +99,12 @@ carry the anonymous mode back into Convex subprocesses.
8999

90100
- When a CLI package parses scaffolded project files during bootstrap, do not
91101
assume package self-resolution points at the new app install
102+
- Do not rely on Bun-native import for parse-time project modules when aliasing
103+
or shim rewrites are part of the contract
92104
- For Bun-specific bootstrap bugs, verify both the source path and the packed
93105
artifact path; source tests alone can miss a bundled regression
106+
- Lock the Jiti helper itself so packed builds cannot silently re-enable native
107+
import
94108
- Add a packed-artifact regression whenever the real bug only reproduces from
95109
`bunx` or another published-package entry point
96110
- Treat local deployment-name formats as compatibility inputs; upstream CLI
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, test } from 'bun:test';
2+
import fs from 'node:fs';
3+
import os from 'node:os';
4+
import path from 'node:path';
5+
import { createProjectJiti } from './project-jiti';
6+
7+
describe('cli/utils/project-jiti', () => {
8+
test('forces tryNative off for project parse-time imports', () => {
9+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'kitcn-project-jiti-'));
10+
const previous = process.env.JITI_TRY_NATIVE;
11+
12+
fs.writeFileSync(
13+
path.join(dir, 'package.json'),
14+
JSON.stringify({ name: 'project-jiti-test', private: true }, null, 2)
15+
);
16+
17+
try {
18+
process.env.JITI_TRY_NATIVE = 'true';
19+
20+
const jiti = createProjectJiti(dir);
21+
22+
expect(jiti.options.tryNative).toBe(false);
23+
} finally {
24+
if (previous === undefined) {
25+
process.env.JITI_TRY_NATIVE = undefined;
26+
} else {
27+
process.env.JITI_TRY_NATIVE = previous;
28+
}
29+
fs.rmSync(dir, { force: true, recursive: true });
30+
}
31+
});
32+
});

packages/kitcn/src/cli/utils/project-jiti.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ export const createProjectJiti = (cwd = process.cwd()) =>
260260
runtime: 'automatic',
261261
},
262262
moduleCache: false,
263+
// Bun-native import bypasses Jiti aliasing for transitive project files.
264+
// Parse-time CLI imports need Jiti in the loop end-to-end.
265+
tryNative: false,
263266
alias: {
264267
...buildTsconfigPathAliases(cwd),
265268
...buildLocalPackageExportAliases(cwd, 'kitcn'),

packages/kitcn/src/package-intent.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ describe('package intent metadata', () => {
302302
expect(backendCoreSource).toContain('getProjectServerParserShimPath');
303303
expect(backendCoreSource).toContain('kitcn-parse.ts');
304304
expect(backendCoreSource).toContain('kitcn/server');
305+
expect(backendCoreSource).toContain('tryNative: false');
305306
} finally {
306307
rmSync(packDir, { force: true, recursive: true });
307308
}

0 commit comments

Comments
 (0)