run-rs 0.6.33

Run a subset of Rust as an interpreted script
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
//! The `cargo check` gate for `rust check`. Not used when a script just runs. Results are cached
//! by source hash.

use hex::encode;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, SystemTime};

use anyhow::{Result, anyhow, bail};

use crate::loader::CrateDep;

fn cache_root() -> PathBuf {
    if let Some(dir) = std::env::var_os("XDG_CACHE_HOME") {
        return PathBuf::from(dir).join("rustscript");
    }
    if let Some(home) = std::env::var_os("HOME") {
        return PathBuf::from(home).join(".cache/rustscript");
    }
    // `dirs` knows the per user cache dir on `Windows` too. Don't fall back to `/tmp`,
    // `cmp` would exec a binary from a path any other user could create first.
    if let Some(dir) = dirs::cache_dir() {
        return dir.join("rustscript");
    }
    std::env::temp_dir().join("rustscript")
}

fn bin_cache() -> PathBuf {
    cache_root().join("bin")
}

/// Cache entries older than this are removed after every check and build.
const GC_MAX_AGE: Duration = Duration::from_hours(720);

fn touch(path: &Path) {
    let refreshed = File::options()
        .append(true)
        .open(path)
        .and_then(|f| f.set_modified(SystemTime::now()));
    if let Err(e) = refreshed {
        eprintln!(
            "rust: could not refresh cache stamp {}: {e}",
            path.display()
        );
    }
}

/// Last use is the `.checked` stamp, or the mirrored `Cargo.toml` if there is no stamp.
/// The shared `target` dir is never removed, rebuilding it is exactly what the cache avoids.
fn sweep() {
    sweep_root(&cache_root(), SystemTime::now());
}

fn sweep_root(root: &Path, now: SystemTime) {
    let entries = match std::fs::read_dir(root) {
        Ok(entries) => entries,
        Err(e) => {
            if e.kind() != ErrorKind::NotFound {
                eprintln!("rust: could not sweep cache {}: {e}", root.display());
            }
            return;
        }
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if entry.file_name() == "target" {
            continue;
        }
        if entry.file_name() == "bin" {
            sweep_bin(&path, now);
            continue;
        }
        if !path.is_dir() {
            continue;
        }
        let used = mtime(&path.join(".checked"))
            .or_else(|| mtime(&path.join("Cargo.toml")))
            .or_else(|| mtime(&path));
        if is_expired(used, now)
            && let Err(e) = std::fs::remove_dir_all(&path)
        {
            eprintln!(
                "rust: could not remove stale cache entry {}: {e}",
                path.display()
            );
        }
    }
}

fn sweep_bin(dir: &Path, now: SystemTime) {
    let entries = match std::fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(e) => {
            if e.kind() != ErrorKind::NotFound {
                eprintln!("rust: could not sweep cache {}: {e}", dir.display());
            }
            return;
        }
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if is_expired(mtime(&path), now)
            && let Err(e) = std::fs::remove_file(&path)
        {
            eprintln!(
                "rust: could not remove stale cache binary {}: {e}",
                path.display()
            );
        }
    }
}

fn mtime(path: &Path) -> Option<SystemTime> {
    std::fs::metadata(path).and_then(|m| m.modified()).ok()
}

/// Missing metadata or a clock that went backwards count as fresh. Only a known age is swept.
fn is_expired(used: Option<SystemTime>, now: SystemTime) -> bool {
    let Some(used) = used else { return false };
    now.duration_since(used).is_ok_and(|age| age > GC_MAX_AGE)
}

pub fn clean() -> Result<()> {
    let root = cache_root();
    if root.exists() {
        std::fs::remove_dir_all(&root)?;
        println!("cleared {}", root.display());
    } else {
        println!("nothing to clean");
    }
    Ok(())
}

/// `files` are mirrored into the cache project so `mod` resolves the same way. `crate_deps` are
/// added as path deps.
pub fn check(
    script_path: &Path,
    files: &[(PathBuf, String)],
    crate_deps: &[CrateDep],
) -> Result<()> {
    if std::env::var_os("RUSTSCRIPT_SKIP_CHECK").is_some() {
        return Ok(());
    }
    let hash = hash_files(files, crate_deps);
    let project = cache_root().join(hash.clone());
    let stamp = project.join(".checked");
    if stamp.exists() {
        touch(&stamp);
        sweep();
        return Ok(());
    }

    write_project(&project, files, crate_deps)?;

    // One shared target dir. Otherwise every source hash recompiles all deps and eats gigabytes.
    let output = Command::new("cargo")
        .args(["check", "--quiet"])
        .env("CARGO_TARGET_DIR", cache_root().join("target"))
        .current_dir(&project)
        .output();

    let output = match output {
        Ok(o) => o,
        Err(e) => bail!("could not run cargo check: {e}"),
    };

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!(
            "{} is not valid Rust:\n{}",
            script_path.display(),
            stderr.trim_end()
        );
    }

    std::fs::write(&stamp, "")?;
    sweep();
    Ok(())
}

fn write_project(
    project: &Path,
    files: &[(PathBuf, String)],
    crate_deps: &[CrateDep],
) -> Result<()> {
    std::fs::create_dir_all(project)?;
    let root = files.first().map(|(rel, _)| rel.as_path());
    std::fs::write(project.join("Cargo.toml"), manifest(root, crate_deps))?;
    for (rel, source) in files {
        let dst = project.join(rel);
        if let Some(parent) = dst.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(&dst, source)?;
    }
    Ok(())
}

/// A successful build also counts as a passed check. The binary is cached by source hash.
pub fn build(
    script_path: &Path,
    files: &[(PathBuf, String)],
    crate_deps: &[CrateDep],
) -> Result<PathBuf> {
    let hash = hash_files(files, crate_deps);
    let bin = bin_cache().join(format!("{hash}{}", std::env::consts::EXE_SUFFIX));
    if bin.exists() {
        touch(&bin);
        sweep();
        return Ok(bin);
    }

    let project = cache_root().join(hash.clone());
    write_project(&project, files, crate_deps)?;

    let target = cache_root().join("target");
    eprintln!("rust: compiling {}", script_path.display());
    let status = Command::new("cargo")
        .args(["build"])
        .env("CARGO_TARGET_DIR", &target)
        .current_dir(&project)
        .status();
    let status = match status {
        Ok(s) => s,
        Err(e) => bail!("could not run cargo build: {e}"),
    };
    if !status.success() {
        bail!("{} failed to compile", script_path.display());
    }

    let built = target
        .join("debug")
        .join(format!("script{}", std::env::consts::EXE_SUFFIX));
    std::fs::create_dir_all(bin_cache())?;
    // Copy to a temp path and rename, so a concurrent run never runs a half written binary.
    let tmp = bin_cache().join(format!(".{hash}.{}", std::process::id()));
    std::fs::copy(&built, &tmp)
        .map_err(|e| anyhow!("cannot copy built binary {}: {e}", built.display()))?;
    match std::fs::rename(&tmp, &bin) {
        Ok(()) => {}
        Err(e) => {
            // a concurrent build may have put the same binary there first
            if let Err(rm) = std::fs::remove_file(&tmp) {
                eprintln!("rust: could not remove temp binary {}: {rm}", tmp.display());
            }
            if !bin.exists() {
                return Err(anyhow!("cannot place binary {}: {e}", bin.display()));
            }
        }
    }
    sweep();
    Ok(bin)
}

/// Always there, a script can `use` them without declaring anything. Rendered by build.rs from
/// the `[dependencies]` of this crate, so the versions match the bridges.
const MANIFEST: &str = include_str!(concat!(env!("OUT_DIR"), "/script_manifest.toml"));

/// The empty `[workspace]` detaches the project from any workspace above the cache dir.
/// The bin is named after the script so diagnostics show the real name.
fn manifest(root: Option<&Path>, crate_deps: &[CrateDep]) -> String {
    let root = root.unwrap_or(Path::new("main.rs")).to_string_lossy();
    let mut out = format!(
        r#"[package]
name = "script"
version = "0.0.0"
edition = "2024"

[[bin]]
name = "script"
path = {root:?}

{MANIFEST}"#
    );
    for dep in crate_deps {
        let dir = dep.dir.to_string_lossy();
        // Explicit `[dependencies.name]` header. `MANIFEST` ends with the
        // `[target."cfg(windows)".dependencies]` table, a bare key would land there and make the
        // crate `Windows` only.
        out.push_str(&format!("\n[dependencies.{}]\npath = {dir:?}\n", dep.name));
    }
    out.push_str("\n[workspace]\n");
    out
}

/// The key covers `MANIFEST` and the compiler too, not only the sources. Otherwise after `rust update`
/// old stamps vouch for sources the new dep set may reject. Sha256 because `DefaultHasher` is not
/// stable across releases.
fn hash_files(files: &[(PathBuf, String)], crate_deps: &[CrateDep]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(MANIFEST.as_bytes());
    hasher.update(crate::build_info::version().as_bytes());
    for (rel, source) in files {
        hasher.update(rel.to_string_lossy().as_bytes());
        hasher.update(source.as_bytes());
    }
    // a grafted crate change must re-trigger the check
    for dep in crate_deps {
        hasher.update(dep.name.as_bytes());
        for (rel, source) in &dep.files {
            hasher.update(rel.to_string_lossy().as_bytes());
            hasher.update(source.as_bytes());
        }
    }
    // half the digest is still 128 bits and dir names stay readable
    encode(&hasher.finalize()[..16])
}

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

    /// The graft must not land after the `Windows` target table, `use shared::..` fails off
    /// `Windows` then.
    #[test]
    fn graft_dep_is_all_target_not_windows_only() {
        let dep = CrateDep {
            name: "shared".to_string(),
            dir: PathBuf::from("/tmp/shared"),
            files: Vec::new(),
        };
        let text = manifest(Some(Path::new("notes.rs")), &[dep]);
        let value: toml::Value = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("manifest must be valid TOML: {e}\n{text}"));

        let all_target = value
            .get("dependencies")
            .and_then(|d| d.get("shared"))
            .is_some();
        assert!(
            all_target,
            "shared must be an all-target dependency:\n{text}"
        );

        let windows_only = value
            .get("target")
            .and_then(|t| t.get("cfg(windows)"))
            .and_then(|c| c.get("dependencies"))
            .and_then(|d| d.get("shared"))
            .is_some();
        assert!(
            !windows_only,
            "shared must not be a Windows only dependency:\n{text}"
        );
    }

    fn source(name: &str, body: &str) -> Vec<(PathBuf, String)> {
        vec![(PathBuf::from(name), body.to_string())]
    }

    /// Anything that can change the cargo result belongs in the key.
    #[test]
    fn the_cache_key_separates_different_inputs() {
        let base = hash_files(&source("a.rs", "fn main() {}"), &[]);
        assert_eq!(base.len(), 32, "128 bits rendered as hex");
        assert!(base.chars().all(|c| c.is_ascii_hexdigit()));
        assert_eq!(base, hash_files(&source("a.rs", "fn main() {}"), &[]));

        assert_ne!(base, hash_files(&source("a.rs", "fn main() { }"), &[]));
        assert_ne!(base, hash_files(&source("b.rs", "fn main() {}"), &[]));

        let dep = CrateDep {
            name: "shared".to_string(),
            dir: PathBuf::from("/tmp/shared"),
            files: source("lib.rs", "pub fn helper() {}"),
        };
        let with_dep = hash_files(&source("a.rs", "fn main() {}"), &[dep]);
        assert_ne!(base, with_dep);

        // a grafted crate change must re-trigger the check
        let changed_dep = CrateDep {
            name: "shared".to_string(),
            dir: PathBuf::from("/tmp/shared"),
            files: source("lib.rs", "pub fn helper() -> u8 { 0 }"),
        };
        let with_changed = hash_files(&source("a.rs", "fn main() {}"), &[changed_dep]);
        assert_ne!(with_dep, with_changed);
    }

    fn set_mtime(path: &Path, to: SystemTime) {
        File::options()
            .append(true)
            .open(path)
            .unwrap()
            .set_modified(to)
            .unwrap();
    }

    fn project_entry(root: &Path, name: &str, stamped: bool, used: SystemTime) {
        let dir = root.join(name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("Cargo.toml"), "").unwrap();
        set_mtime(&dir.join("Cargo.toml"), used);
        if stamped {
            std::fs::write(dir.join(".checked"), "").unwrap();
            set_mtime(&dir.join(".checked"), used);
        }
    }

    #[test]
    fn sweep_removes_stale_entries_and_never_the_target_dir() {
        let root = tempfile::tempdir().unwrap();
        let root = root.path();
        let now = SystemTime::now();
        let old = now - GC_MAX_AGE - Duration::from_hours(24);

        project_entry(root, "stale", true, old);
        project_entry(root, "fresh", true, now);
        // a project without a stamp must still age out
        project_entry(root, "unstamped", false, old);
        std::fs::create_dir_all(root.join("target/debug")).unwrap();
        let bin = root.join("bin");
        std::fs::create_dir_all(&bin).unwrap();
        std::fs::write(bin.join("aaaa"), "x").unwrap();
        set_mtime(&bin.join("aaaa"), old);
        std::fs::write(bin.join("bbbb"), "x").unwrap();

        sweep_root(root, now);

        assert!(!root.join("stale").exists(), "stale project must go");
        assert!(
            !root.join("unstamped").exists(),
            "unstamped project must go"
        );
        assert!(root.join("fresh").exists(), "fresh project must stay");
        assert!(root.join("target/debug").exists(), "target must never go");
        assert!(!bin.join("aaaa").exists(), "stale binary must go");
        assert!(bin.join("bbbb").exists(), "fresh binary must stay");
    }

    /// Every script crate is in the manifest, and the whole thing is one valid cargo manifest.
    #[test]
    fn the_manifest_lists_every_script_crate() {
        let text = manifest(Some(Path::new("notes.rs")), &[]);
        let value: toml::Value = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("manifest must be valid TOML: {e}\n{text}"));
        let listed = |name: &str| {
            let plain = value
                .get("dependencies")
                .and_then(|d| d.get(name))
                .is_some();
            let windows = value
                .get("target")
                .and_then(|t| t.get("cfg(windows)"))
                .and_then(|c| c.get("dependencies"))
                .and_then(|d| d.get(name))
                .is_some();
            plain || windows
        };
        for name in crate::interpreter::coverage::SCRIPT_CRATES {
            let name = name.replace('_', "-");
            // `serde_json` is spelled with an underscore on crates.io too
            assert!(
                listed(&name) || listed(&name.replace('-', "_")),
                "`{name}` is missing from the script manifest:\n{text}"
            );
        }
        assert!(listed("reqwest") && listed("winreg"));
    }

    /// The diagnostic must point at `notes.rs`, not `main.rs`.
    #[test]
    fn manifest_bin_path_is_the_real_script_name() {
        let text = manifest(Some(Path::new("notes.rs")), &[]);
        let value: toml::Value = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("manifest must be valid TOML: {e}\n{text}"));
        let path = value
            .get("bin")
            .and_then(|b| b.get(0))
            .and_then(|b| b.get("path"))
            .and_then(|p| p.as_str());
        assert_eq!(path, Some("notes.rs"), "manifest was:\n{text}");
    }
}