candle-graph 0.2.0

Static structure and dataflow analysis for candle-rs models
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
//! Bounded Cargo/config discovery for analyzing a crate in its real feature/cfg context.
//!
//! Discovers the nearest `Cargo.toml`, runs `cargo metadata` and `rustc --print cfg` via
//! [`std::process::Command`] (never a shell), and returns a deterministic, serializable snapshot.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{anyhow, bail, Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Feature / target selection passed through to `cargo metadata` and `rustc --print cfg`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CargoOptions {
    /// Explicit features forwarded as `--features`.
    pub features: Vec<String>,
    /// Forwarded as `--all-features`.
    pub all_features: bool,
    /// Forwarded as `--no-default-features`.
    pub no_default_features: bool,
    /// Optional `--filter-platform` / `rustc --target` triple.
    pub target: Option<String>,
    /// Optional Cargo target name (`lib`/binary target), independent of the target triple.
    pub package_target: Option<String>,
}

/// One compile target of the selected package (lib, bin, …).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CargoTarget {
    pub name: String,
    pub kind: Vec<String>,
    pub src_path: PathBuf,
}

/// Deterministic snapshot of the Cargo package / workspace / feature / cfg context.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CargoContext {
    pub package_name: String,
    pub package_version: String,
    pub package_id: String,
    pub manifest_path: PathBuf,
    pub workspace_root: PathBuf,
    pub target_directory: PathBuf,
    /// Targets of the selected package, sorted by `(name, kind, src_path)`.
    pub targets: Vec<CargoTarget>,
    /// Resolved active features for the selected package, sorted.
    pub active_features: Vec<String>,
    /// Versions of every package whose name starts with `candle-`, keyed by name.
    pub candle_versions: BTreeMap<String, String>,
    /// Rust crate identifier (including dependency renames) to Cargo package name.
    pub dependency_aliases: BTreeMap<String, String>,
    /// Active `rustc --print cfg` lines plus `feature="…"` for each active feature, sorted.
    pub cfgs: Vec<String>,
}

/// Discover Cargo context for `path`, which may be a crate root or a nested source directory.
pub fn discover(path: impl AsRef<Path>, options: &CargoOptions) -> Result<CargoContext> {
    CargoContext::discover(path, options)
}

impl CargoContext {
    /// Discover Cargo context for `path`, which may be a crate root or a nested source directory.
    pub fn discover(path: impl AsRef<Path>, options: &CargoOptions) -> Result<Self> {
        let path = path.as_ref();
        let manifest_path = find_manifest(path)
            .with_context(|| format!("failed to locate Cargo.toml from {}", path.display()))?;

        let metadata = run_cargo_metadata(&manifest_path, options)?;
        let package = select_package(&metadata, &manifest_path)?;

        let package_name = package
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow!("package missing name"))?
            .to_string();
        let package_version = package
            .get("version")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow!("package missing version"))?
            .to_string();
        let package_id = package
            .get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow!("package missing id"))?
            .to_string();

        let manifest_path = path_from_json(package, "manifest_path")?;
        let workspace_root = path_from_root(&metadata, "workspace_root")?;
        let target_directory = path_from_root(&metadata, "target_directory")?;

        let mut targets = parse_targets(package)?;
        targets
            .sort_by(|a, b| (&a.name, &a.kind, &a.src_path).cmp(&(&b.name, &b.kind, &b.src_path)));

        let mut active_features = resolve_active_features(&metadata, &package_id)?;
        active_features.sort();
        active_features.dedup();

        let candle_versions = collect_candle_versions(&metadata, &package_id)?;
        let dependency_aliases = collect_dependency_aliases(package)?;

        let mut cfgs = collect_rustc_cfgs(options.target.as_deref())?;
        for feature in &active_features {
            cfgs.push(format!("feature=\"{feature}\""));
        }
        cfgs.sort();
        cfgs.dedup();

        Ok(Self {
            package_name,
            package_version,
            package_id,
            manifest_path,
            workspace_root,
            target_directory,
            targets,
            active_features,
            candle_versions,
            dependency_aliases,
            cfgs,
        })
    }

    /// Crate roots selected for source analysis.
    ///
    /// By default a library target is preferred because binaries normally consume it. Packages
    /// without a library select their first ordinary binary. Tests/examples/benches are included
    /// only when explicitly selected by target name.
    pub fn selected_source_roots(&self, requested: Option<&str>) -> Result<Vec<PathBuf>> {
        if let Some(name) = requested {
            let selected = self
                .targets
                .iter()
                .filter(|target| target.name == name)
                .map(|target| target.src_path.clone())
                .collect::<Vec<_>>();
            if selected.is_empty() {
                bail!(
                    "Cargo target `{name}` not found; available targets: {}",
                    self.targets
                        .iter()
                        .map(|target| target.name.as_str())
                        .collect::<Vec<_>>()
                        .join(", ")
                );
            }
            return Ok(selected);
        }

        if let Some(library) = self
            .targets
            .iter()
            .find(|target| target.kind.iter().any(|kind| kind == "lib"))
        {
            return Ok(vec![library.src_path.clone()]);
        }
        if let Some(binary) = self
            .targets
            .iter()
            .find(|target| target.kind.iter().any(|kind| kind == "bin"))
        {
            return Ok(vec![binary.src_path.clone()]);
        }
        bail!(
            "package `{}` has no library or binary target; select a target explicitly",
            self.package_name
        )
    }
}

fn collect_dependency_aliases(package: &Value) -> Result<BTreeMap<String, String>> {
    let dependencies = package
        .get("dependencies")
        .and_then(Value::as_array)
        .ok_or_else(|| anyhow!("package missing dependencies array"))?;
    let mut aliases = BTreeMap::new();
    for dependency in dependencies {
        let Some(name) = dependency.get("name").and_then(Value::as_str) else {
            continue;
        };
        let alias = dependency
            .get("rename")
            .and_then(Value::as_str)
            .unwrap_or(name)
            .replace('-', "_");
        aliases.insert(alias, name.to_string());
    }
    Ok(aliases)
}

/// Evaluate item-level `cfg` predicates against an active Cargo/rustc cfg snapshot.
///
/// `Some(true)` and `Some(false)` are exact for the standard `all`, `any`, `not`, key/value,
/// and bare-name forms. `None` means the source used a predicate form this bounded evaluator
/// does not understand; callers must preserve that branch rather than guessing.
pub fn cfg_predicates_active(predicates: &[String], active_cfg: &[String]) -> Option<bool> {
    let active = active_cfg
        .iter()
        .map(|item| normalize_cfg(item))
        .collect::<std::collections::HashSet<_>>();
    let mut unknown = false;
    for predicate in predicates {
        match eval_cfg(&normalize_cfg(predicate), &active) {
            Some(false) => return Some(false),
            Some(true) => {}
            None => unknown = true,
        }
    }
    (!unknown).then_some(true)
}

fn eval_cfg(predicate: &str, active: &std::collections::HashSet<String>) -> Option<bool> {
    if let Some(arguments) = outer_arguments(predicate, "all") {
        let parts = split_cfg_arguments(arguments)?;
        let mut unknown = false;
        for part in parts {
            match eval_cfg(part, active) {
                Some(false) => return Some(false),
                Some(true) => {}
                None => unknown = true,
            }
        }
        return (!unknown).then_some(true);
    }
    if let Some(arguments) = outer_arguments(predicate, "any") {
        let parts = split_cfg_arguments(arguments)?;
        let mut unknown = false;
        for part in parts {
            match eval_cfg(part, active) {
                Some(true) => return Some(true),
                Some(false) => {}
                None => unknown = true,
            }
        }
        return (!unknown).then_some(false);
    }
    if let Some(arguments) = outer_arguments(predicate, "not") {
        let parts = split_cfg_arguments(arguments)?;
        let [inner] = parts.as_slice() else {
            return None;
        };
        return eval_cfg(inner, active).map(|value| !value);
    }
    if predicate.is_empty()
        || predicate.contains('(')
        || predicate.contains(')')
        || predicate.contains(',')
    {
        None
    } else {
        Some(active.contains(predicate))
    }
}

fn normalize_cfg(value: &str) -> String {
    let mut normalized = String::with_capacity(value.len());
    let mut quoted = false;
    for character in value.chars() {
        if character == '"' {
            quoted = !quoted;
            normalized.push(character);
        } else if quoted || !character.is_whitespace() {
            normalized.push(character);
        }
    }
    normalized
}

fn outer_arguments<'a>(value: &'a str, name: &str) -> Option<&'a str> {
    value
        .strip_prefix(name)?
        .strip_prefix('(')?
        .strip_suffix(')')
}

fn split_cfg_arguments(value: &str) -> Option<Vec<&str>> {
    if value.is_empty() {
        return Some(Vec::new());
    }
    let mut parts = Vec::new();
    let mut depth = 0usize;
    let mut quoted = false;
    let mut start = 0usize;
    for (index, character) in value.char_indices() {
        match character {
            '"' => quoted = !quoted,
            '(' if !quoted => depth = depth.checked_add(1)?,
            ')' if !quoted => depth = depth.checked_sub(1)?,
            ',' if !quoted && depth == 0 => {
                parts.push(&value[start..index]);
                start = index + character.len_utf8();
            }
            _ => {}
        }
    }
    if quoted || depth != 0 {
        return None;
    }
    parts.push(&value[start..]);
    Some(parts)
}

/// Walk upward from `start` until a `Cargo.toml` is found.
fn find_manifest(start: &Path) -> Result<PathBuf> {
    if !start.exists() {
        bail!("path does not exist: {}", start.display());
    }

    let mut dir = if start.is_file() {
        start
            .parent()
            .ok_or_else(|| anyhow!("path has no parent: {}", start.display()))?
            .to_path_buf()
    } else {
        start.to_path_buf()
    };

    // Prefer a stable absolute base when possible.
    if let Ok(canon) = dir.canonicalize() {
        dir = canon;
    }

    loop {
        let candidate = dir.join("Cargo.toml");
        if candidate.is_file() {
            return Ok(candidate);
        }
        if !dir.pop() {
            bail!("Cargo.toml not found starting from {}", start.display());
        }
    }
}

fn run_cargo_metadata(manifest_path: &Path, options: &CargoOptions) -> Result<Value> {
    let mut cmd = Command::new("cargo");
    cmd.arg("metadata")
        .arg("--format-version")
        .arg("1")
        .arg("--manifest-path")
        .arg(manifest_path);

    if options.all_features {
        cmd.arg("--all-features");
    }
    if options.no_default_features {
        cmd.arg("--no-default-features");
    }
    if !options.features.is_empty() {
        cmd.arg("--features").arg(options.features.join(","));
    }
    if let Some(target) = options.target.as_deref() {
        cmd.arg("--filter-platform").arg(target);
    }

    let output = cmd.output().with_context(|| {
        format!(
            "failed to spawn cargo metadata for {}",
            manifest_path.display()
        )
    })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!(
            "cargo metadata failed for {} (status {}): {}",
            manifest_path.display(),
            output.status,
            stderr.trim()
        );
    }

    let stdout = String::from_utf8(output.stdout).context("cargo metadata stdout was not UTF-8")?;
    serde_json::from_str(&stdout).context("failed to parse cargo metadata JSON")
}

fn select_package<'a>(metadata: &'a Value, manifest_path: &Path) -> Result<&'a Value> {
    let packages = metadata
        .get("packages")
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow!("cargo metadata missing packages array"))?;

    let want = normalize_path(manifest_path);

    for package in packages {
        let Some(mp) = package.get("manifest_path").and_then(|v| v.as_str()) else {
            continue;
        };
        if normalize_path(Path::new(mp)) == want {
            return Ok(package);
        }
    }

    bail!(
        "no package in cargo metadata matched manifest {}",
        manifest_path.display()
    )
}

fn normalize_path(path: &Path) -> PathBuf {
    path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
}

fn path_from_json(obj: &Value, key: &str) -> Result<PathBuf> {
    let s = obj
        .get(key)
        .and_then(|v| v.as_str())
        .ok_or_else(|| anyhow!("missing string field `{key}`"))?;
    Ok(PathBuf::from(s))
}

fn path_from_root(metadata: &Value, key: &str) -> Result<PathBuf> {
    path_from_json(metadata, key).with_context(|| format!("cargo metadata missing `{key}`"))
}

fn parse_targets(package: &Value) -> Result<Vec<CargoTarget>> {
    let targets = package
        .get("targets")
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow!("package missing targets array"))?;

    let mut out = Vec::with_capacity(targets.len());
    for target in targets {
        let name = target
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow!("target missing name"))?
            .to_string();
        let kind = target
            .get("kind")
            .and_then(|v| v.as_array())
            .ok_or_else(|| anyhow!("target missing kind"))?
            .iter()
            .filter_map(|v| v.as_str().map(str::to_string))
            .collect::<Vec<_>>();
        let src_path = path_from_json(target, "src_path")?;
        out.push(CargoTarget {
            name,
            kind,
            src_path,
        });
    }
    Ok(out)
}

fn resolve_active_features(metadata: &Value, package_id: &str) -> Result<Vec<String>> {
    let resolve = metadata
        .get("resolve")
        .ok_or_else(|| anyhow!("cargo metadata missing resolve"))?;
    let nodes = resolve
        .get("nodes")
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow!("cargo metadata resolve missing nodes"))?;

    for node in nodes {
        let id = node.get("id").and_then(|v| v.as_str()).unwrap_or("");
        if id == package_id {
            let features = node
                .get("features")
                .and_then(|v| v.as_array())
                .ok_or_else(|| anyhow!("resolve node missing features for {package_id}"))?;
            return Ok(features
                .iter()
                .filter_map(|v| v.as_str().map(str::to_string))
                .collect());
        }
    }

    bail!("resolve node not found for package id {package_id}")
}

fn collect_candle_versions(
    metadata: &Value,
    selected_package_id: &str,
) -> Result<BTreeMap<String, String>> {
    let packages = metadata
        .get("packages")
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow!("cargo metadata missing packages array"))?;

    let mut versions: BTreeMap<String, std::collections::BTreeSet<String>> = BTreeMap::new();
    for package in packages {
        if package.get("id").and_then(Value::as_str) == Some(selected_package_id) {
            continue;
        }
        let name = match package.get("name").and_then(|v| v.as_str()) {
            Some(n) if n.starts_with("candle-") => n,
            _ => continue,
        };
        let version = package
            .get("version")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow!("package `{name}` missing version"))?;
        versions
            .entry(name.to_string())
            .or_default()
            .insert(version.to_string());
    }
    Ok(versions
        .into_iter()
        .map(|(name, versions)| (name, versions.into_iter().collect::<Vec<_>>().join(",")))
        .collect())
}

fn collect_rustc_cfgs(target: Option<&str>) -> Result<Vec<String>> {
    let mut cmd = Command::new("rustc");
    cmd.arg("--print").arg("cfg");
    if let Some(triple) = target {
        cmd.arg("--target").arg(triple);
    }

    let output = cmd.output().context("failed to spawn rustc --print cfg")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!(
            "rustc --print cfg failed (status {}): {}",
            output.status,
            stderr.trim()
        );
    }

    let stdout =
        String::from_utf8(output.stdout).context("rustc --print cfg stdout was not UTF-8")?;
    let mut cfgs = stdout
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .map(str::to_string)
        .collect::<Vec<_>>();
    cfgs.sort();
    cfgs.dedup();
    Ok(cfgs)
}