cgx-core 0.0.3

Core library for cgx, the Rust equivalent of uvx or npx for running Rust crates quickly and easily
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use crate::{
    Result,
    builder::{BuildOptions, BuildTarget},
    error,
};
use snafu::{OptionExt, ResultExt};
use std::{
    path::{Path, PathBuf},
    process::Command,
};

pub(crate) use cargo_metadata::Metadata;

/// Verbosity level for cargo build operations.
///
/// Maps to cargo's `-v` flags for controlling build output verbosity.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CargoVerbosity {
    /// Normal cargo output (no verbosity flags).
    #[default]
    Normal,

    /// Verbose output (corresponds to `-v`).
    Verbose,

    /// Very verbose output (corresponds to `-vv`).
    VeryVerbose,

    /// Extremely verbose output including build.rs output (corresponds to `-vvv`).
    ExtremelyVerbose,
}

impl CargoVerbosity {
    /// Construct a [`CargoVerbosity`] from a verbosity counter.
    ///
    /// The counter typically comes from CLI arguments where `-v` can be repeated.
    pub(crate) fn from_count(count: u8) -> Self {
        match count {
            0 => Self::Normal,
            1 => Self::Verbose,
            2 => Self::VeryVerbose,
            _ => Self::ExtremelyVerbose,
        }
    }
}

/// Options for controlling cargo metadata invocation.
#[derive(Clone, Debug, Default)]
pub(crate) struct CargoMetadataOptions {
    /// Exclude dependency information from metadata output.
    /// Corresponds to `--no-deps` flag.
    /// Default: false (dependencies are included by default)
    pub no_deps: bool,

    /// Only include dependencies for the specified target platform.
    /// Corresponds to `--filter-platform TARGET` flag.
    pub filter_platform: Option<String>,

    /// Space or comma separated list of features to activate.
    /// Corresponds to `--features` flag.
    pub features: Vec<String>,

    /// Activate all available features.
    /// Corresponds to `--all-features` flag.
    pub all_features: bool,

    /// Do not activate the `default` feature.
    /// Corresponds to `--no-default-features` flag.
    pub no_default_features: bool,

    /// Run without accessing the network.
    /// Corresponds to `--offline` flag.
    pub offline: bool,

    /// Require Cargo.lock is up to date.
    /// Corresponds to `--locked` flag.
    pub locked: bool,
}

impl From<&BuildOptions> for CargoMetadataOptions {
    fn from(opts: &BuildOptions) -> Self {
        Self {
            no_deps: false,
            filter_platform: opts.target.clone(),
            features: opts.features.clone(),
            all_features: opts.all_features,
            no_default_features: opts.no_default_features,
            offline: opts.offline,
            locked: opts.locked,
        }
    }
}

/// Rust wrapper around shelling out to `cargo` for building and running Rust projects.
///
/// Much as it pains me, sometimes we must shell out to `cargo` to do things.  That's ugly,
/// error-prone, and worst of all inelegant.  But it's also the only way to get certain things
/// done.
///
/// This type is mainly concerened with the surprisingly complex task of figuring out where `cargo`
/// is and how to invoke it, and secondarily with constructing its command lines and parsing the
/// resulting output.
pub(crate) trait CargoRunner: std::fmt::Debug + Send + Sync + 'static {
    /// Get cargo metadata for a source directory.
    ///
    /// Executes `cargo metadata` on the specified directory and returns the
    /// parsed metadata including workspace members, packages, and targets.
    ///
    /// # Arguments
    ///
    /// * `source_dir` - Path to directory containing Cargo.toml
    /// * `options` - Options controlling metadata invocation (deps, features, platform, etc.)
    fn metadata(&self, source_dir: &Path, options: &CargoMetadataOptions) -> Result<Metadata>;

    /// Build a binary from source.
    ///
    /// Executes cargo build with specified options and returns the absolute path
    /// to the compiled binary, determined by parsing `--message-format=json` output.
    ///
    /// It is assumed that either the only crate in the workspace is a binary, or that the crate
    /// `package` has a binary or example matching `options.build_target`.
    ///
    /// # Arguments
    ///
    /// * `source_dir` - Directory containing Cargo.toml
    /// * `package` - Package name for `-p` flag (required for multi-package workspaces)
    /// * `options` - Build configuration
    ///
    /// # Toolchain Handling
    ///
    /// If `options.toolchain` is specified:
    /// - Requires rustup (errors if unavailable)
    /// - Invokes via `rustup run {toolchain} cargo build ...`
    /// - This works regardless of whether cargo is a rustup proxy
    ///
    /// # Binary Location
    ///
    /// Uses `--message-format=json` to parse compiler artifacts and find the
    /// executable path from "compiler-artifact" messages. This handles:
    /// - Cross-compilation: target/{triple}/{profile}/...
    /// - Examples: target/{profile}/examples/...
    /// - Platform extensions: .exe on Windows
    ///
    /// # Errors
    ///
    /// - Cargo.toml not found in `source_dir`
    /// - Toolchain specified but rustup not found
    /// - Cargo build command fails
    /// - Expected binary not found in cargo's JSON output
    fn build(&self, source_dir: &Path, package: Option<&str>, options: &BuildOptions) -> Result<PathBuf>;
}

/// Locate cargo and construct a runner instance that will use it.
pub(crate) fn find_cargo() -> Result<impl CargoRunner> {
    // Locate cargo and rustup executables.
    //
    // Searches for cargo in priority order:
    // 1. `CARGO` environment variable (cargo's own convention)
    // 2. `cargo` in PATH (via `which` crate)
    // 3. `$CARGO_HOME/bin/cargo` where CARGO_HOME defaults to ~/.cargo
    //
    // Also searches for rustup (needed for `rustup run {toolchain}`).
    // Rustup not found is non-fatal - only errors when toolchain specified.

    let cargo_path = find_executable("cargo", "CARGO")?;
    let rustup_path = find_executable("rustup", "RUSTUP").ok();

    Ok(RealCargoRunner {
        cargo_path,
        rustup_path,
    })
}

#[derive(Debug, Clone)]
struct RealCargoRunner {
    cargo_path: PathBuf,
    rustup_path: Option<PathBuf>,
}

impl CargoRunner for RealCargoRunner {
    fn metadata(&self, source_dir: &Path, options: &CargoMetadataOptions) -> Result<Metadata> {
        use snafu::ResultExt;

        let mut cmd = cargo_metadata::MetadataCommand::new();
        cmd.cargo_path(&self.cargo_path).current_dir(source_dir);

        // Only exclude deps if explicitly requested
        if options.no_deps {
            cmd.no_deps();
        }

        // Handle feature flags
        if options.all_features {
            cmd.features(cargo_metadata::CargoOpt::AllFeatures);
        } else {
            if options.no_default_features {
                cmd.features(cargo_metadata::CargoOpt::NoDefaultFeatures);
            }
            if !options.features.is_empty() {
                cmd.features(cargo_metadata::CargoOpt::SomeFeatures(options.features.clone()));
            }
        }

        // Build other_options for flags that don't have dedicated MetadataCommand methods
        let mut other_args = Vec::new();

        // Always filter by platform when resolving dependencies to avoid getting
        // deps for all platforms mixed together. Default to current platform if not specified.
        let platform: Option<&str> = if options.no_deps {
            // When not resolving deps, platform filtering doesn't matter
            options.filter_platform.as_deref()
        } else {
            // When resolving deps, MUST filter by platform
            // Default to current platform if not specified
            Some(
                options
                    .filter_platform
                    .as_deref()
                    .unwrap_or(build_context::TARGET),
            )
        };

        if let Some(platform_str) = platform {
            other_args.push("--filter-platform".to_string());
            other_args.push(platform_str.to_string());
        }

        if options.offline {
            other_args.push("--offline".to_string());
        }

        if options.locked {
            other_args.push("--locked".to_string());
        }

        if !other_args.is_empty() {
            cmd.other_options(other_args);
        }

        cmd.exec().with_context(|_| error::CargoMetadataSnafu {
            cargo_path: self.cargo_path.clone(),
            source_dir: source_dir.to_path_buf(),
        })
    }

    fn build(&self, source_dir: &Path, package: Option<&str>, options: &BuildOptions) -> Result<PathBuf> {
        // Verify Cargo.toml exists
        if !source_dir.join("Cargo.toml").exists() {
            return error::CargoTomlNotFoundSnafu {
                source_dir: source_dir.to_path_buf(),
            }
            .fail();
        }

        // Build the command
        let mut cmd = if let Some(toolchain) = &options.toolchain {
            // If toolchain is specified, we need rustup
            let rustup_path = self
                .rustup_path
                .as_ref()
                .with_context(|| error::RustupNotFoundSnafu {
                    toolchain: toolchain.clone(),
                })?;

            let mut cmd = Command::new(rustup_path);
            cmd.args(["run", toolchain, "cargo"]);
            cmd
        } else {
            Command::new(&self.cargo_path)
        };

        // Add cargo build command and flags
        cmd.arg("build");
        cmd.current_dir(source_dir);
        cmd.arg("--message-format=json");

        // Profile (default to release)
        if let Some(profile) = &options.profile {
            cmd.args(["--profile", profile]);
        } else {
            cmd.arg("--release");
        }

        // Package selection for workspaces
        if let Some(pkg) = package {
            cmd.args(["-p", pkg]);
        }

        // Features
        if options.all_features {
            cmd.arg("--all-features");
        } else {
            if options.no_default_features {
                cmd.arg("--no-default-features");
            }
            if !options.features.is_empty() {
                cmd.arg("--features");
                cmd.arg(options.features.join(","));
            }
        }

        // Target triple for cross-compilation
        if let Some(target) = &options.target {
            cmd.args(["--target", target]);
        }

        // Build target (bin/example)
        match &options.build_target {
            BuildTarget::DefaultBin => {
                // No specific flag needed, cargo will build the default binary
            }
            BuildTarget::Bin(name) => {
                cmd.args(["--bin", name]);
            }
            BuildTarget::Example(name) => {
                cmd.args(["--example", name]);
            }
        }

        // Other flags
        if options.locked {
            cmd.arg("--locked");
        }
        if options.offline {
            cmd.arg("--offline");
        }
        if let Some(jobs) = options.jobs {
            cmd.args(["-j", &jobs.to_string()]);
        }
        if options.ignore_rust_version {
            cmd.arg("--ignore-rust-version");
        }

        // Verbosity flags
        match options.cargo_verbosity {
            CargoVerbosity::Normal => {}
            CargoVerbosity::Verbose => {
                cmd.arg("-v");
            }
            CargoVerbosity::VeryVerbose => {
                cmd.arg("-vv");
            }
            CargoVerbosity::ExtremelyVerbose => {
                cmd.arg("-vvv");
            }
        }

        // Execute the command
        let output = cmd.output().context(error::CommandExecutionSnafu)?;

        if !output.status.success() {
            return error::CargoBuildFailedSnafu {
                exit_code: output.status.code(),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            }
            .fail();
        }

        // Parse JSON output to find the binary
        let stdout = String::from_utf8_lossy(&output.stdout);
        for line in stdout.lines() {
            if let Ok(msg) = serde_json::from_str::<serde_json::Value>(line) {
                if msg.get("reason").and_then(|r| r.as_str()) == Some("compiler-artifact") {
                    let target = msg.get("target");
                    let kinds = target.and_then(|t| t.get("kind")).and_then(|k| k.as_array());
                    let name = target.and_then(|t| t.get("name")).and_then(|n| n.as_str());

                    let matches = match &options.build_target {
                        BuildTarget::DefaultBin => {
                            kinds.is_some_and(|k| k.iter().any(|v| v.as_str() == Some("bin")))
                        }
                        BuildTarget::Bin(bin_name) => {
                            kinds.is_some_and(|k| k.iter().any(|v| v.as_str() == Some("bin")))
                                && name == Some(bin_name.as_str())
                        }
                        BuildTarget::Example(ex_name) => {
                            kinds.is_some_and(|k| k.iter().any(|v| v.as_str() == Some("example")))
                                && name == Some(ex_name.as_str())
                        }
                    };

                    if matches {
                        if let Some(exe) = msg.get("executable").and_then(|e| e.as_str()) {
                            return Ok(PathBuf::from(exe));
                        }
                    }
                }
            }
        }

        error::BinaryNotFoundInOutputSnafu.fail()
    }
}

/// Find an executable by name, checking environment variable, PATH, and default locations.
fn find_executable(name: &str, env_var: &str) -> Result<PathBuf> {
    // Check environment variable
    if let Ok(path) = std::env::var(env_var) {
        let path = PathBuf::from(path);
        if path.exists() {
            return Ok(path);
        }
    }

    // Check PATH using `which` crate
    if let Ok(path) = which::which(name) {
        return Ok(path);
    }

    // Check $CARGO_HOME/bin/{name}
    let cargo_home = std::env::var("CARGO_HOME")
        .ok()
        .map(PathBuf::from)
        .or_else(|| home::cargo_home().ok());

    if let Some(cargo_home) = cargo_home {
        let path = cargo_home.join("bin").join(name);
        if path.exists() {
            return Ok(path);
        }
    }

    error::ExecutableNotFoundSnafu {
        name: name.to_string(),
    }
    .fail()
}

/// Testing a wrapper around `cargo` thoroughly is out of the scope of simple unit tests, however
/// we at least need to verify basic functionality and correctness.
///
/// By definition, if these tests are running, `cargo` must be present, so we've made some tests
/// that operate on this project itself as test data.  Of course this isn't adequate coverage for
/// all various scenarios, but it's better than nothing.
#[cfg(test)]
mod tests {
    use super::*;
    use crate::{builder::BuildTarget, testdata::CrateTestCase};

    /// Get the path to the cgx workspace root directory.
    fn cgx_project_root() -> PathBuf {
        // CARGO_MANIFEST_DIR points to cgx-core, we need the workspace root (parent directory)
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .expect("cgx-core should have a parent directory (workspace root)")
            .to_path_buf()
    }

    #[test]
    fn find_cargo_succeeds() {
        crate::logging::init_test_logging();

        // This test verifies that we can locate cargo on the system.
        // This should always succeed since cargo is required to run the tests.
        let _cargo = find_cargo().unwrap();
    }

    #[test]
    fn metadata_reads_cgx_crate() {
        crate::logging::init_test_logging();

        let cargo = find_cargo().unwrap();
        let cgx_root = cgx_project_root();

        let metadata = cargo
            .metadata(
                &cgx_root,
                &CargoMetadataOptions {
                    no_deps: true,
                    ..Default::default()
                },
            )
            .unwrap();

        // Verify we found the cgx package
        let cgx_pkg = metadata
            .packages
            .iter()
            .find(|p| p.name.as_str() == "cgx")
            .unwrap();

        assert_eq!(cgx_pkg.name.as_str(), "cgx");

        // Verify version is valid semver
        assert!(!cgx_pkg.version.to_string().is_empty());

        // Verify we have at least one binary target
        let has_bin = cgx_pkg
            .targets
            .iter()
            .any(|t| t.kind.iter().any(|k| k.to_string() == "bin"));
        assert!(has_bin, "cgx should have a binary target");
    }

    #[test]
    fn build_compiles_cgx_in_tempdir() {
        crate::logging::init_test_logging();

        let cargo = find_cargo().unwrap();
        let cgx_root = cgx_project_root();
        let temp_dir = tempfile::tempdir().unwrap();

        // Copy source to temp directory
        crate::helpers::copy_source_tree(&cgx_root, temp_dir.path()).unwrap();

        // Verify Cargo.toml was copied
        assert!(
            temp_dir.path().join("Cargo.toml").exists(),
            "Cargo.toml should be copied"
        );

        // Build in dev mode (faster than release)
        let options = BuildOptions {
            profile: Some("dev".to_string()),
            build_target: BuildTarget::DefaultBin,
            ..Default::default()
        };

        let binary_path = cargo.build(temp_dir.path(), Some("cgx"), &options).unwrap();

        // Verify binary exists and is a file
        assert!(binary_path.exists(), "Binary should exist at {:?}", binary_path);
        assert!(binary_path.is_file(), "Binary should be a file");

        // Verify it's named correctly (cgx or cgx.exe on Windows)
        let file_name = binary_path.file_name().and_then(|n| n.to_str()).unwrap();
        assert!(
            file_name == "cgx" || file_name == "cgx.exe",
            "Binary should be named cgx or cgx.exe, got {}",
            file_name
        );
    }

    #[test]
    fn metadata_loads_all_testcases() {
        crate::logging::init_test_logging();

        let cargo = find_cargo().unwrap();

        for testcase in CrateTestCase::all() {
            let result = cargo.metadata(testcase.path(), &CargoMetadataOptions::default());

            assert!(
                result.is_ok(),
                "Failed to load metadata for {}: {:?}",
                testcase.name,
                result.err()
            );
        }
    }
}