verctl-core 0.9.0

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
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
//! Whether a manifest opts its package out of publishing.
//!
//! The stack's own tool reads this setting from the manifest, so verctl reads
//! it there too. A package that opts out releases on GitHub only, with no
//! `publisher: none` beside it. Nothing here follows a symlink or leaves the
//! tree it is given.

use std::fs;
use std::io::ErrorKind;
use std::path::{Component, Path, PathBuf};

use anyhow::{Context, Result};
use toml_edit::{DocumentMut, Item};

/// How the manifest at `manifest` opts its package out of publishing, in the
/// words an error can repeat. `None` when it publishes, when the file is
/// missing, and for a manifest of another stack.
///
/// A `Cargo.toml` opts out with `package.publish` set to `false` or `[]`, and
/// a `package.json` with `"private": true`. `tree` is the root of the tree the
/// manifest is in, and no lookup leaves it.
///
/// # Errors
///
/// Returns an error when the manifest exists and cannot be read or parsed.
pub fn opt_out(manifest: &Path, tree: &Path) -> Result<Option<&'static str>> {
    let Some(bytes) = read_optional(manifest)? else {
        return Ok(None);
    };
    let text = String::from_utf8(bytes).with_context(|| manifest.display().to_string())?;
    let opted = match manifest.file_name().and_then(|name| name.to_str()) {
        Some("Cargo.toml") => {
            cargo_opts_out(manifest, &text, tree).with_context(|| manifest.display().to_string())?
        }
        Some("package.json") => {
            let json: serde_json::Value =
                serde_json::from_str(&text).with_context(|| manifest.display().to_string())?;
            (json.get("private") == Some(&serde_json::Value::Bool(true)))
                .then_some("package.json sets \"private\": true")
        }
        _ => None,
    };
    Ok(opted)
}

/// How the `Cargo.toml` at `manifest`, containing `text`, sets
/// `package.publish` to `false` or `[]`, or `None` when it does not.
///
/// With `publish.workspace = true`, the value is `workspace.package.publish`
/// of its workspace root. That root is the file `package.workspace` names.
/// Without that key it is the same file when it contains a `[workspace]`, else
/// the nearest ancestor manifest that does, as Cargo finds it.
///
/// The search stays inside `tree` and never passes through a symlink. The
/// merge-base copy and the working tree therefore resolve the same root, and no
/// manifest outside the repository is read.
fn cargo_opts_out(manifest: &Path, text: &str, tree: &Path) -> Result<Option<&'static str>> {
    let document = text.parse::<DocumentMut>()?;
    let Some(package) = document.get("package") else {
        return Ok(None);
    };
    let Some(publish) = package.get("publish") else {
        return Ok(None);
    };
    if publish.get("workspace").and_then(Item::as_bool) != Some(true) {
        return Ok(denies(publish).map(|denial| match denial {
            Reason::False => "Cargo.toml sets publish = false",
            Reason::NoRegistry => "Cargo.toml sets publish = []",
        }));
    }
    let Some(workspace) = workspace_root(manifest, &document, tree)? else {
        return Ok(None);
    };
    Ok(workspace
        .get("workspace")
        .and_then(|table| table.get("package"))
        .and_then(|package| package.get("publish"))
        .and_then(denies)
        .map(|denial| match denial {
            Reason::False => "Cargo.toml inherits publish = false from its workspace",
            Reason::NoRegistry => "Cargo.toml inherits publish = [] from its workspace",
        }))
}

/// The two values of `publish` that turn publishing off, as the
/// [manifest reference] lists them.
///
/// [manifest reference]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field
enum Reason {
    /// `publish = false`.
    False,
    /// `publish = []`: no registry may receive the package.
    NoRegistry,
}

/// Which value of `publish` turns publishing off, if any.
fn denies(publish: &Item) -> Option<Reason> {
    if publish.as_bool() == Some(false) {
        Some(Reason::False)
    } else if publish.as_array().is_some_and(toml_edit::Array::is_empty) {
        Some(Reason::NoRegistry)
    } else {
        None
    }
}

/// The workspace root manifest of the package at `manifest`, parsed, when
/// one exists inside `tree`. Every path is followed from `tree` without
/// resolving a symlink, in the package's own path and in each candidate's.
/// The merge-base copy contains no symlinks, so a manifest reached through one
/// would let the two trees resolve different roots.
fn workspace_root(
    manifest: &Path,
    document: &DocumentMut,
    tree: &Path,
) -> Result<Option<DocumentMut>> {
    if document.contains_key("workspace") {
        return Ok(Some(document.clone()));
    }
    let parent = manifest.parent().unwrap_or_else(|| Path::new(""));
    let Ok(package) = parent.strip_prefix(tree) else {
        return Ok(None);
    };
    if without_links(tree, package)?.is_none() {
        return Ok(None);
    }
    let named = document
        .get("package")
        .and_then(|package| package.get("workspace"))
        .and_then(Item::as_str);
    let dirs: Vec<PathBuf> = named.map_or_else(
        || package.ancestors().skip(1).map(Path::to_path_buf).collect(),
        |path| vec![package.join(path)],
    );
    for dir in dirs {
        let Some(candidate) = without_links(tree, &dir.join("Cargo.toml"))? else {
            continue;
        };
        let Some(bytes) = read_optional(&candidate)? else {
            continue;
        };
        let text = String::from_utf8(bytes).with_context(|| candidate.display().to_string())?;
        let parsed = text
            .parse::<DocumentMut>()
            .with_context(|| candidate.display().to_string())?;
        if parsed.contains_key("workspace") {
            return Ok(Some(parsed));
        }
    }
    Ok(None)
}

/// `path`, relative to `tree`, joined onto `tree` one component at a time.
/// `None` when a component is missing or a symlink, or when the path leaves
/// `tree`. A `..` drops a component already checked, so it never resolves
/// through a link.
fn without_links(tree: &Path, path: &Path) -> Result<Option<PathBuf>> {
    let mut resolved = tree.to_path_buf();
    let mut depth = 0_usize;
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                if depth == 0 {
                    return Ok(None);
                }
                resolved.pop();
                depth -= 1;
            }
            Component::Normal(name) => {
                resolved.push(name);
                depth += 1;
                match fs::symlink_metadata(&resolved) {
                    Ok(metadata) if metadata.file_type().is_symlink() => return Ok(None),
                    Ok(_) => {}
                    Err(error)
                        if matches!(
                            error.kind(),
                            ErrorKind::NotFound | ErrorKind::NotADirectory
                        ) =>
                    {
                        return Ok(None);
                    }
                    Err(error) => {
                        return Err(error).with_context(|| resolved.display().to_string());
                    }
                }
            }
            Component::RootDir | Component::Prefix(_) => return Ok(None),
        }
    }
    Ok(Some(resolved))
}

fn read_optional(path: &Path) -> Result<Option<Vec<u8>>> {
    match fs::read(path) {
        Ok(bytes) => Ok(Some(bytes)),
        Err(error) if error.kind() == ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error).with_context(|| path.display().to_string()),
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::os::unix::fs::symlink;
    use std::path::Path;

    use indoc::{formatdoc, indoc};
    use tempfile::TempDir;

    use super::{cargo_opts_out, opt_out};

    #[test]
    fn a_private_package_json_opts_out() {
        let dir = TempDir::new().expect("tempdir");
        let manifest = dir.path().join("package.json");
        fs::write(&manifest, r#"{"name": "web", "private": true}"#).expect("write");
        assert_eq!(
            opt_out(&manifest, dir.path()).expect("json"),
            Some("package.json sets \"private\": true")
        );
        fs::write(&manifest, r#"{"name": "web"}"#).expect("write");
        assert_eq!(opt_out(&manifest, dir.path()).expect("json"), None);
        assert_eq!(
            opt_out(&dir.path().join("missing/Cargo.toml"), dir.path()).expect("missing"),
            None
        );
    }

    #[test]
    fn publish_false_and_an_empty_list_opt_out() {
        let opts_out = |text: &str| {
            cargo_opts_out(Path::new("Cargo.toml"), text, Path::new("."))
                .expect("toml")
                .is_some()
        };
        assert!(opts_out(indoc! {r#"
            [package]
            name = "a"
            publish = false
        "#}));
        assert!(opts_out(indoc! {r#"
            [package]
            name = "a"
            publish = []
        "#}));
        assert!(!opts_out(indoc! {r#"
            [package]
            name = "a"
            publish = ["crates-io"]
        "#}));
        assert!(opts_out(indoc! {r#"
            [package]
            name = "a"
            publish.workspace = true

            [workspace.package]
            publish = false
        "#}));
    }

    /// A member that inherits `publish` reads it from the workspace root,
    /// as Cargo does.
    #[test]
    fn an_inherited_publish_reads_the_workspace_root() {
        let dir = TempDir::new().expect("tempdir");
        let member = dir.path().join("crates/a/Cargo.toml");
        fs::create_dir_all(member.parent().expect("parent")).expect("dir");
        let root = |line: &str| {
            fs::write(
                dir.path().join("Cargo.toml"),
                formatdoc! {r#"
                    [workspace]
                    members = ["crates/a"]

                    [workspace.package]
                    {line}
                "#},
            )
            .expect("write");
        };
        let text = indoc! {r#"
            [package]
            name = "a"
            publish.workspace = true
        "#};
        root("publish = false");
        assert!(
            cargo_opts_out(&member, text, dir.path())
                .expect("toml")
                .is_some()
        );
        root(r#"edition = "2024""#);
        assert!(
            cargo_opts_out(&member, text, dir.path())
                .expect("toml")
                .is_none()
        );
    }

    /// A manifest above the tree is never read: a workspace there does not
    /// count, and a malformed one does not fail the check.
    #[test]
    fn the_workspace_search_stops_at_the_tree() {
        let dir = TempDir::new().expect("tempdir");
        let tree = dir.path().join("repo");
        let member = tree.join("crates/a/Cargo.toml");
        fs::create_dir_all(member.parent().expect("parent")).expect("dir");
        let text = indoc! {r#"
            [package]
            name = "a"
            publish.workspace = true
        "#};
        let above = dir.path().join("Cargo.toml");
        fs::write(
            &above,
            indoc! {"
                [workspace]

                [workspace.package]
                publish = false
            "},
        )
        .expect("write");
        assert!(
            cargo_opts_out(&member, text, &tree)
                .expect("toml")
                .is_none()
        );
        fs::write(&above, "[workspace").expect("write");
        assert!(
            cargo_opts_out(&member, text, &tree)
                .expect("toml")
                .is_none()
        );
    }

    /// A symlinked `Cargo.toml` above the package is never followed, so a
    /// workspace outside the tree does not count, as in the merge-base copy.
    #[test]
    fn a_symlinked_workspace_manifest_is_skipped() {
        let dir = TempDir::new().expect("tempdir");
        let tree = dir.path().join("repo");
        let member = tree.join("crates/a/Cargo.toml");
        fs::create_dir_all(member.parent().expect("parent")).expect("dir");
        let outside = dir.path().join("outside.toml");
        fs::write(
            &outside,
            indoc! {"
                [workspace]

                [workspace.package]
                publish = false
            "},
        )
        .expect("write");
        symlink(&outside, tree.join("Cargo.toml")).expect("symlink");
        let text = indoc! {r#"
            [package]
            name = "a"
            publish.workspace = true
        "#};
        assert!(
            cargo_opts_out(&member, text, &tree)
                .expect("toml")
                .is_none()
        );
        fs::write(&outside, "[workspace").expect("write");
        assert!(
            cargo_opts_out(&member, text, &tree)
                .expect("toml")
                .is_none()
        );
    }

    /// A workspace reached through a directory symlink does not count. That
    /// applies whether `package.workspace` names the link or the package itself
    /// sits below one. Skipping it matches the merge-base copy, which contains
    /// no links.
    #[test]
    fn a_workspace_behind_a_directory_symlink_is_skipped() {
        let dir = TempDir::new().expect("tempdir");
        let tree = dir.path().join("repo");
        let real = tree.join("real");
        fs::create_dir_all(real.join("a")).expect("dir");
        fs::create_dir_all(tree.join("crates/a")).expect("dir");
        fs::write(
            real.join("Cargo.toml"),
            indoc! {"
                [workspace]

                [workspace.package]
                publish = false
            "},
        )
        .expect("write");
        symlink("real", tree.join("linked")).expect("symlink");
        symlink("../real/a", tree.join("crates/b")).expect("symlink");
        let named = |workspace: &str| {
            formatdoc! {r#"
                [package]
                name = "a"
                workspace = "{workspace}"
                publish.workspace = true
            "#}
        };
        let member = tree.join("crates/a/Cargo.toml");
        assert!(
            cargo_opts_out(&member, &named("../../real"), &tree)
                .expect("toml")
                .is_some()
        );
        assert!(
            cargo_opts_out(&member, &named("../../linked"), &tree)
                .expect("toml")
                .is_none()
        );
        let text = indoc! {r#"
            [package]
            name = "a"
            publish.workspace = true
        "#};
        assert!(
            cargo_opts_out(&real.join("a/Cargo.toml"), text, &tree)
                .expect("toml")
                .is_some()
        );
        assert!(
            cargo_opts_out(&tree.join("crates/b/Cargo.toml"), text, &tree)
                .expect("toml")
                .is_none()
        );
    }
}