quasar-cli 0.0.0

CLI for the Quasar Solana framework
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use {
    crate::{config::QuasarConfig, error::CliResult, style, toolchain, utils},
    std::{
        fs,
        path::{Path, PathBuf},
        process::{Command, Stdio},
        time::Instant,
    },
};

pub fn run(debug: bool, watch: bool, features: Option<String>) -> CliResult {
    if watch {
        return run_watch(debug, features);
    }

    run_once(debug, features.as_deref())
}

fn run_once(debug: bool, features: Option<&str>) -> CliResult {
    let config = QuasarConfig::load()?;
    let start = Instant::now();

    crate::idl::generate(Path::new("."), config.has_typescript_tests())?;

    let sp = style::spinner("Building...");

    let output = if config.is_solana_toolchain() {
        let mut cmd = Command::new("cargo");
        cmd.arg("build-sbf");
        if debug {
            cmd.arg("--debug");
        }
        if let Some(f) = features {
            cmd.args(["--features", f]);
        }
        cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output()
    } else {
        if !toolchain::has_sbpf_linker() {
            sp.finish_and_clear();
            eprintln!("\n  {}", style::fail("sbpf-linker not found on PATH."));
            eprintln!();
            eprintln!("  Install platform-tools first:");
            eprintln!(
                "    {}",
                style::bold("git clone https://github.com/anza-xyz/platform-tools")
            );
            eprintln!("    {}", style::bold("cd platform-tools"));
            eprintln!("    {}", style::bold("cargo install-with-gallery"));
            std::process::exit(1);
        }

        let mut cmd = Command::new("cargo");
        if debug {
            cmd.env("RUSTFLAGS", "-C link-arg=--btf -C debuginfo=2");
        }
        cmd.arg("build-bpf");
        if let Some(f) = features {
            cmd.args(["--features", f]);
        }
        cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output()
    };

    sp.finish_and_clear();

    match output {
        Ok(o) if o.status.success() => {
            let elapsed = start.elapsed();

            if !config.is_solana_toolchain() {
                let program = config.module_name();
                let src = PathBuf::from("target")
                    .join("bpfel-unknown-none")
                    .join("release")
                    .join(format!("lib{}.so", program));
                let dest_dir = PathBuf::from("target").join("deploy");
                fs::create_dir_all(&dest_dir)?;
                let dest = dest_dir.join(format!("lib{}.so", program));
                fs::copy(&src, &dest).map_err(|e| {
                    eprintln!(
                        "  {}",
                        style::fail(&format!("failed to copy {}: {e}", src.display()))
                    );
                    e
                })?;
            }

            // Show warnings even on success
            let stderr = String::from_utf8_lossy(&o.stderr);
            let warnings = extract_warnings(&stderr);
            if !warnings.is_empty() {
                eprintln!();
                for line in &warnings {
                    eprintln!("  {line}");
                }
            }

            let so_path = utils::find_so(&config, false);
            let size_info = so_path
                .and_then(|p| {
                    let meta = fs::metadata(&p).ok()?;
                    let new_size = meta.len();
                    let delta = size_delta(&p, new_size);
                    save_last_size(&p, new_size);
                    Some(format!(
                        " ({}{delta})",
                        style::dim(&style::human_size(new_size))
                    ))
                })
                .unwrap_or_default();

            println!(
                "  {}",
                style::success(&format!(
                    "Build complete in {}{size_info}",
                    style::bold(&style::human_duration(elapsed))
                ))
            );
            Ok(())
        }
        Ok(o) => {
            let elapsed = start.elapsed();
            let stderr = String::from_utf8_lossy(&o.stderr);
            print_build_errors(&stderr, elapsed);
            std::process::exit(o.status.code().unwrap_or(1));
        }
        Err(e) => {
            eprintln!(
                "  {}",
                style::fail(&format!("failed to run build command: {e}"))
            );
            std::process::exit(1);
        }
    }
}

/// Build with debug symbols only (no feature flags) for profiling.
/// Copies the .so to target/profile/ and returns the path.
pub fn profile_build() -> Result<PathBuf, crate::error::CliError> {
    let config = QuasarConfig::load()?;
    let start = Instant::now();

    crate::idl::generate(Path::new("."), config.has_typescript_tests())?;

    let sp = style::spinner("Profile build...");

    let output = if config.is_solana_toolchain() {
        let mut cmd = Command::new("cargo");
        cmd.arg("build-sbf").arg("--debug");
        cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output()
    } else {
        if !toolchain::has_sbpf_linker() {
            sp.finish_and_clear();
            eprintln!("\n  {}", style::fail("sbpf-linker not found on PATH."));
            eprintln!();
            eprintln!("  Install platform-tools first:");
            eprintln!(
                "    {}",
                style::bold("git clone https://github.com/anza-xyz/platform-tools")
            );
            eprintln!("    {}", style::bold("cd platform-tools"));
            eprintln!("    {}", style::bold("cargo install-with-gallery"));
            std::process::exit(1);
        }

        // Read existing rustflags from .cargo/config.toml and append debug flags
        let existing_flags = read_target_rustflags();
        let mut all_flags = existing_flags;
        all_flags.extend([
            "-C".to_string(),
            "link-arg=--btf".to_string(),
            "-C".to_string(),
            "debuginfo=2".to_string(),
        ]);

        // Use CARGO_ENCODED_RUSTFLAGS (0x1f-separated) which takes priority
        let encoded = all_flags.join("\x1f");
        let mut cmd = Command::new("cargo");
        cmd.env("CARGO_ENCODED_RUSTFLAGS", encoded);
        cmd.arg("build-bpf");
        cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output()
    };

    sp.finish_and_clear();

    match output {
        Ok(o) if o.status.success() => {
            let elapsed = start.elapsed();
            let program = config.module_name();
            let profile_dir = PathBuf::from("target").join("profile");
            fs::create_dir_all(&profile_dir)?;

            // Find the built .so and copy to target/profile/
            let src = if config.is_solana_toolchain() {
                // build-sbf --debug puts it in target/deploy/ or
                // target/sbf-solana-solana/release/
                utils::find_so(&config, false).unwrap_or_else(|| {
                    PathBuf::from("target")
                        .join("sbf-solana-solana")
                        .join("release")
                        .join(format!("{}.so", program))
                })
            } else {
                PathBuf::from("target")
                    .join("bpfel-unknown-none")
                    .join("release")
                    .join(format!("lib{}.so", program))
            };

            let dest = profile_dir.join(format!("{}.so", program));
            fs::copy(&src, &dest).map_err(|e| {
                eprintln!(
                    "  {}",
                    style::fail(&format!("failed to copy {}: {e}", src.display()))
                );
                e
            })?;

            let size = fs::metadata(&dest).map(|m| m.len()).unwrap_or(0);
            println!(
                "  {}",
                style::success(&format!(
                    "Profile build in {} ({})",
                    style::bold(&style::human_duration(elapsed)),
                    style::dim(&style::human_size(size))
                ))
            );

            Ok(dest)
        }
        Ok(o) => {
            let elapsed = start.elapsed();
            let stderr = String::from_utf8_lossy(&o.stderr);
            print_build_errors(&stderr, elapsed);
            std::process::exit(o.status.code().unwrap_or(1));
        }
        Err(e) => {
            eprintln!(
                "  {}",
                style::fail(&format!("failed to run build command: {e}"))
            );
            std::process::exit(1);
        }
    }
}

fn run_watch(debug: bool, features: Option<String>) -> CliResult {
    if let Err(e) = run_once(debug, features.as_deref()) {
        eprintln!("  {}", style::fail(&format!("{e}")));
    }

    loop {
        let baseline = collect_mtimes(Path::new("src"));
        loop {
            std::thread::sleep(std::time::Duration::from_secs(1));
            let current = collect_mtimes(Path::new("src"));
            if current != baseline {
                if let Err(e) = run_once(debug, features.as_deref()) {
                    eprintln!("  {}", style::fail(&format!("{e}")));
                }
                break;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Build error formatting
// ---------------------------------------------------------------------------

/// Extract warning lines from cargo output (for display on success).
fn extract_warnings(stderr: &str) -> Vec<String> {
    let mut warnings = Vec::new();
    let mut capture = false;

    for line in stderr.lines() {
        if line.starts_with("warning") {
            if line.contains("warnings emitted")
                || line.contains("warning emitted")
                || line.contains("user-defined alias")
                || line.contains("shadowing")
            {
                continue;
            }
            capture = true;
            warnings.push(line.to_string());
        } else if capture {
            if line.starts_with("  ") || line.starts_with(" -->") || line.is_empty() {
                warnings.push(line.to_string());
            } else {
                capture = false;
            }
        }
    }

    warnings
}

/// Extract and display only the meaningful error/warning lines from cargo
/// output.
fn print_build_errors(stderr: &str, elapsed: std::time::Duration) {
    let mut errors: Vec<String> = Vec::new();
    let mut capture = false;

    for line in stderr.lines() {
        // Primary error/warning lines from rustc or cargo
        if line.starts_with("error") || line.starts_with("warning") {
            // Skip "warning: N warnings emitted" summary lines
            if line.contains("warnings emitted") || line.contains("warning emitted") {
                continue;
            }
            // Skip the cargo alias shadow warning
            if line.contains("user-defined alias") || line.contains("shadowing") {
                continue;
            }
            capture = true;
            errors.push(line.to_string());
        } else if capture {
            // Capture continuation lines (source snippets, arrows, notes, "Caused by:")
            if line.starts_with("  ")
                || line.starts_with(" -->")
                || line.starts_with("Caused by:")
                || line.is_empty()
            {
                errors.push(line.to_string());
            } else {
                capture = false;
            }
        }
    }

    if errors.is_empty() {
        // Fallback: show raw stderr if we couldn't parse errors
        if !stderr.is_empty() {
            eprint!("{stderr}");
        }
        eprintln!(
            "  {}",
            style::fail(&format!(
                "build failed in {}",
                style::bold(&style::human_duration(elapsed))
            ))
        );
        return;
    }

    eprintln!();
    for line in &errors {
        eprintln!("  {line}");
    }
    eprintln!();

    // Count errors vs warnings
    let err_count = errors.iter().filter(|l| l.starts_with("error")).count();
    let warn_count = errors.iter().filter(|l| l.starts_with("warning")).count();

    let mut summary = String::new();
    if err_count > 0 {
        summary.push_str(&format!(
            "{err_count} error{}",
            if err_count == 1 { "" } else { "s" }
        ));
    }
    if warn_count > 0 {
        if !summary.is_empty() {
            summary.push_str(", ");
        }
        summary.push_str(&format!(
            "{warn_count} warning{}",
            if warn_count == 1 { "" } else { "s" }
        ));
    }

    eprintln!(
        "  {}",
        style::fail(&format!(
            "build failed in {} ({summary})",
            style::bold(&style::human_duration(elapsed))
        ))
    );
}

// ---------------------------------------------------------------------------
// Build size tracking
// ---------------------------------------------------------------------------

const LAST_SIZE_FILE: &str = "target/.quasar-last-size";

fn size_delta(so_path: &Path, new_size: u64) -> String {
    let key = so_path.to_string_lossy();
    let last = fs::read_to_string(LAST_SIZE_FILE)
        .ok()
        .and_then(|contents| {
            contents
                .lines()
                .find(|l| l.starts_with(&*key))
                .and_then(|l| l.rsplit_once(' '))
                .and_then(|(_, s)| s.parse::<u64>().ok())
        });

    let Some(prev) = last else {
        return String::new();
    };

    if new_size == prev {
        return String::new();
    }

    let diff = new_size as i64 - prev as i64;
    if diff > 0 {
        format!(
            ", {}",
            style::color(196, &format!("+{}", style::human_size(diff as u64)))
        )
    } else {
        format!(
            ", {}",
            style::color(83, &format!("-{}", style::human_size((-diff) as u64)))
        )
    }
}

fn save_last_size(so_path: &Path, size: u64) {
    let key = so_path.to_string_lossy();
    let entry = format!("{key} {size}");

    // Read existing entries, replace or append
    let mut lines: Vec<String> = fs::read_to_string(LAST_SIZE_FILE)
        .unwrap_or_default()
        .lines()
        .filter(|l| !l.starts_with(&*key))
        .map(String::from)
        .collect();
    lines.push(entry);
    let _ = fs::write(LAST_SIZE_FILE, lines.join("\n"));
}

/// Read rustflags from .cargo/config.toml for the bpfel-unknown-none target.
fn read_target_rustflags() -> Vec<String> {
    let config_path = Path::new(".cargo").join("config.toml");
    let contents = match fs::read_to_string(&config_path) {
        Ok(c) => c,
        Err(_) => return vec![],
    };
    let value: toml::Value = match contents.parse() {
        Ok(v) => v,
        Err(_) => return vec![],
    };
    value
        .get("target")
        .and_then(|t| t.get("bpfel-unknown-none"))
        .and_then(|t| t.get("rustflags"))
        .and_then(|f| f.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default()
}

pub fn collect_mtimes(dir: &Path) -> Vec<(PathBuf, std::time::SystemTime)> {
    let mut times = Vec::new();
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                times.extend(collect_mtimes(&path));
            } else if path.extension().is_some_and(|e| e == "rs") {
                if let Ok(meta) = fs::metadata(&path) {
                    if let Ok(mtime) = meta.modified() {
                        times.push((path, mtime));
                    }
                }
            }
        }
    }
    times.sort_by(|a, b| a.0.cmp(&b.0));
    times
}