pathlint 0.0.24

Lint the PATH environment variable against declarative ordering rules.
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
// The shape types below mirror src/config.rs only to validate
// the input TOML's structure. Many fields are never read after
// deserialization succeeds — the side effect (reject mismatched
// shape) is the whole point. Silence dead_code warnings rather
// than uglify the types with `#[allow(dead_code)]` per field.
#![allow(dead_code)]

//! Concatenate the per-package-manager plugin TOML files in
//! `plugins/` into a single `embedded_catalog.toml` placed in
//! `OUT_DIR`, and emit the path so `src/catalog.rs` can
//! `include_str!` it.
//!
//! The order is `catalog_version` line + each plugin in the
//! order listed in `plugins/_index.toml`. Plugin files are
//! concatenated verbatim (already-written comments survive).
//!
//! As of 0.0.12 every plugin file is parsed through `toml` +
//! `serde::Deserialize` against a build-script-local copy of the
//! Config / Relation / SourceDef shape. Shape mismatches (an
//! unknown field, a typo'd relation kind, a relation pointing at
//! a source no plugin defines) become build errors, not runtime
//! errors. The shape types here are intentionally a duplicate of
//! `src/config.rs` because build scripts cannot depend on the
//! crate they are building. When a relation kind or field is
//! added to `src/config.rs`, mirror the change here.
//!
//! Run `cargo test --workspace` to also exercise the
//! `#[cfg(test)] mod tests` block at the bottom of this file —
//! those tests pin the shape-checking behaviour without needing
//! the rest of the crate to build.

use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;

fn main() {
    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let plugins_dir = manifest_dir.join("plugins");
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let out_file = out_dir.join("embedded_catalog.toml");

    println!("cargo:rerun-if-changed=plugins");

    let index_path = plugins_dir.join("_index.toml");
    let index_text = fs::read_to_string(&index_path)
        .unwrap_or_else(|e| panic!("could not read {}: {e}", index_path.display()));

    let index: IndexFile = toml::from_str(&index_text).unwrap_or_else(|e| {
        panic!(
            "plugins/_index.toml failed shape check: {e}\n\
             expected `catalog_version = <int>` and `plugins = [\"name\", ...]`"
        )
    });

    // Parse + shape-check every plugin file before emitting the
    // concatenated catalog. We hold on to the parsed plugins so the
    // referential-integrity check can see every defined source
    // name in one pass.
    let mut plugins: Vec<(String, PluginFile, String)> = Vec::with_capacity(index.plugins.len());
    for name in &index.plugins {
        let path = plugins_dir.join(format!("{name}.toml"));
        let body = fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("could not read {}: {e}", path.display()));
        let parsed: PluginFile = toml::from_str(&body).unwrap_or_else(|e| {
            panic!(
                "plugins/{name}.toml failed shape check: {e}\n\
                 (build.rs validates plugin TOML against the same shape src/config.rs uses)"
            )
        });
        plugins.push((name.clone(), parsed, body));
    }

    if let Err(violations) = check_referential_integrity(&plugins) {
        panic!(
            "plugin catalog failed referential integrity ({} violations):\n  - {}\n\n\
             every relation must point at a source defined by some plugin file",
            violations.len(),
            violations.join("\n  - ")
        );
    }

    let mut buf = String::new();
    buf.push_str("# Generated by build.rs from plugins/*.toml. Edit those.\n");
    buf.push_str(&format!("catalog_version = {}\n\n", index.catalog_version));

    for (name, _parsed, body) in &plugins {
        buf.push_str(&format!("# ---- plugin: {name} ----\n\n"));
        buf.push_str(body);
        if !body.ends_with('\n') {
            buf.push('\n');
        }
        buf.push('\n');
    }

    write_if_changed(&out_file, &buf)
        .unwrap_or_else(|e| panic!("could not write {}: {e}", out_file.display()));
}

/// Write `contents` to `path` only when the existing file differs.
/// Cargo re-runs the build script anyway, but this keeps the
/// generated file's mtime stable across no-op rebuilds (so
/// downstream incremental compilation does not invalidate).
fn write_if_changed(path: &Path, contents: &str) -> std::io::Result<()> {
    if let Ok(existing) = fs::read_to_string(path) {
        if existing == contents {
            return Ok(());
        }
    }
    fs::write(path, contents)
}

/// `plugins/_index.toml` shape.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct IndexFile {
    catalog_version: u32,
    plugins: Vec<String>,
}

/// Per-plugin TOML shape — mirrors `pathlint::catalog::PluginFileShape`
/// (and `src/config.rs::SourceDef` / `Relation` for the nested
/// payloads) enough to catch typos and unknown fields at build
/// time. Intentional duplication: build scripts cannot depend on
/// the crate they're building, so the same shape is declared
/// twice. When adding a Relation kind or SourceDef field, update:
///
///   1. `src/config.rs::Relation` / `SourceDef` (runtime)
///   2. This `PluginFile` / `PluginRelation` / `PluginSourceDef`
///      (build-time validation)
///   3. `pathlint::catalog::PluginFileShape` (test-time gate via
///      tests/plugin_validation.rs)
///
/// Forgetting any of the three breaks `cargo test` for plugin
/// authors. See module docs for the rationale.
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct PluginFile {
    #[serde(default, rename = "source")]
    sources: BTreeMap<String, PluginSourceDef>,
    #[serde(default, rename = "relation")]
    relations: Vec<PluginRelation>,
}

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct PluginSourceDef {
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    windows: Option<String>,
    #[serde(default)]
    macos: Option<String>,
    #[serde(default)]
    linux: Option<String>,
    #[serde(default)]
    termux: Option<String>,
    #[serde(default)]
    unix: Option<String>,
    #[serde(default)]
    uninstall_command: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
enum PluginRelation {
    AliasOf {
        parent: String,
        children: Vec<String>,
    },
    ConflictsWhenBothInPath {
        sources: Vec<String>,
        diagnostic: String,
    },
    ServedByVia {
        host: String,
        guest_pattern: String,
        guest_provider: String,
        #[serde(default)]
        installer_token: Option<String>,
    },
    DependsOn {
        source: String,
        target: String,
    },
    PreferOrderOver {
        earlier: String,
        later: String,
    },
}

/// Build-time referential-integrity check: every relation field
/// that names a source must refer to a source actually defined
/// by some plugin file. A typo'd `host = "mise_install"` (missing
/// `s`) gets caught here instead of failing silently at runtime.
///
/// Pure: `plugins` is the deserialized catalog; the only output
/// is a `Result<(), Vec<String>>` listing every violation found.
/// Aggregating the list (rather than bailing on the first) lets a
/// plugin author see all dangling references in a single
/// `cargo build` cycle instead of fix-rebuild-fix-rebuild.
fn check_referential_integrity(
    plugins: &[(String, PluginFile, String)],
) -> Result<(), Vec<String>> {
    let mut all_sources: BTreeSet<&str> = BTreeSet::new();
    for (_name, plugin, _body) in plugins {
        for src_name in plugin.sources.keys() {
            all_sources.insert(src_name.as_str());
        }
    }

    let mut violations: Vec<String> = Vec::new();
    for (plugin_name, plugin, _body) in plugins {
        for rel in &plugin.relations {
            let referenced = relation_source_refs(rel);
            for r in referenced {
                if !all_sources.contains(r) {
                    violations.push(format!(
                        "{plugin_name}.toml: relation references undefined source `{r}`"
                    ));
                }
            }
        }
    }

    if violations.is_empty() {
        Ok(())
    } else {
        Err(violations)
    }
}

/// Every source name that a relation references. Pure helper for
/// `check_referential_integrity`. Returning &str ties the lifetime
/// to the relation; the caller uses the values immediately, no
/// allocation needed.
fn relation_source_refs(rel: &PluginRelation) -> Vec<&str> {
    match rel {
        PluginRelation::AliasOf { parent, children } => {
            let mut out = vec![parent.as_str()];
            out.extend(children.iter().map(|c| c.as_str()));
            out
        }
        PluginRelation::ConflictsWhenBothInPath { sources, .. } => {
            sources.iter().map(|s| s.as_str()).collect()
        }
        PluginRelation::ServedByVia {
            host,
            guest_provider,
            ..
        } => vec![host.as_str(), guest_provider.as_str()],
        PluginRelation::DependsOn { source, target } => vec![source.as_str(), target.as_str()],
        PluginRelation::PreferOrderOver { earlier, later } => {
            vec![earlier.as_str(), later.as_str()]
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_plugin(text: &str) -> Result<PluginFile, toml::de::Error> {
        toml::from_str(text)
    }

    #[test]
    fn empty_plugin_parses_to_empty_collections() {
        let p = parse_plugin("").unwrap();
        assert!(p.sources.is_empty());
        assert!(p.relations.is_empty());
    }

    #[test]
    fn shape_check_rejects_unknown_top_level_field() {
        let err = parse_plugin("unknown_top = 1").unwrap_err();
        assert!(
            err.to_string().contains("unknown"),
            "expected unknown-field error, got: {err}"
        );
    }

    #[test]
    fn shape_check_rejects_unknown_source_field() {
        let err = parse_plugin(
            r#"
[source.x]
unix = "/foo/bin"
typo_field = "oops"
"#,
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("typo_field"),
            "expected unknown-field error, got: {err}"
        );
    }

    #[test]
    fn shape_check_rejects_unknown_relation_kind() {
        let err = parse_plugin(
            r#"
[[relation]]
kind = "made_up_kind"
foo = "bar"
"#,
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("made_up_kind"),
            "expected unknown-variant error, got: {err}"
        );
    }

    #[test]
    fn referential_integrity_passes_when_every_relation_target_exists() {
        let plugin = parse_plugin(
            r#"
[source.host]
unix = "/h"

[source.guest]
unix = "/g"

[[relation]]
kind = "served_by_via"
host = "host"
guest_pattern = "x-*"
guest_provider = "guest"
"#,
        )
        .unwrap();
        let plugins = vec![("p".to_string(), plugin, String::new())];
        assert!(check_referential_integrity(&plugins).is_ok());
    }

    #[test]
    fn referential_integrity_rejects_dangling_relation_target() {
        let plugin = parse_plugin(
            r#"
[source.host]
unix = "/h"

[[relation]]
kind = "served_by_via"
host = "host"
guest_pattern = "x-*"
guest_provider = "missing_guest"
"#,
        )
        .unwrap();
        let plugins = vec![("p".to_string(), plugin, String::new())];
        let violations = check_referential_integrity(&plugins).unwrap_err();
        assert!(
            violations.iter().any(|v| v.contains("missing_guest")),
            "expected the dangling source name in the violations: {violations:?}"
        );
    }

    #[test]
    fn referential_integrity_lists_every_violation() {
        // 0.0.14: aggregate, don't bail at first. A plugin that
        // declares two dangling references should produce two
        // violation strings so the author can fix both at once.
        let plugin = parse_plugin(
            r#"
[source.host]
unix = "/h"

[[relation]]
kind = "served_by_via"
host = "host"
guest_pattern = "x-*"
guest_provider = "missing_one"

[[relation]]
kind = "depends_on"
source = "host"
target = "missing_two"
"#,
        )
        .unwrap();
        let plugins = vec![("p".to_string(), plugin, String::new())];
        let violations = check_referential_integrity(&plugins).unwrap_err();
        assert_eq!(violations.len(), 2);
        assert!(violations.iter().any(|v| v.contains("missing_one")));
        assert!(violations.iter().any(|v| v.contains("missing_two")));
    }

    #[test]
    fn referential_integrity_sees_sources_across_plugins() {
        // Plugin A defines the host, plugin B uses it via a
        // relation. The integrity check pools every plugin's
        // sources before validating relations.
        let plugin_a = parse_plugin(
            r#"
[source.host]
unix = "/h"
"#,
        )
        .unwrap();
        let plugin_b = parse_plugin(
            r#"
[source.guest]
unix = "/g"

[[relation]]
kind = "served_by_via"
host = "host"
guest_pattern = "x-*"
guest_provider = "guest"
"#,
        )
        .unwrap();
        let plugins = vec![
            ("a".to_string(), plugin_a, String::new()),
            ("b".to_string(), plugin_b, String::new()),
        ];
        assert!(check_referential_integrity(&plugins).is_ok());
    }
}