@ranimontagna/agent-toolkit 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,513 @@
1
+ # Debugging Guide
2
+
3
+ Practical debugging strategies for common issues in Astro development.
4
+
5
+ ## Quick Debugging Decision Tree
6
+
7
+ **What's failing?** → **Debug with:**
8
+
9
+ | Symptom | First Check | Debug Command | Section |
10
+ | ---------------- | ---------------- | -------------------------------------------- | ----------------------------------------- |
11
+ | Build fails | Astro build logs | `DEBUG=astro:* pnpm -C packages/astro build` | [Build Issues](#debugging-build-failures) |
12
+ | Dev server crash | Core logs | `DEBUG=astro:* astro dev` | [Core Issues](#debugging-core-nodejs) |
13
+ | HMR not working | Browser network | `agent-browser` (not curl) | [HMR Issues](#debugging-hmr) |
14
+ | SSR fails | Runtime context | `DEBUG=astro:* astro dev` | [SSR Issues](#debugging-ssr-issues) |
15
+ | Content missing | Data store | `cat .astro/data-store.json` | [Content](#debugging-content-collections) |
16
+ | Tests failing | Fixture setup | Check `outDir` uniqueness | [Tests](testing.md) |
17
+
18
+ ## Debugging Approaches
19
+
20
+ ### 1. Use DEBUG Environment Variable (Recommended)
21
+
22
+ **Most issues are in Astro's code, not Vite.** Use Astro-specific debugging first.
23
+
24
+ ```bash
25
+ # Debug everything in Astro
26
+ DEBUG=astro:* astro dev
27
+ DEBUG=astro:* astro build
28
+
29
+ # Debug specific subsystems
30
+ DEBUG=astro:build astro build # Build process
31
+ DEBUG=astro:content astro dev # Content collections
32
+ DEBUG=astro:server astro dev # Dev server
33
+ DEBUG=astro:render astro dev # Page rendering
34
+ DEBUG=astro:config astro dev # Configuration
35
+
36
+ # Combine multiple namespaces
37
+ DEBUG=astro:build,astro:config astro build
38
+ DEBUG=astro:render,astro:server astro dev
39
+ ```
40
+
41
+ ### 2. Add Direct Logging
42
+
43
+ **Fastest approach**: Add console.log directly in source files.
44
+
45
+ ```typescript
46
+ // Pattern: Add logs with context prefix
47
+ console.log('[CONTEXT] Message:', data);
48
+
49
+ // Example
50
+ console.log('[BUILD] Processing routes:', routes.length);
51
+ console.log('[RENDER] Component:', component.name);
52
+ console.log('[CONTENT] Collections:', collections);
53
+ ```
54
+
55
+ **Workflow:**
56
+
57
+ 1. Add logs to relevant file (see [Strategic Logging Locations](#strategic-logging-locations))
58
+ 2. Run `pnpm -C packages/astro build` to rebuild
59
+ 3. Test your change
60
+
61
+ **Use Astro's debug logger** (optional):
62
+
63
+ ```typescript
64
+ import { debug } from '../logger/core.js';
65
+ const logger = debug('astro:feature-name');
66
+ logger('Operation starting', { data });
67
+ ```
68
+
69
+ ### 3. Node Inspector (Advanced)
70
+
71
+ ```bash
72
+ # Start with debugger
73
+ node --inspect node_modules/.bin/astro dev
74
+ node --inspect node_modules/.bin/astro build
75
+
76
+ # Connect with Chrome DevTools
77
+ # Open chrome://inspect in Chrome
78
+ # Click "inspect" on the Node.js process
79
+ ```
80
+
81
+ **Set breakpoints** in Chrome DevTools, navigate to source files, click line numbers.
82
+
83
+ ## Strategic Logging Locations
84
+
85
+ **Where to add logs based on issue type:**
86
+
87
+ | Issue Type | File Location | Context |
88
+ | ----------------- | ---------------------------------------------------- | ----------- |
89
+ | Build fails | `packages/astro/src/core/build/index.ts` | Core |
90
+ | Routes not found | `packages/astro/src/core/routing/manifest/create.ts` | Core |
91
+ | Content missing | `packages/astro/src/content/content-layer.ts` | Core |
92
+ | Rendering errors | `packages/astro/src/core/render/core.ts` | Runtime |
93
+ | Config issues | `packages/astro/src/core/config/config.ts` | Core |
94
+ | Dev server issues | `packages/astro/src/core/dev/dev.ts` | Core |
95
+ | Component compile | `packages/astro/src/vite-plugin-astro/index.ts` | Vite |
96
+ | Virtual modules | `packages/astro/src/vite-plugin-*/` | Vite |
97
+ | Middleware issues | `packages/astro/src/core/middleware/` | Core |
98
+ | Adapter issues | Check specific adapter in `packages/integrations/` | Integration |
99
+
100
+ ## Debugging Core (Node.js)
101
+
102
+ Core code runs in Node.js context: `packages/astro/src/core/`
103
+
104
+ **This is where most Astro bugs live.**
105
+
106
+ ### Build Pipeline Flow
107
+
108
+ **Entry point**: `packages/astro/src/core/build/index.ts`
109
+
110
+ **Flow:**
111
+
112
+ 1. `build()` → Main entry
113
+ 2. `staticBuild()` or `viteBuild()` → Build strategy
114
+ 3. Build plugins execute in order (see below)
115
+ 4. Assets emitted to `dist/`
116
+
117
+ **Build plugin order** (`packages/astro/src/core/build/plugins/README.md`):
118
+
119
+ 1. middleware
120
+ 2. renderers
121
+ 3. pages
122
+ 4. ssr
123
+ 5. manifest
124
+
125
+ **Add tracing logs** to understand flow:
126
+
127
+ ```typescript
128
+ // In build/index.ts
129
+ console.log('[1] build() entry');
130
+ console.log('[2] Settings created');
131
+ console.log('[3] Build complete');
132
+ ```
133
+
134
+ ### Component Identification
135
+
136
+ **Astro Vite plugins**: `packages/astro/src/vite-plugin-*/`
137
+
138
+ - `vite-plugin-astro` → `.astro` file compilation
139
+ - `vite-plugin-astro-server` → Dev server integration
140
+ - `vite-plugin-env` → Environment variables
141
+ - `vite-plugin-html` → HTML injection
142
+
143
+ **Build plugins**: `packages/astro/src/core/build/plugins/`
144
+
145
+ - `plugin-middleware.ts` → Middleware emission
146
+ - `plugin-renderers.ts` → Renderer collection
147
+ - `plugin-pages.ts` → Page virtual modules
148
+ - `plugin-ssr.ts` → SSR entry points
149
+ - `plugin-manifest.ts` → Manifest generation
150
+
151
+ ## Debugging SSR Issues
152
+
153
+ SSR issues span multiple contexts. Identify context first.
154
+
155
+ ### Context Identification
156
+
157
+ **Determine which context the issue occurs in:**
158
+
159
+ 1. Does issue occur in `astro dev`? → Dev/render context
160
+ 2. Does issue occur after `astro build`? → Build context
161
+ 3. Does issue occur in `astro preview`? → Runtime/adapter context
162
+
163
+ See [architecture.md](architecture.md) for pipeline details.
164
+
165
+ ### Debug by Context
166
+
167
+ **Dev SSR:**
168
+
169
+ - Location: `packages/astro/src/core/render/`
170
+ - Command: `DEBUG=astro:render,astro:server astro dev`
171
+ - Check: Components loading, middleware executing, virtual modules available
172
+
173
+ **Build SSR:**
174
+
175
+ - Location: `packages/astro/src/core/build/`
176
+ - Command: `DEBUG=astro:build astro build`
177
+ - Check: `dist/` structure, hashed chunks in `dist/server/chunks/`
178
+
179
+ **Runtime SSR:**
180
+
181
+ - Location: `packages/astro/src/core/app/`
182
+ - Command: `astro preview`
183
+ - Check: Adapter implementation, middleware presence, routing, environment variables
184
+
185
+ ### Inspect Build Output
186
+
187
+ ```bash
188
+ # Inspect dist/ structure
189
+ ls -laR dist/
190
+
191
+ # SSR build structure (varies by adapter pattern):
192
+ # dist/client/ → Client assets (hashed)
193
+ # dist/server/chunks/ → All server code (hashed files)
194
+ # dist/server/virtual_astro_middleware.mjs → Middleware
195
+ # dist/server/[entrypoint] → Entry point (filename depends on adapter)
196
+
197
+ # Legacy adapters: Use entry.mjs
198
+ # Self adapters: Adapter decides filename (e.g., custom.mjs, _render.mjs)
199
+ ```
200
+
201
+ **Adapter patterns:**
202
+
203
+ - **Legacy** (`adapter.entrypointResolution = 'explicit'`): Always uses `entry.mjs`
204
+ - **Self** (`adapter.entrypointResolution = 'self'`): Adapter controls entrypoint filename
205
+
206
+ **Find entrypoint:**
207
+
208
+ ```bash
209
+ # List server files (entrypoint is typically at top level)
210
+ ls dist/server/*.mjs
211
+
212
+ # Check entrypoint content (always a re-export to chunks)
213
+ cat dist/server/entry.mjs # or whatever the adapter named it
214
+ ```
215
+
216
+ **Find actual code:**
217
+
218
+ ```bash
219
+ # All server code is in hashed chunks
220
+ ls dist/server/chunks/
221
+
222
+ # Search for specific code in chunks
223
+ grep -r "function.*render" dist/server/chunks/
224
+ ```
225
+
226
+ ## Debugging Virtual Modules
227
+
228
+ Virtual modules use `virtual:astro:*` prefix.
229
+
230
+ ### Common Virtual Modules
231
+
232
+ - `virtual:astro:manifest` → Manifest data
233
+ - `virtual:astro:routes` → Route definitions
234
+ - `virtual:astro:middleware` → Middleware module
235
+ - `virtual:astro:renderers` → Framework renderers
236
+
237
+ ### Debug Virtual Module Generation
238
+
239
+ Add logging to Vite plugin hooks:
240
+
241
+ ```typescript
242
+ // In Vite plugin
243
+ {
244
+ resolveId: {
245
+ handler(id) {
246
+ if (id.includes('virtual:astro')) {
247
+ console.log('[VIRTUAL] Resolving:', id);
248
+ }
249
+ // ...
250
+ }
251
+ },
252
+ load: {
253
+ handler(id) {
254
+ if (id.includes('\0virtual:astro')) {
255
+ console.log('[VIRTUAL] Loading:', id);
256
+ const code = generateCode();
257
+ console.log('[VIRTUAL] Generated code:', code);
258
+ return { code };
259
+ }
260
+ }
261
+ }
262
+ }
263
+ ```
264
+
265
+ ### Inspect at Runtime
266
+
267
+ ```bash
268
+ # See loaded virtual modules
269
+ DEBUG=astro:* astro dev 2>&1 | grep "virtual:astro"
270
+ ```
271
+
272
+ ## Debugging Content Collections
273
+
274
+ Content layer issues often relate to data store or type generation.
275
+
276
+ ### Inspect Data Store
277
+
278
+ ```bash
279
+ # View entire data store
280
+ cat .astro/data-store.json | jq
281
+
282
+ # Check specific collection
283
+ cat .astro/data-store.json | jq '.collections["blog"]'
284
+
285
+ # Count entries per collection
286
+ cat .astro/data-store.json | jq '.collections | to_entries | map({key: .key, count: .value.entries | length})'
287
+ ```
288
+
289
+ ### Debug Content Layer
290
+
291
+ **Location**: `packages/astro/src/content/content-layer.ts`
292
+
293
+ **Enable debugging:**
294
+
295
+ ```bash
296
+ DEBUG=astro:content astro dev
297
+ DEBUG=astro:content astro build
298
+ ```
299
+
300
+ ### Check Type Generation
301
+
302
+ **Location**: `.astro/types.d.ts`
303
+
304
+ ```bash
305
+ # View generated types
306
+ cat .astro/types.d.ts | grep -A 20 "declare module 'astro:content'"
307
+ ```
308
+
309
+ ### Debug Loaders
310
+
311
+ Add logging in your loader implementation:
312
+
313
+ ```typescript
314
+ export function myLoader() {
315
+ return {
316
+ name: 'my-loader',
317
+ async load({ store, logger }) {
318
+ logger.info('Loading data...');
319
+ const data = await fetchData();
320
+ logger.info(`Loaded ${data.length} entries`);
321
+
322
+ for (const entry of data) {
323
+ console.log('[LOADER] Setting:', entry.id);
324
+ store.set({ id: entry.id, data: entry });
325
+ }
326
+ },
327
+ };
328
+ }
329
+ ```
330
+
331
+ ## Debugging HMR
332
+
333
+ HMR testing requires a browser. **Do not use `curl`** for HMR issues.
334
+
335
+ ### Use agent-browser
336
+
337
+ ```bash
338
+ # Start dev server in background
339
+ pnpm exec bgproc start -n devserver --wait-for-port 10 -- pnpm -C examples/minimal dev
340
+
341
+ # Open browser
342
+ agent-browser open http://localhost:4321
343
+
344
+ # Get snapshot
345
+ agent-browser snapshot -i
346
+
347
+ # Make changes to source files
348
+ # Verify HMR updates the page
349
+
350
+ # View logs
351
+ pnpm exec bgproc logs -n devserver
352
+
353
+ # Cleanup
354
+ pnpm exec bgproc stop -n devserver
355
+ ```
356
+
357
+ ### Check HMR Boundaries
358
+
359
+ Vite maintains HMR boundaries. If HMR isn't working, check module boundaries.
360
+
361
+ ```bash
362
+ DEBUG=vite:hmr astro dev
363
+ ```
364
+
365
+ **Look for:**
366
+
367
+ - "hmr update" messages
368
+ - Module invalidation chains
369
+ - Boundary violations
370
+
371
+ ### Common HMR Issues
372
+
373
+ | Issue | Cause | Fix |
374
+ | ---------------------- | ------------------- | ----------------------- |
375
+ | Full page reload | No HMR boundary | Add HMR accept |
376
+ | Styles not updating | CSS module cache | Check Vite CSS handling |
377
+ | Component not updating | Module not in graph | Check import chain |
378
+
379
+ ## Debugging Build Failures
380
+
381
+ ### Check Build Output
382
+
383
+ ```bash
384
+ # Build with full output
385
+ astro build
386
+
387
+ # Inspect dist/ structure
388
+ ls -laR dist/
389
+
390
+ # SSR build structure:
391
+ # dist/client/ → Client assets (hashed)
392
+ # dist/server/[entrypoint] → Entry shim (filename varies by adapter)
393
+ # dist/server/chunks/ → All server code (hashed files)
394
+
395
+ # Find entrypoint (adapter-dependent filename)
396
+ ls dist/server/*.mjs
397
+
398
+ # Check entry point (always a re-export to chunks)
399
+ cat dist/server/entry.mjs # or whatever filename adapter uses
400
+
401
+ # All actual code is in hashed chunks
402
+ ls dist/server/chunks/
403
+ ```
404
+
405
+ ### Build Plugin Execution
406
+
407
+ **Order matters.** Plugins execute sequentially.
408
+
409
+ ```bash
410
+ # Check plugin execution
411
+ DEBUG=vite:* astro build 2>&1 | grep "plugin-"
412
+ ```
413
+
414
+ ### Asset Processing
415
+
416
+ **Check:**
417
+
418
+ - `dist/client/` → Client assets
419
+ - `dist/server/` → SSR code
420
+ - Image optimization
421
+ - CSS bundling
422
+
423
+ ```bash
424
+ # Find asset references
425
+ find dist/ -name "*.html" -exec grep -l "asset-file.jpg" {} \;
426
+ ```
427
+
428
+ ## Debugging Test Failures
429
+
430
+ See [testing.md](testing.md) for comprehensive test debugging.
431
+
432
+ **Quick checks:**
433
+
434
+ 1. **Unique outDir**: Each test must have unique output directory
435
+ 2. **Fixture structure**: Verify package.json has workspace dependencies
436
+ 3. **Build cache**: Clean `.astro/` and `dist/` in fixture
437
+ 4. **Parallel execution**: Check if `--parallel` is causing issues
438
+
439
+ ## Common Error Patterns
440
+
441
+ ### "Cannot find module 'node:fs'"
442
+
443
+ **Cause**: Using Node.js API in `runtime/` code
444
+
445
+ **Fix**: Move code to `core/` or use `@astrojs/internal-helpers`
446
+
447
+ **See**: [constraints.md](constraints.md)
448
+
449
+ ### "Virtual module not found"
450
+
451
+ **Cause**: Virtual module not registered or plugin not loaded
452
+
453
+ **Fix:**
454
+
455
+ 1. Check plugin registration
456
+ 2. Verify `resolveId` and `load` hooks use filter/handler pattern
457
+ 3. Confirm virtual module prefix is `virtual:astro:*`
458
+
459
+ ### "Test fails intermittently"
460
+
461
+ **Cause**: Shared `outDir` between tests causing cache pollution
462
+
463
+ **Fix**: Set unique `outDir` for each test fixture
464
+
465
+ **See**: [testing.md](testing.md)
466
+
467
+ ### "Port already in use"
468
+
469
+ **Cause**: Previous dev server still running
470
+
471
+ **Fix:**
472
+
473
+ ```bash
474
+ # List running processes
475
+ pnpm exec bgproc list
476
+
477
+ # Stop specific server
478
+ pnpm exec bgproc stop -n devserver
479
+
480
+ # Kill all node processes (nuclear option)
481
+ killall node
482
+ ```
483
+
484
+ ### "HMR not working"
485
+
486
+ **Cause**: Module boundaries, full reload triggered, or browser cache
487
+
488
+ **Fix:**
489
+
490
+ 1. Use `agent-browser` (not curl)
491
+ 2. Check HMR boundaries with `DEBUG=vite:hmr`
492
+ 3. Clear browser cache
493
+ 4. Check HMR accept statements in modules
494
+
495
+ ## Debugging Checklist
496
+
497
+ Before asking for help or filing an issue:
498
+
499
+ - [ ] Read error message completely
500
+ - [ ] Identified execution context (core/runtime/client)
501
+ - [ ] Enabled appropriate DEBUG flags
502
+ - [ ] Verified with minimal reproduction
503
+ - [ ] Checked if issue exists in `examples/`
504
+ - [ ] Reviewed related documentation
505
+ - [ ] Searched existing issues on GitHub
506
+ - [ ] Isolated to specific component/plugin
507
+
508
+ ## Further Reading
509
+
510
+ - Architecture: [architecture.md](architecture.md)
511
+ - Constraints: [constraints.md](constraints.md)
512
+ - Testing: [testing.md](testing.md)
513
+ - Vite debugging: https://vitejs.dev/guide/troubleshooting.html