ccgo 3.4.4

A high-performance C++ cross-platform build CLI
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
427
428
429
430
431
432
433
434
435
436
437
# Error Handling Implementation Guide

> Version: v3.0.10 | Updated: 2026-01-21

## Overview

CCGO's enhanced error handling system provides:

1. **User-friendly error messages** with actionable hints
2. **Graceful degradation** when optional tools are missing
3. **Comprehensive configuration validation** with helpful suggestions
4. **Better build failure diagnostics**

## Architecture

```
src/
├── error.rs                      # Custom error types and hints
├── config/
│   ├── ccgo_toml.rs             # Configuration parsing (enhanced)
│   └── validation.rs            # Comprehensive validation
└── utils/
    └── tools.rs                 # Tool detection and validation
```

## Components

### 1. Error Module (`src/error.rs`)

Custom error types with contextual hints:

```rust
use crate::error::{CcgoError, hints};

// Configuration errors
CcgoError::config_error_with_hint(
    "Invalid package name",
    None,
    "Package name must be a valid C++ identifier"
)

// Dependency errors
CcgoError::dependency_error_with_hint(
    "fmt",
    "Invalid version requirement",
    "Version requirements support: ^1.0.0, ~1.2.0, >=1.0.0"
)

// Missing tool errors
CcgoError::missing_tool(
    "cmake",
    "building projects",
    hints::cmake()  // Provides installation instructions
)

// Build failures with diagnostics
CcgoError::build_failure_with_diagnostics(
    "Android",
    "NDK not found",
    vec!["Check ANDROID_NDK environment variable"],
    Some(hints::android_ndk())
)
```

#### Available Hints

The `hints` module provides installation instructions for common tools:

- `hints::cmake()` - CMake installation
- `hints::git()` - Git installation
- `hints::android_ndk()` - Android NDK setup
- `hints::xcode()` - Xcode installation
- `hints::visual_studio()` - Visual Studio setup
- `hints::mingw()` - MinGW installation
- `hints::gradle()` - Gradle installation
- `hints::python()` - Python installation
- `hints::doxygen()` - Doxygen installation

#### Common Error Patterns

```rust
// CCGO.toml not found
hints::ccgo_toml_not_found()

// Invalid CCGO.toml
hints::invalid_ccgo_toml()

// Dependency resolution failure
hints::dependency_resolution()

// Build configuration issues
hints::build_config()

// Lockfile out of sync
hints::lockfile_mismatch()
```

### 2. Configuration Validation (`src/config/validation.rs`)

Comprehensive validation with helpful error messages:

```rust
use crate::config::validate_config;

// Load and validate configuration
let config = CcgoConfig::load()?;  // Automatically validates

// Manual validation
validate_config(&config)?;
```

#### Validation Checks

**Package Metadata:**
- ✅ Package name (must be valid C++ identifier, not a keyword)
- ✅ Version (must be valid semver: `1.0.0`, `2.3.4-alpha.1`)
- ✅ License (warns about non-standard SPDX identifiers)

**Workspace Configuration:**
- ✅ Non-empty members list for workspace-only configs
- ✅ Valid resolver version ("1" or "2")

**Dependencies:**
- ✅ At least one valid source (version, git, or path)
- ✅ Valid version requirements (`^1.0.0`, `~1.2.0`, `>=1.0.0`)
- ✅ Valid git URLs (must start with `https://`, `git://`, etc.)
- ✅ No conflicting git refs (can't have both branch and tag)
- ✅ Path dependencies exist (warning only)

**Build Configuration:**
- ✅ Parallel jobs > 0
- ✅ Reasonable job count (warns if > 128)

**Platform Configuration:**
- ✅ Android minSdk >= 16 (warns if below recommended)
- ✅ Valid iOS version format (`"12.0"`, `"14.0"`)

**Binaries and Examples:**
- ✅ Valid names (C++ identifiers)
- ✅ Source files exist (warning only)

### 3. Tool Detection (`src/utils/tools.rs`)

Detect build tools with graceful degradation:

```rust
use crate::utils::tools::{
    check_tool,
    require_tool,
    check_tool_with_requirement,
    ToolRequirement,
    PlatformTools,
};

// Check if a tool exists
if let Some(tool_info) = check_tool("cmake") {
    println!("CMake version: {:?}", tool_info.version);
}

// Require a tool (error if missing)
let cmake = require_tool("cmake", "building projects")?;

// Check with requirement level
check_tool_with_requirement(
    "doxygen",
    ToolRequirement::Recommended,
    "documentation generation"
)?;
// ⚠️ Warns if missing, but continues
```

#### Tool Requirement Levels

```rust
pub enum ToolRequirement {
    /// Required - fails with helpful error if missing
    Required,

    /// Optional - silent if missing
    Optional,

    /// Recommended - warns if missing, continues
    Recommended,
}
```

#### Platform-Specific Tool Checking

```rust
use crate::utils::tools::{
    PlatformTools,
    check_android_environment,
    check_apple_environment,
    check_windows_environment,
};

// Check platform-specific tools
let checker = PlatformTools::new("android");
let (required, optional) = checker.check_all()?;

// Android environment
check_android_environment()?;  // Checks ANDROID_NDK, ANDROID_HOME

// Apple environment
check_apple_environment()?;    // Checks Xcode, command line tools

// Windows environment
check_windows_environment("msvc")?;  // Checks MSVC or MinGW
```

## Integration Examples

### Example 1: Enhanced Build Command

```rust
use crate::utils::tools::{PlatformTools, ToolRequirement};
use crate::error::CcgoError;

pub fn execute_build(platform: &str) -> Result<()> {
    // Check required tools
    let checker = PlatformTools::new(platform);
    let (required_tools, optional_tools) = checker.check_all()?;

    println!("✓ Required tools found:");
    for tool in &required_tools {
        println!("  • {}: {}", tool.name,
            tool.version.as_deref().unwrap_or("installed"));
    }

    if !optional_tools.is_empty() {
        println!("✓ Optional tools found:");
        for tool in &optional_tools {
            println!("  • {}", tool.name);
        }
    }

    // Proceed with build...
    Ok(())
}
```

### Example 2: Enhanced Install Command

```rust
use crate::error::{CcgoError, hints};
use crate::config::validate_config;

pub fn execute_install(dep_name: Option<&str>) -> Result<()> {
    // Load and validate config
    let config = CcgoConfig::load()?;  // Auto-validates

    // Check git is available for git dependencies
    let has_git_deps = config.dependencies.iter()
        .any(|d| d.git.is_some());

    if has_git_deps {
        require_tool("git", "installing git dependencies")?;
    }

    // Install dependencies...
    Ok(())
}
```

### Example 3: Better Error Messages

**Before:**
```
Error: Failed to parse CCGO.toml
```

**After:**
```
ERROR: Failed to parse CCGO.toml

Common issues:
• Invalid TOML syntax (check quotes, brackets, commas)
• Typo in section names (should be [package] or [workspace])
• Missing closing brackets or quotes

Validate your TOML at: https://www.toml-lint.com/
```

**Before:**
```
Error: Invalid version requirement '1.0'
```

**After:**
```
ERROR: Invalid version requirement '1.0' for dependency 'fmt'

HINT: Version requirements support:
• Exact: "1.2.3" or "=1.2.3"
• Range: ">=1.0.0, <2.0.0"
• Caret: "^1.0.0" (allows 1.x.x)
• Tilde: "~1.2.0" (allows 1.2.x)
• Wildcard: "1.*" or "1.2.*"
```

## Best Practices

### 1. Always Provide Context

```rust
// Bad
bail!("Tool not found");

// Good
Err(CcgoError::missing_tool(
    "cmake",
    "building C++ projects",
    hints::cmake()
).into())
```

### 2. Use Appropriate Error Types

```rust
// For configuration errors
CcgoError::config_error_with_hint(message, source, hint)

// For dependency errors
CcgoError::dependency_error_with_hint(dep_name, message, hint)

// For missing tools
CcgoError::missing_tool(tool, required_for, hint)

// For build failures
CcgoError::build_failure_with_diagnostics(platform, message, diagnostics, hint)
```

### 3. Validate Early

```rust
// Validate configuration immediately after loading
let config = CcgoConfig::load()?;  // Validates automatically

// Or explicit validation
validate_config(&config)?;
```

### 4. Check Tools Before Building

```rust
// For platform-specific builds
let checker = PlatformTools::new("android");
checker.check_required()?;  // Fails fast with helpful errors

// For optional features
check_tool_with_requirement(
    "doxygen",
    ToolRequirement::Recommended,
    "documentation generation"
)?;
```

## Error Message Guidelines

When creating error messages:

1. **Be specific**: Say what went wrong and where
2. **Be actionable**: Tell user how to fix it
3. **Be concise**: Keep hints focused on the most likely solution
4. **Be helpful**: Include examples, links, or commands to run

Example:
```
❌ Bad: "Invalid configuration"
✅ Good:
ERROR: Invalid package name 'class' in CCGO.toml

HINT: Package name 'class' is a C++ reserved keyword.
Choose a different name, e.g., 'class_lib' or 'libclass'
```

## Testing

All validation logic includes unit tests:

```bash
cargo test error
cargo test validation
cargo test tools
```

## Current Status

✅ **Completed:**
- Custom error types with hints
- Comprehensive configuration validation
- Tool detection with graceful degradation
- Enhanced CCGO.toml parsing errors
- Platform-specific tool checking

📝 **Integration Tasks (Future):**
- Integrate tool checking into `build` command
- Integrate tool checking into `publish` command
- Add more platform-specific validations
- Build failure diagnostics with common solutions

## Migration Guide

To use the new error handling in existing code:

### Before:
```rust
use anyhow::bail;

if tool_missing {
    bail!("cmake not found");
}
```

### After:
```rust
use crate::error::{CcgoError, hints};
use crate::utils::tools::require_tool;

// Option 1: Use tool detection
require_tool("cmake", "building projects")?;

// Option 2: Manual error creation
if tool_missing {
    return Err(CcgoError::missing_tool(
        "cmake",
        "building projects",
        hints::cmake()
    ).into());
}
```

## See Also

- [Contributing Guide]contributing.md
- [CCGO.toml Reference]../reference/ccgo-toml.md
- [Roadmap]roadmap.md