alef 0.63.1

Opinionated polyglot binding generator for Rust libraries
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
//! Validation of user-supplied pipeline overrides in `alef.toml`.
//!
//! When a user provides an explicit `[lint.<lang>]` / `[test.<lang>]` /
//! `[build_commands.<lang>]` / `[setup.<lang>]` / `[update.<lang>]` /
//! `[clean.<lang>]` table that **sets a main command field**, that table
//! must also declare a `precondition`. The rationale:
//!
//! - Built-in defaults all declare a `command -v <tool>` precondition so
//!   pipelines degrade gracefully when the underlying tool is missing.
//! - Custom commands are opaque to alef — only the user knows what the
//!   command requires. Forcing an explicit `precondition` keeps the
//!   warn-and-skip behavior intact on systems that can't run the command.
//!
//! Tables that only customize `before` (without overriding the main command)
//! are exempt: the default precondition still applies via the surrounding
//! defaults logic.

mod preconditions;

use super::resolved::ResolvedCrateConfig;
use crate::core::error::AlefError;
use preconditions::{
    build_main_fields, clean_main_fields, lint_main_fields, setup_main_fields, test_main_fields, update_main_fields,
    validate_build_dependency_preconditions, validate_section, validate_test_e2e_precondition, validate_tools,
};

/// Validate user-supplied pipeline overrides in a resolved per-crate config.
///
/// Operates on the merged pipeline maps (already `HashMap` rather than
/// `Option<HashMap>`) that `ResolvedCrateConfig` carries after workspace
/// defaults are folded in.
pub fn validate_resolved(config: &ResolvedCrateConfig) -> Result<(), AlefError> {
    validate_tools(&config.tools)?;
    validate_package_metadata(config)?;
    validate_section("lint", &config.lint, lint_main_fields, |c| c.precondition.as_deref())?;
    validate_section("test", &config.test, test_main_fields, |c| c.precondition.as_deref())?;
    validate_test_e2e_precondition(&config.test)?;
    validate_section("build_commands", &config.build_commands, build_main_fields, |c| {
        c.precondition.as_deref()
    })?;
    validate_build_dependency_preconditions(&config.build_commands)?;
    validate_section("setup", &config.setup, setup_main_fields, |c| c.precondition.as_deref())?;
    validate_section("update", &config.update, update_main_fields, |c| {
        c.precondition.as_deref()
    })?;
    validate_section("clean", &config.clean, clean_main_fields, |c| c.precondition.as_deref())?;
    validate_trait_bridges(config)?;
    Ok(())
}

/// Reject a trait bridge that declares a registration function it cannot emit.
///
/// `registry_getter` is `Option` on the config struct, but the FFI backend's registration
/// emitter needs it and `expect`s it — so a bridge with `register_fn` and no `registry_getter`
/// passed validation, survived extraction, and panicked several stages later inside binding
/// generation, naming an internal function rather than the config key at fault. Checking it here
/// fails at load with the bridge named, before any file is written. ~keep
fn validate_trait_bridges(config: &ResolvedCrateConfig) -> Result<(), AlefError> {
    for bridge in &config.trait_bridges {
        if bridge.register_fn.is_some() && bridge.registry_getter.is_none() {
            return Err(AlefError::Config(format!(
                "trait bridge `{}` sets `register_fn` but no `registry_getter`. The generated \
                 registration function needs the registry accessor to install an implementation, \
                 so this pair cannot be emitted. Add `registry_getter` to \
                 `[[crates.trait_bridges]]` for `{}`, or drop `register_fn` if the bridge is not \
                 meant to be registerable.",
                bridge.trait_name, bridge.trait_name
            )));
        }
    }
    Ok(())
}

fn validate_package_metadata(config: &ResolvedCrateConfig) -> Result<(), AlefError> {
    const CRATES_IO_LIST_LIMIT: usize = 5;
    let Some(meta) = &config.package_metadata else {
        return Ok(());
    };
    if !meta.truncate_registry_lists {
        if meta.keywords.len() > CRATES_IO_LIST_LIMIT {
            return Err(AlefError::Config(format!(
                "crate `{}` package_metadata.keywords has {} entries; crates.io supports at most {CRATES_IO_LIST_LIMIT}. \
                 Reduce the list or set package_metadata.truncate_registry_lists = true.",
                config.name,
                meta.keywords.len()
            )));
        }
        if meta.categories.len() > CRATES_IO_LIST_LIMIT {
            return Err(AlefError::Config(format!(
                "crate `{}` package_metadata.categories has {} entries; crates.io supports at most {CRATES_IO_LIST_LIMIT}. \
                 Reduce the list or set package_metadata.truncate_registry_lists = true.",
                config.name,
                meta.categories.len()
            )));
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::config::new_config::NewAlefConfig;
    use tracing_test::traced_test;

    /// Parse a new-schema alef.toml and return the first resolved crate.
    fn resolve_first(toml_str: &str) -> ResolvedCrateConfig {
        let cfg: NewAlefConfig = toml::from_str(toml_str).expect("config should parse");
        cfg.resolve().expect("config should resolve").remove(0)
    }

    fn base_config() -> &'static str {
        r#"
[workspace]
languages = ["python"]

[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
"#
    }

    #[test]
    fn no_user_overrides_is_valid() {
        let config = resolve_first(base_config());
        validate_resolved(&config).expect("default config should validate");
    }

    #[test]
    fn lint_override_with_main_cmd_no_precondition_errors() {
        let toml = format!(
            "{base}\n[crates.lint.python]\nformat = \"black .\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("missing precondition should error");
        let msg = format!("{err}");
        assert!(msg.contains("[lint.python]"), "error should name the section: {msg}");
        assert!(msg.contains("precondition"), "error should mention precondition: {msg}");
    }

    #[test]
    fn lint_override_with_main_cmd_and_precondition_is_ok() {
        let toml = format!(
            "{base}\n[crates.lint.python]\nprecondition = \"command -v black\"\nformat = \"black .\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("config with precondition should validate");
    }

    #[test]
    fn lint_override_with_only_before_no_precondition_is_ok() {
        let toml = format!(
            "{base}\n[crates.lint.python]\nbefore = \"echo hi\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("table with only `before` should validate");
    }

    #[test]
    fn test_override_with_main_cmd_no_precondition_errors() {
        let toml = format!(
            "{base}\n[crates.test.python]\ncommand = \"pytest\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("missing precondition should error");
        assert!(format!("{err}").contains("[test.python]"));
    }

    #[test]
    fn test_override_with_only_e2e_requires_precondition() {
        let toml = format!(
            "{base}\n[crates.test.python]\ne2e = \"pytest tests/e2e\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("e2e without precondition or e2e_precondition should error");
        let msg = format!("{err}");
        assert!(msg.contains("[test.python]"), "{msg}");
        assert!(msg.contains("e2e_precondition"), "{msg}");
    }

    #[test]
    fn test_override_with_only_e2e_and_e2e_precondition_is_ok() {
        let toml = format!(
            "{base}\n[crates.test.python]\ne2e_precondition = \"command -v uv\"\ne2e = \"pytest tests/e2e\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("e2e with e2e_precondition alone should validate");
    }

    #[test]
    fn test_override_with_e2e_and_command_needs_only_top_level_precondition() {
        let toml = format!(
            "{base}\n[crates.test.python]\nprecondition = \"command -v pytest\"\ncommand = \"pytest\"\ne2e = \
             \"pytest tests/e2e\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config)
            .expect("a top-level precondition still satisfies both command and e2e when no e2e_precondition is set");
    }

    #[test]
    fn build_override_with_main_cmd_no_precondition_errors() {
        let toml = format!(
            "{base}\n[crates.build_commands.python]\nbuild = \"maturin develop\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("missing precondition should error");
        assert!(format!("{err}").contains("[build_commands.python]"));
    }

    #[test]
    fn build_dependency_precondition_without_remediation_errors() {
        let toml = format!(
            "{base}\n[crates.build_commands.python]\nprecondition = \"command -v maturin\"\n\
             build = \"maturin develop\"\ndependency_precondition = \"[ -d .venv ]\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("dependency check without remediation should error");
        let msg = format!("{err}");
        assert!(msg.contains("[build_commands.python]"), "{msg}");
        assert!(msg.contains("dependency_remediation"), "{msg}");
    }

    #[test]
    fn build_dependency_precondition_with_remediation_is_ok() {
        let toml = format!(
            "{base}\n[crates.build_commands.python]\nprecondition = \"command -v maturin\"\n\
             build = \"maturin develop\"\ndependency_precondition = \"[ -d .venv ]\"\n\
             dependency_remediation = \"uv venv\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("declared pair should validate");
    }

    /// A user's own build command replaces alef's, so the built-in dependency check written for
    /// alef's command must not survive to block it — `maturin build` needs no virtualenv at
    /// all. ~keep
    #[test]
    fn user_build_override_drops_the_builtin_dependency_precondition() {
        let toml = format!(
            "{base}\n[crates.build_commands.python]\nprecondition = \"command -v maturin\"\n\
             build = \"maturin build --out dist\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let effective = config.build_command_config_for_language(crate::core::config::Language::Python);

        assert_eq!(effective.dependency_precondition, None);
        assert_eq!(effective.dependency_remediation, None);
    }

    /// ...but a table that only adds a `before` hook keeps alef's default command, so it must
    /// keep the check that describes it. ~keep
    #[test]
    fn a_before_only_override_keeps_the_builtin_dependency_precondition() {
        let toml = format!(
            "{base}\n[crates.build_commands.python]\nbefore = \"echo hi\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let effective = config.build_command_config_for_language(crate::core::config::Language::Python);

        assert!(effective.dependency_precondition.is_some());
        assert!(effective.dependency_remediation.is_some());
    }

    #[test]
    fn setup_override_with_install_no_precondition_errors() {
        let toml = format!(
            "{base}\n[crates.setup.python]\ninstall = \"uv sync --no-install-project --no-install-workspace\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("setup install without precondition should error");
    }

    #[test]
    fn update_override_with_main_cmd_no_precondition_errors() {
        let toml = format!(
            "{base}\n[crates.update.python]\nupdate = \"uv sync --upgrade\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("update without precondition should error");
    }

    #[test]
    fn clean_override_with_main_cmd_no_precondition_errors() {
        let toml = format!(
            "{base}\n[crates.clean.python]\nclean = \"rm -rf dist\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("clean without precondition should error");
    }

    #[test]
    fn error_message_lists_only_actually_set_main_fields() {
        let toml = format!(
            "{base}\n[crates.lint.python]\nformat = \"black .\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let msg = format!("{}", validate_resolved(&config).unwrap_err());
        assert!(msg.contains("`format`"), "expected `format`, got: {msg}");
        assert!(!msg.contains("`check`"), "should not mention unset `check`: {msg}");
        assert!(
            !msg.contains("`typecheck`"),
            "should not mention unset `typecheck`: {msg}"
        );
    }

    #[test]
    fn before_plus_main_cmd_without_precondition_still_errors() {
        let toml = format!(
            "{base}\n[crates.lint.python]\nbefore = \"echo hi\"\nformat = \"black .\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("before + main without precondition must error");
    }

    #[test]
    fn malformed_python_package_manager_value_is_rejected() {
        let toml = format!(
            "{base}\n[workspace.tools]\npython_package_manager = \"uv; rm -rf /\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("non-identifier tool name must be rejected");
        assert!(format!("{err}").contains("well-formed"));
    }

    #[test]
    fn malformed_node_package_manager_value_is_rejected() {
        let toml = format!(
            "{base}\n[workspace.tools]\nnode_package_manager = \"pnpm$(echo bad)\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("non-identifier tool name must be rejected");
    }

    #[test]
    fn malformed_rust_dev_tool_entry_is_rejected() {
        let toml = format!(
            "{base}\n[workspace.tools]\nrust_dev_tools = [\"cargo-edit\", \"cargo`evil`\"]\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("non-identifier tool name must be rejected");
    }

    #[test]
    fn whitespace_in_tool_name_is_rejected() {
        let toml = format!(
            "{base}\n[workspace.tools]\npython_package_manager = \"uv \"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("trailing whitespace must be rejected");
    }

    #[test]
    fn empty_tool_name_is_rejected() {
        let toml = format!(
            "{base}\n[workspace.tools]\npython_package_manager = \"\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect_err("empty tool name must be rejected");
    }

    #[test]
    fn safe_tool_names_are_accepted() {
        let toml = format!(
            "{base}\n[workspace.tools]\npython_package_manager = \"uv\"\n\
             node_package_manager = \"pnpm\"\n\
             rust_dev_tools = [\"cargo-edit\", \"cargo_sort\", \"tool.v2\"]\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("normal tool names should validate");
    }

    #[test]
    fn package_metadata_keywords_over_crates_io_limit_errors() {
        let toml = format!(
            "{base}\n[crates.package_metadata]\nkeywords = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        let err = validate_resolved(&config).expect_err("too many crates.io keywords should error");
        let msg = format!("{err}");
        assert!(msg.contains("package_metadata.keywords"), "got: {msg}");
        assert!(msg.contains("at most 5"), "got: {msg}");
    }

    #[test]
    fn package_metadata_can_opt_into_registry_list_truncation() {
        let toml = format!(
            "{base}\n[crates.package_metadata]\n\
             truncate_registry_lists = true\n\
             keywords = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]\n\
             categories = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("explicit truncation opt-in should validate");
    }

    #[test]
    fn override_with_main_cmd_and_precondition_validates_for_each_section() {
        for (section, field, lang) in [
            ("lint", "format", "python"),
            ("test", "command", "python"),
            ("build_commands", "build", "python"),
            ("setup", "install", "python"),
            ("update", "update", "python"),
            ("clean", "clean", "python"),
        ] {
            let toml = format!(
                "{base}\n[crates.{section}.{lang}]\nprecondition = \"command -v tool\"\n{field} = \"tool run\"\n",
                base = base_config()
            );
            let config = resolve_first(&toml);
            validate_resolved(&config).unwrap_or_else(|e| panic!("[{section}] with precondition should validate: {e}"));
        }
    }

    #[traced_test]
    #[test]
    fn lint_verbatim_default_emits_warning() {
        use crate::core::config::extras::Language;
        use crate::core::config::lint_defaults;
        use crate::core::config::tools::LangContext;
        let config = resolve_first(base_config());
        let ctx = LangContext::default(&config.tools);
        let default = lint_defaults::default_lint_config(Language::Python, "packages/python", &ctx);
        let Some(fmt_cmd) = default.format.as_ref().map(|c| c.commands().join(" ")) else {
            return;
        };
        let toml = format!(
            "{base}\n[crates.lint.python]\nformat = {fmt_cmd:?}\n",
            base = base_config()
        );
        let _resolved = resolve_first(&toml);
    }

    #[traced_test]
    #[test]
    fn lint_all_custom_emits_no_warning() {
        let toml = format!(
            "{base}\n[crates.lint.python]\nprecondition = \"command -v custom\"\nformat = \"custom-fmt\"\n",
            base = base_config()
        );
        let config = resolve_first(&toml);
        validate_resolved(&config).expect("custom lint with precondition must validate");
        assert!(!logs_contain("matches the built-in default"));
    }

    #[traced_test]
    #[test]
    fn node_custom_value_no_warning() {
        let toml_str = r#"
[workspace]
languages = ["node"]

[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]

[crates.lint.node]
precondition = "command -v custom-linter"
check = "custom-linter src/"
"#;
        let config = resolve_first(toml_str);
        validate_resolved(&config).expect("custom node lint must validate");
        assert!(!logs_contain("matches the built-in default"));
    }
}