Debugging Compiler Code
Using VS Code
Build the Compiler
First, build TypeScript with source maps:Source maps are enabled by default and allow debugging TypeScript source.
Using Chrome DevTools
Debug with Chrome’s inspector:- Open Chrome and navigate to
chrome://inspect - Click Inspect next to your Node process
- DevTools opens with source maps
- Set breakpoints and step through code
Use
--inspect-brk to break at the first line, or --inspect to break at breakpoints only.Command-Line Debugging
For quick debugging, add console logs:src/compiler/checker.ts
Debugging Tests
Interactive Test Debugging
Run tests with the inspector:--inspect-brk, pausing before execution.
VS Code Test Debugging
The provided launch configuration enables debugging test files:Launch Configuration Details
.vscode/launch.json
--no-timeouts- Prevents tests from timing out during debugging-f ${fileBasenameNoExtension}- Filters to current file namesmartStep: true- Steps over uninteresting codepreLaunchTask- Rebuilds tests before each debug session
Debugging Language Service
Debugging tsserver
The TypeScript language server runs as a separate process in editors. Here’s how to debug it:- VS Code
- Using Custom tsserver
- Standalone tsserver
Language Service Features
Debug specific language service operations:Debugging Techniques
Conditional Breakpoints
In VS Code, right-click a breakpoint and set a condition:Call Stack Navigation
The TypeScript compiler has deep call stacks. Use these techniques:- Smart Step - Skip getter/setter properties
- Step Into Target - Choose which function to step into
- Restart Frame - Rerun a function from the start
Watching Expressions
Add watches for common TypeScript objects:Debug Output
Enable detailed compiler output:Performance Debugging
Tracing Compiler Performance
Generate performance traces:- Open
chrome://tracing - Load the generated JSON file
- Analyze compilation time
Extended Diagnostics
Show detailed timing information:- Files read
- Parse time
- Bind time
- Check time
- Emit time
- I/O operations
Profiling with Node
Generate CPU profiles:Memory Debugging
Heap Snapshots
Capture memory usage:- Go to Memory tab
- Take heap snapshot
- Analyze object retention
Memory Leaks
Increase memory limit for debugging:Tracking References
Use WeakMaps and WeakSets to track object relationships without preventing garbage collection.Common Debugging Scenarios
Debugging Type Checking
Debugging Type Checking
Set breakpoints in:
src/compiler/checker.ts- Main type checkergetTypeOfSymbol()- Symbol type resolutioncheckExpression()- Expression type checkingcheckSourceFile()- File-level checking
Debugging Parsing
Debugging Parsing
Set breakpoints in:
src/compiler/parser.ts- Main parserparseSourceFile()- Entry pointparseStatement()- Statement parsingparseExpression()- Expression parsing
Debugging Emit
Debugging Emit
Set breakpoints in:
src/compiler/emitter.ts- JavaScript emittersrc/compiler/declarationEmitter.ts- .d.ts emitteremitSourceFile()- File emit entryemit()- Node emit dispatcher
Debugging Module Resolution
Debugging Module Resolution
Enable trace output:Set breakpoints in:
src/compiler/moduleNameResolver.tsresolveModuleName()loadModuleFromFile()
Debugging Transformations
Debugging Transformations
Set breakpoints in
src/compiler/transformers/:ts.ts- TypeScript-specific transformses2015.ts- ES2015 transformsesnext.ts- ESNext transformsjsx.ts- JSX transforms
Debugging Tools
Built-in Utilities
TypeScript provides debug helpers:Stack Trace Limits
Increase stack trace depth:Source Maps
Verify source maps are working:Remote Debugging
Debug TypeScript running in a remote environment:Troubleshooting Debugging
Breakpoints not hit
Breakpoints not hit
Verify:
- Source maps are enabled
- Files are built (
hereby local) - Breakpoint is in reachable code
- Using correct Node version (>= 14.17)
Can't see TypeScript source
Can't see TypeScript source
Source maps may be missing or incorrect:Use unbundled mode for better debugging.
Debugger is too slow
Debugger is too slow
- Disable all breakpoints except critical ones
- Use conditional breakpoints
- Use logpoints instead of breakpoints
- Disable source map stepping in non-TypeScript code
Variables show '[Function]' in debugger
Variables show '[Function]' in debugger
Use console or watch expressions:
Resources
VS Code Debugging
Official VS Code debugging guide
Node.js Debugging
Node.js debugging documentation
Chrome DevTools
Chrome DevTools reference
TypeScript Compiler Notes
Architectural documentation
Next Steps
Testing
Learn how to write and run tests
Building
Build system reference