alef 0.34.12

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
use std::collections::{BTreeSet, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use tracing::info;

/// Returns `true` if the file at `path` contains an `alef:hash:` line in its
/// first 10 lines — the cryptographic marker that alef's finalizer appends.
///
/// Loose markers such as `"auto-generated by alef"` or `"DO NOT EDIT"` are
/// intentionally ignored: they appear in countless vendored files (cgo headers,
/// swig output, autoconf artefacts) that alef must never delete.
pub(crate) fn has_alef_hash(path: &Path) -> bool {
    let Ok(content) = fs::read_to_string(path) else {
        return false;
    };
    crate::core::hash::extract_hash(&content).is_some()
}

/// Returns `true` if the file at `path` is recognizably alef-owned even when
/// the cryptographic `alef:hash:` line is missing.
///
/// Backwards-compatibility safety net for files emitted by older alef versions
/// (or files whose hash line was stripped by a post-write tool). Detection
/// requires the specific phrase `auto-generated by alef` (case-sensitive) inside
/// the first ten lines. The looser `by alef` substring used previously
/// false-matched descriptive prose like `# Test apps are driven by alef` in
/// files that were never alef-emitted, causing accidental deletion. Tightening
/// to the full template phrase keeps cgo, swig, autoconf, bindgen, protobuf,
/// and arbitrary documentation prose safe — only alef's own `// This file is
/// auto-generated by alef. DO NOT EDIT.` header templates match.
pub(crate) fn has_alef_self_referential_header(path: &Path) -> bool {
    let Ok(content) = fs::read_to_string(path) else {
        return false;
    };
    content
        .lines()
        .take(10)
        .any(|line| line.contains("auto-generated by alef"))
}

/// Clean up orphan alef-generated files that are no longer in the current generation output.
///
/// Strategy: walk only the directories where the current run actually wrote files
/// (the parent dir of every entry in `current_gen_paths`). For each file in those
/// directories whose first lines contain an alef-generated header marker, if its
/// canonicalized absolute path is NOT in the current run's path set, delete it.
///
/// Walking only the parents of just-written files is what keeps the cleanup safe
/// when callers (e.g. `alef generate`) emit only a subset of categories: scaffold
/// dirs that the current run did not touch are never visited, so untouched files
/// in those dirs (e.g. user-customized package manifests) are preserved.
pub fn cleanup_orphaned_files(current_gen_paths: &HashSet<PathBuf>) -> anyhow::Result<usize> {
    if current_gen_paths.is_empty() {
        return Ok(0);
    }

    let normalized: HashSet<PathBuf> = current_gen_paths
        .iter()
        .map(|p| p.canonicalize().unwrap_or_else(|_| p.clone()))
        .collect();

    let touched_dirs: BTreeSet<PathBuf> = current_gen_paths
        .iter()
        .filter_map(|p| p.parent().map(|d| d.canonicalize().unwrap_or_else(|_| d.to_path_buf())))
        .collect();

    let mut removed_count = 0;
    let mut visited_dirs: HashSet<PathBuf> = HashSet::new();

    for dir in &touched_dirs {
        if !dir.exists() {
            continue;
        }
        let canonical_dir = dir.canonicalize().unwrap_or_else(|_| dir.clone());
        if !visited_dirs.insert(canonical_dir.clone()) {
            continue;
        }
        if has_dependency_cache_ancestor(&canonical_dir) {
            continue;
        }
        removed_count += cleanup_dir_recursive(&canonical_dir, &normalized, &touched_dirs)?;
    }

    Ok(removed_count)
}

/// Walk `dir` and remove orphan alef-generated files. Recurses into subdirectories
/// that are themselves touched, that contain a touched path, or that live beneath
/// a touched directory. The third clause catches orphans in subtrees a backend
/// previously owned but no longer writes to (e.g. the kotlin-android backend
/// dropped its `src/main/java/` Java DTO emit and left stale alef-marked Java
/// files behind). The `has_alef_hash` gate is the safety net that prevents
/// deletion of user-customised files and vendored artefacts.
/// Consumer dependency cache directory names. The cleanup walker must NEVER
/// descend into these because they contain alef-generated files that were
/// installed from registries (PyPI venv, Hex deps, npm node_modules, Composer
/// vendor, Bundler vendor, Rust target, etc.). Those files have legitimate
/// `alef:hash:` headers from the published package but are not part of the
/// current generator output — they belong to a downstream consumer's cache.
const DEPENDENCY_CACHE_DIRS: &[&str] = &[
    ".venv",
    "venv",
    "__pypackages__",
    "node_modules",
    "deps",
    "_build",
    "vendor",
    "target",
    ".cargo",
    "pkg",
    ".gradle",
    ".m2",
    ".cache",
    "test_apps",
];

/// True if `dir`'s own basename matches a dependency-cache name. Used inside
/// the recursive descent to short-circuit `node_modules/`, `vendor/`, etc.
fn is_consumer_dependency_dir(dir: &Path) -> bool {
    dir.file_name()
        .and_then(|n| n.to_str())
        .is_some_and(|name| DEPENDENCY_CACHE_DIRS.contains(&name))
}

/// True if any path component of `dir` matches a dependency-cache name. Used
/// at the touched-dirs entry point because `current_gen_paths.parent()` can
/// land on a path like `test_apps/python/tests/` whose basename (`tests`)
/// would otherwise sneak past `is_consumer_dependency_dir` and trigger
/// orphan-delete inside a protected subtree.
fn has_dependency_cache_ancestor(dir: &Path) -> bool {
    dir.components()
        .filter_map(|c| c.as_os_str().to_str())
        .any(|name| DEPENDENCY_CACHE_DIRS.contains(&name))
}

fn cleanup_dir_recursive(
    dir: &Path,
    normalized_gen_paths: &HashSet<PathBuf>,
    touched_dirs: &BTreeSet<PathBuf>,
) -> anyhow::Result<usize> {
    let mut removed_count = 0;
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            if is_consumer_dependency_dir(&path) {
                continue;
            }
            let canonical_sub = path.canonicalize().unwrap_or_else(|_| path.clone());
            let descend = touched_dirs
                .iter()
                .any(|td| td == &canonical_sub || td.starts_with(&canonical_sub) || canonical_sub.starts_with(td));
            if descend {
                removed_count += cleanup_dir_recursive(&path, normalized_gen_paths, touched_dirs)?;
            }
            continue;
        }

        //     (e.g. via `[crates.exclude]` or `#[cfg_attr(alef, alef(skip))]`).
        if !has_alef_hash(&path) && !has_alef_self_referential_header(&path) {
            continue;
        }

        let canonical_path = path.canonicalize().unwrap_or_else(|_| path.clone());
        if !normalized_gen_paths.contains(&canonical_path) {
            info!("Removing stale alef-generated file: {}", path.display());
            fs::remove_file(&path)?;
            removed_count += 1;
        }
    }

    Ok(removed_count)
}

#[cfg(test)]
mod tests {
    use super::{cleanup_orphaned_files, has_alef_hash};
    use std::collections::HashSet;
    use std::fs;

    /// A representative 64-char hex string used as a stand-in for a real alef hash
    /// in test fixtures. The actual hex value is irrelevant — only the presence of
    /// the `alef:hash:` prefix matters for ownership detection.
    const TEST_HASH: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

    #[test]
    fn cleanup_removes_orphan_with_alef_hash_header() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let package_dir = tempdir.path().join("packages/kotlin/src/main/kotlin/dev/demo");
        fs::create_dir_all(&package_dir).expect("create package dir");

        let current_file = package_dir.join("GraphQLRouteConfig.kt");
        let stale_file = package_dir.join("DefaultClient.kt");
        let alef_header = format!("// alef:hash:{TEST_HASH}\n\n");
        fs::write(&current_file, format!("{alef_header}class GraphQLRouteConfig\n")).expect("write current file");
        fs::write(&stale_file, format!("{alef_header}class DefaultClient\n")).expect("write stale file");

        let current_gen_paths = HashSet::from([current_file.clone()]);

        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 1);
        assert!(current_file.exists());
        assert!(!stale_file.exists());
    }

    /// Loose markers such as "Generated by alef" or "DO NOT EDIT" without the
    /// `alef:hash:` line must NOT trigger deletion — they appear in vendored cgo
    /// headers, swig output, and other consumer-managed files.
    #[test]
    fn cleanup_preserves_file_with_loose_marker_but_no_hash() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let package_dir = tempdir.path().join("packages/go/include");
        fs::create_dir_all(&package_dir).expect("create dir");

        let vendored = package_dir.join("sample_crawler.h");
        fs::write(
            &vendored,
            "// DO NOT EDIT — generated by cgo. See CGO_ENABLED.\n#ifndef SAMPLE_CRAWLER_H\n#define SAMPLE_CRAWLER_H\n#endif\n",
        )
        .expect("write vendored header");

        let alef_file = package_dir.join("bindings.go");
        fs::write(&alef_file, format!("// alef:hash:{TEST_HASH}\npackage main\n")).expect("write alef file");

        let current_gen_paths = HashSet::from([alef_file.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 0, "vendored file without alef:hash must not be deleted");
        assert!(vendored.exists(), "vendored cgo header must survive");
        assert!(alef_file.exists(), "current alef file must survive");
    }

    /// `has_alef_hash` must return true only when the `alef:hash:` line is present.
    #[test]
    fn has_alef_hash_detects_hash_line() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let with_hash = tempdir.path().join("with_hash.rs");
        let without_hash = tempdir.path().join("without_hash.rs");
        fs::write(&with_hash, format!("// alef:hash:{TEST_HASH}\nfn main() {{}}\n")).expect("write");
        fs::write(
            &without_hash,
            "// auto-generated by alef\n// DO NOT EDIT\nfn main() {}\n",
        )
        .expect("write");

        assert!(has_alef_hash(&with_hash), "must detect alef:hash: line");
        assert!(!has_alef_hash(&without_hash), "must not match loose markers");
    }

    /// Regression: stale alef-emitted files from older versions (or files whose
    /// hash line was stripped by a downstream tool) must still be cleanable.
    /// Detection requires BOTH the alef-generated comment AND a self-referential
    /// `alef ...` regenerate command — together they uniquely identify alef-
    /// emitted output without false-matching cgo/swig/autoconf artefacts.
    #[test]
    fn cleanup_removes_stale_file_with_alef_header_but_no_hash() {
        use super::has_alef_self_referential_header;

        let tempdir = tempfile::tempdir().expect("tempdir");
        let java_dir = tempdir.path().join("packages/java/dev/demo");
        fs::create_dir_all(&java_dir).expect("create dir");

        let stale = java_dir.join("FixedDelayHedge.java");
        fs::write(
            &stale,
            "// This file is auto-generated by alef — DO NOT EDIT.\n\
             // To regenerate: alef generate\n\
             // To verify freshness: alef verify --exit-code\n\
             package dev.demo;\npublic class FixedDelayHedge {}\n",
        )
        .expect("write stale file");

        let fresh = java_dir.join("AssistantMessage.java");
        fs::write(
            &fresh,
            format!("// auto-generated by alef\n// alef:hash:{TEST_HASH}\npackage dev.demo;\npublic class AssistantMessage {{}}\n"),
        )
        .expect("write fresh file");

        assert!(has_alef_self_referential_header(&stale));
        assert!(!has_alef_self_referential_header(
            java_dir.parent().unwrap().parent().unwrap()
        ));

        let current_gen_paths = HashSet::from([fresh.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 1, "stale file with alef header must be removed");
        assert!(!stale.exists(), "stale java file should be deleted");
        assert!(fresh.exists(), "fresh java file must survive");
    }

    /// Conversely: a vendored cgo/swig header that mentions "auto-generated by"
    /// but does NOT reference an `alef` command must still survive — the
    /// self-referential check is what keeps the two paths distinguishable.
    #[test]
    fn cleanup_preserves_cgo_style_header_without_alef_command() {
        use super::has_alef_self_referential_header;

        let tempdir = tempfile::tempdir().expect("tempdir");
        let include_dir = tempdir.path().join("packages/go/include");
        fs::create_dir_all(&include_dir).expect("create dir");

        let cgo = include_dir.join("auto.h");
        fs::write(
            &cgo,
            "// auto-generated by autoconf, DO NOT EDIT\n#ifndef AUTO_H\n#define AUTO_H\n#endif\n",
        )
        .expect("write cgo header");

        let alef_file = include_dir.join("bindings.go");
        fs::write(&alef_file, format!("// alef:hash:{TEST_HASH}\npackage main\n")).expect("write");

        assert!(
            !has_alef_self_referential_header(&cgo),
            "cgo header without alef command must NOT register as alef-owned"
        );

        let current_gen_paths = HashSet::from([alef_file.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 0, "cgo header must survive");
        assert!(cgo.exists(), "cgo header preserved");
        assert!(alef_file.exists(), "fresh alef file preserved");
    }

    /// Regression: descriptive prose mentioning alef in the header (e.g.
    /// `# Test apps are driven by alef`) must NOT register as alef-owned.
    /// Before tightening, the loose `by alef` substring matched any line
    /// containing "by alef" — including `.task/test-apps.yml`'s descriptive
    /// header — and alef's cleanup pass deleted it on every regen, breaking
    /// Taskfile.yml's include of the file.
    #[test]
    fn cleanup_preserves_file_with_descriptive_prose_mentioning_alef() {
        use super::has_alef_self_referential_header;

        let tempdir = tempfile::tempdir().expect("tempdir");
        let task_dir = tempdir.path().join(".task");
        fs::create_dir_all(&task_dir).expect("create .task dir");

        let descriptive = task_dir.join("test-apps.yml");
        fs::write(
            &descriptive,
            "version: \"3\"\n\
             \n\
             # Test apps are driven by alef (`alef test-apps run`). The per-language\n\
             # commands live in alef.toml; a single alef invocation exercises every\n\
             # published binding consistently. Homebrew is run separately because\n\
             # alef does not yet manage the brew tap.\n\
             \n\
             tasks:\n  all:\n    cmds:\n      - echo hi\n",
        )
        .expect("write descriptive file");

        let alef_file = task_dir.join("c.yml");
        fs::write(&alef_file, format!("# alef:hash:{TEST_HASH}\nversion: \"3\"\n")).expect("write alef file");

        assert!(
            !has_alef_self_referential_header(&descriptive),
            "descriptive prose mentioning alef must NOT register as alef-owned"
        );

        let current_gen_paths = HashSet::from([alef_file.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 0, "descriptive yaml file must survive");
        assert!(descriptive.exists(), "descriptive file preserved");
        assert!(alef_file.exists(), "fresh alef file preserved");
    }

    /// Regression: orphans in a sibling subtree of a touched directory must be
    /// swept. This models the kotlin-android case where the backend wrote
    /// Java DTOs into `src/main/java/` in older versions, then dropped that
    /// emit but kept writing Kotlin to `src/main/kotlin/`. The stale Java
    /// orphans live in a subtree the current run never writes to, but they
    /// are descendants of the package root that IS touched (via
    /// `build.gradle.kts` etc.).
    #[test]
    fn cleanup_removes_orphan_in_sibling_subtree_of_touched_dir() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let package_root = tempdir.path().join("packages/kotlin-android");
        let kotlin_dir = package_root.join("src/main/kotlin/dev/demo/android");
        let java_dir = package_root.join("src/main/java/dev/demo");
        fs::create_dir_all(&kotlin_dir).expect("create kotlin dir");
        fs::create_dir_all(&java_dir).expect("create java dir");

        let alef_header = format!("// alef:hash:{TEST_HASH}\n");
        let build_gradle = package_root.join("build.gradle.kts");
        let bridge_kt = kotlin_dir.join("DemoBridge.kt");
        let stale_java = java_dir.join("CrawlEngineHandle.java");
        let user_java = java_dir.join("UserCode.java");
        fs::write(&build_gradle, format!("{alef_header}plugins {{}}\n")).expect("write build.gradle.kts");
        fs::write(&bridge_kt, format!("{alef_header}object DemoBridge\n")).expect("write bridge.kt");
        fs::write(
            &stale_java,
            format!("{alef_header}public class CrawlEngineHandle {{}}\n"),
        )
        .expect("write stale java");
        fs::write(&user_java, "// hand-written\npublic class UserCode {}\n").expect("write user java");

        let current_gen_paths = HashSet::from([build_gradle.clone(), bridge_kt.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 1, "exactly the alef-marked orphan must be removed");
        assert!(build_gradle.exists(), "current build.gradle.kts must survive");
        assert!(bridge_kt.exists(), "current bridge.kt must survive");
        assert!(!stale_java.exists(), "stale java orphan must be removed");
        assert!(user_java.exists(), "user-written java must survive (no alef hash)");
    }

    /// Regression: when the current run writes some files into a `test_apps/`
    /// subtree (registry-mode e2e generation), unrelated alef-marked files in
    /// the same `test_apps/` tree from previous runs must NOT be swept.
    /// Downstream polyglot repos hold curated test projects under
    /// `test_apps/<lang>/`; partial regeneration must not wipe co-located
    /// curated scaffolding (composer.json, build.gradle.kts, pyproject.toml)
    /// just because the current iteration emits a different subset.
    #[test]
    fn cleanup_skips_test_apps_subtree_unconditionally() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let test_apps_python = tempdir.path().join("test_apps/python");
        let test_apps_python_tests = test_apps_python.join("tests");
        fs::create_dir_all(&test_apps_python_tests).expect("create test_apps tests dir");

        let alef_header = format!("// alef:hash:{TEST_HASH}\n");
        let regen_test = test_apps_python_tests.join("test_smoke.py");
        fs::write(&regen_test, format!("{alef_header}def test_smoke(): pass\n")).expect("write regen");
        let stale_sibling = test_apps_python_tests.join("test_legacy.py");
        fs::write(&stale_sibling, format!("{alef_header}def test_legacy(): pass\n")).expect("write stale sibling");

        let current_gen_paths = HashSet::from([regen_test.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 0, "test_apps subtree must be skipped by the walker");
        assert!(regen_test.exists(), "regen test survives");
        assert!(
            stale_sibling.exists(),
            "stale sibling must survive despite alef hash header"
        );
    }

    /// Regression: renamed files in the same directory must be cleaned up.
    /// This models the Swift backend's rename of `SwiftPluginHelpers.swift`
    /// to `ZSwiftPluginHelpers.swift` (Z-prefix anti-collision pattern).
    /// When the new file is emitted, the old file must be deleted.
    #[test]
    fn cleanup_removes_renamed_file_in_same_directory() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let rust_bridge_dir = tempdir.path().join("packages/swift/Sources/RustBridge");
        fs::create_dir_all(&rust_bridge_dir).expect("create rust bridge dir");

        let alef_header = "// Generated by alef. Do not edit by hand.\n".to_string();
        let old_name = rust_bridge_dir.join("SwiftPluginHelpers.swift");
        let new_name = rust_bridge_dir.join("ZSwiftPluginHelpers.swift");

        fs::write(
            &old_name,
            format!("{alef_header}// alef:hash:{TEST_HASH}\n\nenum InboundEnvelope\n"),
        )
        .expect("write old file");

        let new_content = format!("{alef_header}// alef:hash:{TEST_HASH}\n\nenum InboundEnvelope\n");
        fs::write(&new_name, new_content).expect("write new file");

        let current_gen_paths = HashSet::from([new_name.clone()]);
        let removed = cleanup_orphaned_files(&current_gen_paths).expect("cleanup");

        assert_eq!(removed, 1, "exactly the old file must be removed");
        assert!(!old_name.exists(), "old SwiftPluginHelpers.swift must be removed");
        assert!(new_name.exists(), "new ZSwiftPluginHelpers.swift must survive");
    }
}