ferrflow 7.17.0

Universal semantic versioning for monorepos and classic repos
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
use std::path::{Path, PathBuf};

use super::join_within_repo;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Manifest {
    CargoToml,
    PackageJson,
    PyprojectToml,
    Gemfile,
    MixExs,
}

impl Manifest {
    pub fn from_manifest_filename(filename: &str) -> Option<Self> {
        match filename {
            "Cargo.toml" => Some(Self::CargoToml),
            "package.json" => Some(Self::PackageJson),
            "pyproject.toml" => Some(Self::PyprojectToml),
            "Gemfile" => Some(Self::Gemfile),
            "mix.exs" => Some(Self::MixExs),
            _ => None,
        }
    }

    fn lockfiles(self) -> &'static [Lockfile] {
        match self {
            Self::CargoToml => &[Lockfile {
                filename: "Cargo.lock",
                program: "cargo",
                base_args: &["update"],
                update_kind: UpdateKind::PerPackage,
            }],
            Self::PackageJson => &[
                Lockfile {
                    filename: "package-lock.json",
                    program: "npm",
                    base_args: &["install", "--package-lock-only"],
                    update_kind: UpdateKind::Whole,
                },
                Lockfile {
                    filename: "pnpm-lock.yaml",
                    program: "pnpm",
                    base_args: &["install", "--lockfile-only"],
                    update_kind: UpdateKind::Whole,
                },
                Lockfile {
                    filename: "yarn.lock",
                    program: "yarn",
                    base_args: &["install", "--mode=update-lockfile"],
                    update_kind: UpdateKind::Whole,
                },
            ],
            Self::PyprojectToml => &[
                Lockfile {
                    filename: "poetry.lock",
                    program: "poetry",
                    base_args: &["lock", "--no-update"],
                    update_kind: UpdateKind::Whole,
                },
                Lockfile {
                    filename: "uv.lock",
                    program: "uv",
                    base_args: &["lock", "--offline"],
                    update_kind: UpdateKind::Whole,
                },
            ],
            Self::Gemfile => &[Lockfile {
                filename: "Gemfile.lock",
                program: "bundle",
                base_args: &["lock"],
                update_kind: UpdateKind::Whole,
            }],
            Self::MixExs => &[Lockfile {
                filename: "mix.lock",
                program: "mix",
                base_args: &["deps.get"],
                update_kind: UpdateKind::Whole,
            }],
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum UpdateKind {
    PerPackage,
    Whole,
}

#[derive(Debug, Clone, Copy)]
struct Lockfile {
    filename: &'static str,
    program: &'static str,
    base_args: &'static [&'static str],
    update_kind: UpdateKind,
}

#[derive(Debug, PartialEq, Eq)]
pub enum UpdateOutcome {
    Updated { lockfile_rel: String },
    NoLockfile,
    NotOnPath { program: String },
    UnsupportedManifest,
    Failed { program: String, detail: String },
}

pub fn update_for_manifest(repo_root: &Path, manifest_rel: &str) -> anyhow::Result<UpdateOutcome> {
    let manifest_path = join_within_repo(repo_root, manifest_rel)?;
    let filename = match manifest_path.file_name().and_then(|n| n.to_str()) {
        Some(name) => name,
        None => return Ok(UpdateOutcome::UnsupportedManifest),
    };
    let Some(manifest) = Manifest::from_manifest_filename(filename) else {
        return Ok(UpdateOutcome::UnsupportedManifest);
    };

    let manifest_dir = manifest_path.parent().unwrap_or(repo_root);

    for lockfile in manifest.lockfiles() {
        let Some(lockfile_path) = locate_lockfile(repo_root, manifest_dir, lockfile.filename)
        else {
            continue;
        };
        let per_package = match lockfile.update_kind {
            UpdateKind::PerPackage => match manifest_package_name(&manifest_path) {
                Some(name) => Some(name),
                None => return Ok(UpdateOutcome::UnsupportedManifest),
            },
            UpdateKind::Whole => None,
        };
        return Ok(run_update(
            repo_root,
            &lockfile_path,
            lockfile,
            per_package.as_deref(),
        ));
    }

    Ok(UpdateOutcome::NoLockfile)
}

/// What a lockfile says about the version of the package that owns it.
pub enum LockfileState {
    /// No lockfile alongside the manifest, or a manifest kind we do not model.
    None,
    /// A lockfile is there but does not record the owning package's own
    /// version, so there is nothing to compare. pnpm and poetry are like this.
    NoVersionRecorded {
        lockfile_rel: String,
    },
    Agrees {
        lockfile_rel: String,
    },
    Drifted {
        lockfile_rel: String,
        recorded: String,
    },
}

/// What the lockfile beside `manifest_rel` says about that package's version.
///
/// Read-only counterpart to [`update_for_manifest`]: it locates the lockfile
/// the same way but runs nothing, so it is safe on a machine without the
/// package manager installed and without registry access.
pub fn inspect_for_manifest(
    repo_root: &Path,
    manifest_rel: &str,
    manifest_version: &str,
) -> LockfileState {
    let Ok(manifest_path) = join_within_repo(repo_root, manifest_rel) else {
        return LockfileState::None;
    };
    let Some(filename) = manifest_path.file_name().and_then(|n| n.to_str()) else {
        return LockfileState::None;
    };
    let Some(manifest) = Manifest::from_manifest_filename(filename) else {
        return LockfileState::None;
    };
    let manifest_dir = manifest_path.parent().unwrap_or(repo_root);

    for lockfile in manifest.lockfiles() {
        let Some(lockfile_path) = locate_lockfile(repo_root, manifest_dir, lockfile.filename)
        else {
            continue;
        };
        let lockfile_rel = lockfile_path
            .strip_prefix(repo_root)
            .unwrap_or(&lockfile_path)
            .to_string_lossy()
            .replace('\\', "/");

        let Some(package) = manifest_package_name(&manifest_path) else {
            return LockfileState::NoVersionRecorded { lockfile_rel };
        };
        return match recorded_version(&lockfile_path, lockfile.filename, &package) {
            Some(recorded) if recorded == manifest_version => {
                LockfileState::Agrees { lockfile_rel }
            }
            Some(recorded) => LockfileState::Drifted {
                lockfile_rel,
                recorded,
            },
            None => LockfileState::NoVersionRecorded { lockfile_rel },
        };
    }

    LockfileState::None
}

/// The version a lockfile records for `package`, when the format records the
/// owning package at all. Only `Cargo.lock` does among the formats we handle:
/// pnpm, yarn and poetry lock dependencies without restating the version of
/// the package that owns them, so a drift of this shape cannot exist there.
fn recorded_version(lockfile_path: &Path, filename: &str, package: &str) -> Option<String> {
    if filename != "Cargo.lock" {
        return None;
    }
    let content = std::fs::read_to_string(lockfile_path).ok()?;
    let doc = content.parse::<toml_edit::DocumentMut>().ok()?;
    let packages = doc.get("package")?.as_array_of_tables()?;
    packages
        .iter()
        .find(|entry| entry.get("name").and_then(|n| n.as_str()) == Some(package))
        .and_then(|entry| entry.get("version"))
        .and_then(|v| v.as_str())
        .map(|v| v.to_string())
}

fn manifest_package_name(manifest_path: &Path) -> Option<String> {
    let content = std::fs::read_to_string(manifest_path).ok()?;
    let doc = content.parse::<toml_edit::DocumentMut>().ok()?;
    let name = doc.get("package")?.get("name")?.as_str()?;
    Some(name.to_string())
}

fn locate_lockfile(repo_root: &Path, manifest_dir: &Path, filename: &str) -> Option<PathBuf> {
    let mut dir = manifest_dir;
    loop {
        let candidate = dir.join(filename);
        if candidate.is_file() {
            return Some(candidate);
        }
        if dir == repo_root {
            return None;
        }
        dir = dir.parent()?;
    }
}

fn update_args(lockfile: &Lockfile, per_package: Option<&str>, offline: bool) -> Vec<String> {
    let mut args: Vec<String> = lockfile.base_args.iter().map(|a| a.to_string()).collect();
    if let Some(name) = per_package {
        args.push("-p".to_string());
        args.push(name.to_string());
        if offline {
            args.push("--offline".to_string());
        }
    }
    args
}

fn run_update(
    repo_root: &Path,
    lockfile_path: &Path,
    lockfile: &Lockfile,
    per_package: Option<&str>,
) -> UpdateOutcome {
    let lockfile_dir = lockfile_path.parent().unwrap_or(repo_root);

    let run = |offline: bool| {
        std::process::Command::new(lockfile.program)
            .current_dir(lockfile_dir)
            .args(update_args(lockfile, per_package, offline))
            .output()
    };

    let mut output = match run(per_package.is_some()) {
        Ok(output) => output,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            return UpdateOutcome::NotOnPath {
                program: lockfile.program.to_string(),
            };
        }
        Err(err) => {
            return UpdateOutcome::Failed {
                program: lockfile.program.to_string(),
                detail: err.to_string(),
            };
        }
    };

    if !output.status.success()
        && per_package.is_some()
        && let Ok(retry) = run(false)
    {
        output = retry;
    }

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return UpdateOutcome::Failed {
            program: lockfile.program.to_string(),
            detail: first_meaningful_line(&stderr),
        };
    }

    let lockfile_rel = lockfile_path
        .strip_prefix(repo_root)
        .unwrap_or(lockfile_path)
        .to_string_lossy()
        .replace('\\', "/");

    UpdateOutcome::Updated { lockfile_rel }
}

fn first_meaningful_line(text: &str) -> String {
    text.lines()
        .map(str::trim)
        .find(|l| !l.is_empty())
        .unwrap_or("")
        .to_string()
}

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

    #[test]
    fn detects_manifest_kind_from_filename() {
        assert_eq!(
            Manifest::from_manifest_filename("Cargo.toml"),
            Some(Manifest::CargoToml)
        );
        assert_eq!(
            Manifest::from_manifest_filename("package.json"),
            Some(Manifest::PackageJson)
        );
        assert_eq!(Manifest::from_manifest_filename("Chart.yaml"), None);
    }

    fn cargo() -> Lockfile {
        Manifest::CargoToml.lockfiles()[0]
    }

    #[test]
    fn the_retry_drops_offline_so_a_cold_registry_cache_can_be_filled() {
        let first = update_args(&cargo(), Some("ferrgames-discord"), true);
        assert_eq!(first, ["update", "-p", "ferrgames-discord", "--offline"]);

        let retry = update_args(&cargo(), Some("ferrgames-discord"), false);
        assert_eq!(
            retry,
            ["update", "-p", "ferrgames-discord"],
            "the release job checks out and installs the toolchain but never builds, \
             so the registry cache is empty and --offline cannot resolve dependencies"
        );
    }

    #[test]
    fn whole_lockfile_updates_never_get_offline_or_a_package() {
        let args = update_args(&cargo(), None, true);
        assert_eq!(args, ["update"]);
    }

    #[test]
    fn cargo_updates_the_crate_name_not_the_ferrflow_package_name() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let member = root.join("crates").join("api");
        std::fs::create_dir_all(member.join("src")).unwrap();
        std::fs::write(
            root.join("Cargo.toml"),
            "[workspace]\nresolver = \"3\"\nmembers = [\"crates/api\"]\n",
        )
        .unwrap();
        std::fs::write(
            member.join("Cargo.toml"),
            "[package]\nname = \"ferrgames-api\"\nversion = \"2.0.0\"\nedition = \"2024\"\n",
        )
        .unwrap();
        std::fs::write(member.join("src").join("lib.rs"), "").unwrap();
        std::fs::write(
            root.join("Cargo.lock"),
            "version = 4\n\n[[package]]\nname = \"ferrgames-api\"\nversion = \"1.0.0\"\n",
        )
        .unwrap();

        let outcome = update_for_manifest(root, "crates/api/Cargo.toml").unwrap();

        assert_eq!(
            outcome,
            UpdateOutcome::Updated {
                lockfile_rel: "Cargo.lock".to_string()
            },
            "the ferrflow package is named `api` but the crate is `ferrgames-api`; \
             passing the ferrflow name to `cargo update -p` fails and leaves the lock stale"
        );
        let lock = std::fs::read_to_string(root.join("Cargo.lock")).unwrap();
        assert!(lock.contains("2.0.0"), "lockfile not bumped: {lock}");
    }

    #[test]
    fn unsupported_manifest_is_a_noop() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Chart.yaml"), "version: 1.0.0\n").unwrap();
        let outcome = update_for_manifest(dir.path(), "Chart.yaml").unwrap();
        assert_eq!(outcome, UpdateOutcome::UnsupportedManifest);
    }

    #[test]
    fn no_lockfile_when_sibling_absent() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("Cargo.toml"),
            "[package]\nname = \"app\"\nversion = \"1.0.0\"\n",
        )
        .unwrap();
        let outcome = update_for_manifest(dir.path(), "Cargo.toml").unwrap();
        assert_eq!(outcome, UpdateOutcome::NoLockfile);
    }

    #[test]
    fn locate_lockfile_walks_up_to_repo_root() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let member = root.join("crates").join("app");
        std::fs::create_dir_all(&member).unwrap();
        std::fs::write(root.join("Cargo.lock"), "# lock\n").unwrap();

        let found = locate_lockfile(root, &member, "Cargo.lock").unwrap();
        assert_eq!(found, root.join("Cargo.lock"));
    }

    #[test]
    fn locate_lockfile_does_not_escape_repo_root() {
        let outer = tempfile::tempdir().unwrap();
        std::fs::write(outer.path().join("Cargo.lock"), "# lock\n").unwrap();
        let repo_root = outer.path().join("repo");
        let member = repo_root.join("crates").join("app");
        std::fs::create_dir_all(&member).unwrap();

        assert!(locate_lockfile(&repo_root, &member, "Cargo.lock").is_none());
    }
}