debtmap 0.16.3

Code complexity and technical debt analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Error Messages Reference

Understanding common error messages, error codes, and how to resolve them.

## Error Code System

Debtmap uses structured error codes for programmatic handling and documentation lookup. Error codes are assigned by category (from `src/debtmap_error.rs:62-116`):

| Code Range | Category | Description |
|------------|----------|-------------|
| E001-E009 | I/O | File system and I/O errors |
| E010-E019 | Parse | Source code parsing errors |
| E020-E029 | Config | Configuration file errors |
| E030-E039 | Analysis | Analysis algorithm errors |
| E040-E049 | CLI | Command-line argument errors |
| E050-E059 | Validation | Input validation errors |

### I/O Error Codes (E001-E009)

| Code | Name | Description |
|------|------|-------------|
| E001 | `IO_FILE_NOT_FOUND` | File or directory does not exist |
| E002 | `IO_PERMISSION_DENIED` | Insufficient permissions to read file |
| E003 | `IO_RESOURCE_BUSY` | File is locked or resource temporarily unavailable |
| E009 | `IO_GENERIC` | Other I/O errors |

### Parse Error Codes (E010-E019)

| Code | Name | Description |
|------|------|-------------|
| E010 | `PARSE_SYNTAX` | Syntax error in source file |
| E011 | `PARSE_UNSUPPORTED` | Unsupported language or feature |
| E012 | `PARSE_ENCODING` | Invalid file encoding |
| E019 | `PARSE_GENERIC` | Other parse errors |

### Configuration Error Codes (E020-E029)

| Code | Name | Description |
|------|------|-------------|
| E020 | `CONFIG_INVALID` | Invalid configuration value |
| E021 | `CONFIG_MISSING` | Missing required configuration field |
| E022 | `CONFIG_FILE_NOT_FOUND` | Configuration file not found |
| E029 | `CONFIG_GENERIC` | Other configuration errors |

### Analysis Error Codes (E030-E039)

| Code | Name | Description |
|------|------|-------------|
| E030 | `ANALYSIS_COMPLEXITY` | Complexity calculation failed |
| E031 | `ANALYSIS_COVERAGE` | Coverage data loading failed |
| E032 | `ANALYSIS_SCORING` | Debt scoring calculation failed |
| E039 | `ANALYSIS_GENERIC` | Other analysis errors |

### CLI Error Codes (E040-E049)

| Code | Name | Description |
|------|------|-------------|
| E040 | `CLI_INVALID_COMMAND` | Unknown or invalid command |
| E041 | `CLI_MISSING_ARG` | Required argument not provided |
| E042 | `CLI_INVALID_ARG` | Invalid argument value |
| E049 | `CLI_GENERIC` | Other CLI errors |

### Validation Error Codes (E050-E059)

| Code | Name | Description |
|------|------|-------------|
| E050 | `VALIDATION_GENERIC` | General validation failure |
| E051 | `VALIDATION_THRESHOLD` | Threshold validation exceeded |
| E052 | `VALIDATION_CONSTRAINT` | Constraint violation |

## Error Classification

Debtmap classifies errors to help determine appropriate action (from `src/debtmap_error.rs:466-519`):

### Retryable vs Non-Retryable Errors

**Retryable errors** may succeed on a subsequent attempt:
- Resource busy / file locks (`E003`)
- Network timeouts
- Connection issues
- "Temporarily unavailable" errors

**Non-retryable errors** require user intervention:
- Parse/syntax errors
- Configuration errors
- File not found (permanent)
- Validation errors

### User-Fixable vs System Errors

**User-fixable errors** can be resolved by modifying input:
- Configuration errors (fix config file)
- CLI errors (fix command arguments)
- Validation errors (fix input values)
- Parse errors (fix source code syntax)

**System errors** are outside user control:
- I/O errors (system issues)
- Analysis errors (internal algorithm issues)

## Exit Codes

Debtmap returns specific exit codes for CI/CD integration (from `src/debtmap_error.rs:521-532`):

| Exit Code | Meaning | When Returned |
|-----------|---------|---------------|
| 0 | Success | Analysis completed successfully |
| 1 | Analysis/I/O Error | Analysis failed or I/O error occurred |
| 2 | CLI Error | Invalid command-line usage |
| 3 | Config Error | Configuration file or value error |
| 4 | Validation Error | Validation threshold exceeded |
| 5 | Parse Error | Source code parsing failed |

**CI/CD Integration Example:**

```bash
#!/bin/bash
debtmap analyze .
EXIT_CODE=$?

case $EXIT_CODE in
  0)
    echo "Analysis passed"
    ;;
  1)
    echo "Analysis or I/O error - check logs"
    exit 1
    ;;
  2)
    echo "CLI usage error - check command syntax"
    exit 1
    ;;
  3)
    echo "Configuration error - check .debtmap.toml"
    exit 1
    ;;
  4)
    echo "Validation failed - thresholds exceeded"
    exit 1
    ;;
  5)
    echo "Parse error - source code issues"
    exit 1
    ;;
esac
```

## File System Errors

**Message**: `[E001] I/O error: No such file or directory`

**Meaning**: File or directory does not exist

**Solutions**:
- Verify path is correct: `ls -la <path>`
- Check current working directory: `pwd`
- Use absolute paths if needed

---

**Message**: `[E002] I/O error: Permission denied`

**Meaning**: Cannot read file or directory due to permissions

**Solutions**:
- Check file permissions: `ls -la <file>`
- Ensure user has read access
- Run from appropriate directory

---

**Message**: `[E003] I/O error: Resource busy`

**Meaning**: File is locked or temporarily unavailable (retryable)

**Solutions**:
- Wait and retry the analysis
- Close other programs using the file
- Check for file locks

## Parse Errors

**Message**: `[E010] Parse error in file.rs: unexpected token at line 42, column 10`

**Meaning**: Syntax that debtmap cannot parse

**Solutions**:
```bash
# Try fallback mode without semantic analysis
debtmap analyze . --semantic-off

# For Rust macro issues, enable verbose warnings
debtmap analyze . --verbose-macro-warnings --show-macro-stats
```

---

**Message**: `[E011] Parse error: unsupported language`

**Meaning**: The file type is not supported or has unsupported features

**Solutions**:
- Check that the file is a supported language (Rust, Python, JavaScript, TypeScript)
- For Rust, complex procedural macros may require `--semantic-off`

## Configuration Errors

**Message**: `[E020] Configuration error: invalid config value`

**Meaning**: Invalid configuration in `.debtmap.toml` or CLI flags

**Configuration File Locations** (checked in order):
1. `.debtmap.toml` in project root (project-level)
2. `~/.config/debtmap/config.toml` (global user config)

**Solutions**:
- Check `.debtmap.toml` syntax with a TOML validator
- Review CLI flag values
- Check for typos in configuration keys

**Example valid configuration:**
```toml
# .debtmap.toml
[thresholds]
complexity = 10
max_file_length = 500

[scoring]
coverage = 0.40
complexity = 0.30
```

---

**Message**: `[E022] Configuration error: file not found`

**Meaning**: Specified configuration file does not exist

**Solutions**:
- Create `.debtmap.toml` from the example: `cp .debtmap.toml.example .debtmap.toml`
- Verify the configuration path if using `--config` flag

## Validation Errors

**Message**: `[E051] Validation error: threshold validation failed`

**Meaning**: Analysis results exceed configured thresholds

**Solutions**:
- Check threshold values in `.debtmap.toml` under `[thresholds.validation]`
- Ensure `--min-priority` is in valid range (0-10)
- Use `--threshold-preset` with a valid preset name

**Validation thresholds example:**
```toml
[thresholds.validation]
max_average_complexity = 10.0
max_high_complexity_count = 100
max_debt_items = 2000
max_total_debt_score = 10000
max_codebase_risk_score = 7.0
```

## Analysis Errors

**Message**: `[E039] Analysis error: internal analysis failure`

**Meaning**: Internal error during analysis phase

**Solutions**:
```bash
# Try fallback mode
debtmap analyze . --semantic-off

# Report with debug info
debtmap analyze . -vvv 2>&1 | tee error.log

# Isolate problem file
debtmap analyze . --max-files 1 path/to/suspected/file
```

---

**Message**: `[E031] Analysis error: Coverage error`

**Meaning**: Failed to load or process coverage data

**Solutions**:
```bash
# Analyze without coverage data
debtmap analyze . --no-coverage

# Check coverage file format
debtmap explain-coverage <coverage-file>

# Verify function name matching
debtmap analyze . -vvv | grep -i coverage
```

## Dependency Errors

**Message**: `Dependency error: cannot resolve dependency graph`

**Meaning**: Cannot build dependency relationships

**Solutions**:
```bash
# Disable dependency provider
debtmap analyze . --context --disable-context dependency

# Try without context
debtmap analyze .
```

## Concurrency Errors

**Message**: `Concurrency error: parallel processing failure`

**Meaning**: Error during parallel execution

**Solutions**:
```bash
# Disable parallel processing
debtmap analyze . --no-parallel

# Reduce thread count
debtmap analyze . --jobs 1
```

## Pattern Errors

**Message**: `Pattern error: invalid glob pattern`

**Meaning**: Invalid glob pattern in configuration or CLI

**Solutions**:
- Check glob pattern syntax
- Escape special characters if needed
- Use simpler patterns or path prefixes

## Handling False Positives

Debtmap includes context-aware false positive reduction, enabled by default. This uses pattern-based classification to reduce spurious debt reports (from `src/cli/args.rs:202-204`).

### Controlling False Positive Reduction

```bash
# Default: context-aware analysis enabled
debtmap analyze .

# Disable context-aware analysis for raw results
debtmap analyze . --no-context-aware
```

### When to Expect False Positives

False positives are more likely with:
- Complex macro-generated code
- Unusual code patterns
- Generated or vendored code
- Test utilities with intentionally complex patterns

**Reducing false positives:**
1. Use context-aware analysis (default)
2. Configure exclusion patterns in `.debtmap.toml`
3. Use `--verbose-macro-warnings` to identify macro issues

## Boilerplate Detection Messages

Debtmap identifies boilerplate patterns that may be better suited for macros or code generation (from `src/organization/boilerplate_detector.rs:33-58`).

### Understanding Boilerplate Reports

**Message**: `Boilerplate detected: high trait implementation density`

**Meaning**: File contains many similar trait implementations with low complexity, suggesting macro-ification would reduce maintenance burden.

**Detection criteria:**
- 20+ impl blocks (configurable via `min_impl_blocks`)
- 70%+ method uniformity across implementations
- Average complexity below 2.0 (simple, repetitive code)
- 70%+ confidence threshold

### Acting on Boilerplate Recommendations

When a file is flagged as boilerplate:

1. **Consider macro extraction**: If implementing the same trait for many types, use a declarative macro
2. **Consider code generation**: For very large patterns, use build.rs or proc-macros
3. **Review the recommendation**: Debtmap provides specific suggestions for each pattern

**Boilerplate is NOT the same as complex code:**
- Complex code (high cyclomatic complexity) → split into modules
- Boilerplate code (low complexity, high repetition) → macro-ify or generate

## Language-Specific Issues

### Rust Macro Handling

Rust macros may produce parse warnings or analysis limitations:

```bash
# Enable verbose macro warnings
debtmap analyze . --verbose-macro-warnings

# Show macro expansion statistics
debtmap analyze . --show-macro-stats

# Both together for full diagnostics
debtmap analyze . --verbose-macro-warnings --show-macro-stats
```

### Language Support Status

| Language | Support Level | Notes |
|----------|--------------|-------|
| Rust | Full | Primary language, best analysis |
| Python | Stub | Basic complexity analysis |
| JavaScript | Stub | Basic complexity analysis |
| TypeScript | Stub | Basic complexity analysis |

## See Also

- [Quick Fixes]quick-fixes.md - Common problems with immediate solutions
- [Debug Mode]debug-mode.md - Verbosity levels for diagnostics
- [FAQ]faq.md - Frequently asked questions