xbp 10.46.0

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use crate::utils::canonicalize_for_subprocess;
use colored::Colorize;
use serde_json::json;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

const PRIMARY_WORKTREE_LINK_PATHS: &[&str] = &["apps/web/.dev.vars", "apps/web/wrangler.dev.jsonc"];

/// Repo-relative path report (forward slashes). Absolute host paths are never
/// required for consumers — everything is anchored to the current git toplevel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorktreePathReport {
    /// Always `"."` — the current checkout's git toplevel.
    pub repo_root: String,
    /// `"."` when this checkout is the primary worktree; otherwise a relative
    /// path from the current toplevel to the primary worktree (or a portable
    /// absolute path if the two trees do not share a prefix).
    pub primary_worktree_root: String,
    pub is_worktree_checkout: bool,
    /// Shared Wrangler local state, relative to the current toplevel when
    /// possible (e.g. `.wrangler/state` or `../main/.wrangler/state`).
    pub shared_wrangler_state_path: String,
}

pub fn collect_worktree_path_report(invocation_dir: &Path) -> Result<WorktreePathReport, String> {
    let repo_root = get_repo_root(invocation_dir)?;
    let primary_root = get_primary_worktree_root(invocation_dir)?;
    let is_worktree = !path_eq(&repo_root, &primary_root);
    let primary_rel = path_relative_to(&repo_root, &primary_root);
    let shared_rel = if primary_rel == "." {
        ".wrangler/state".to_string()
    } else {
        format!(
            "{}/.wrangler/state",
            primary_rel.trim_end_matches('/')
        )
    };

    Ok(WorktreePathReport {
        repo_root: ".".to_string(),
        primary_worktree_root: primary_rel,
        is_worktree_checkout: is_worktree,
        shared_wrangler_state_path: shared_rel,
    })
}

pub fn print_worktree_paths(invocation_dir: &Path) -> Result<(), String> {
    let report = collect_worktree_path_report(invocation_dir)?;
    print_worktree_path_report(&report)
}

fn print_worktree_path_report(report: &WorktreePathReport) -> Result<(), String> {
    let linked = if report.is_worktree_checkout {
        "yes".yellow().to_string()
    } else {
        "no".green().to_string()
    };

    println!(
        "{} {}",
        "".bright_blue().bold(),
        "Worktree paths".bright_blue().bold()
    );
    println!("{}", "".repeat(48).bright_black());
    print_kv("checkout (repo root)", &report.repo_root);
    print_kv("primary worktree", &report.primary_worktree_root);
    print_kv("linked worktree checkout", &linked);
    print_kv("shared wrangler state", &report.shared_wrangler_state_path);
    println!("{}", "".repeat(48).bright_black());
    println!(
        "{}",
        "Paths are relative to the current git toplevel (portable across Linux / WSL / Windows)."
            .dimmed()
    );
    println!();
    println!(
        "{}",
        serde_json::to_string_pretty(&json!({
            "repo_root": report.repo_root,
            "primary_worktree_root": report.primary_worktree_root,
            "is_worktree_checkout": report.is_worktree_checkout,
            "shared_wrangler_state_path": report.shared_wrangler_state_path,
        }))
        .map_err(|error| format!("Failed to encode JSON output: {}", error))?
    );
    Ok(())
}

fn print_kv(label: &str, value: &str) {
    println!(
        "  {:<26} {}",
        label.bright_white(),
        value.cyan()
    );
}

/// Relative path from `base` to `target` using portable `/` separators.
/// Returns `"."` when equal. Falls back to a portable absolute path when the
/// trees do not share a prefix (different drives, unrelated mounts).
pub fn path_relative_to(base: &Path, target: &Path) -> String {
    if path_eq(base, target) {
        return ".".to_string();
    }

    let base_key = path_compare_key(base);
    let target_key = path_compare_key(target);
    let base_parts = path_components(&base_key);
    let target_parts_cmp = path_components(&target_key);
    // Prefer original portable casing for display segments of the target.
    let target_disp = path_components(&portable_path_string(target));

    let mut common = 0usize;
    while common < base_parts.len()
        && common < target_parts_cmp.len()
        && base_parts[common] == target_parts_cmp[common]
    {
        common += 1;
    }

    // No shared prefix (e.g. `C:/a` vs `D:/b`, or `/home` vs `/mnt/c`) → absolute.
    if common == 0 {
        return portable_path_string(target);
    }

    let mut rel: Vec<String> = Vec::new();
    for _ in common..base_parts.len() {
        rel.push("..".to_string());
    }
    for part in target_disp.iter().skip(common) {
        rel.push(part.clone());
    }

    if rel.is_empty() {
        ".".to_string()
    } else {
        rel.join("/")
    }
}

fn path_components(path: &str) -> Vec<String> {
    path.split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

pub fn link_dev_vars_from_primary_worktree(invocation_dir: &Path) -> Result<(), String> {
    let repo_root = get_repo_root(invocation_dir)?;
    let primary_root = get_primary_worktree_root(invocation_dir)?;

    if path_eq(&repo_root, &primary_root) {
        println!("Current checkout is the primary worktree; nothing to link.");
        return Ok(());
    }

    let mut linked = Vec::new();
    let mut skipped = Vec::new();
    for rel_path in PRIMARY_WORKTREE_LINK_PATHS {
        match link_from_primary_worktree(rel_path, &primary_root, &repo_root)? {
            LinkOutcome::Linked(path) => linked.push(path),
            LinkOutcome::Unchanged(path) => skipped.push(format!("{path} (already linked)")),
            LinkOutcome::Skipped(path) => skipped.push(path),
        }
    }

    if !linked.is_empty() {
        println!("Linked from primary worktree:");
        for file in linked {
            println!("  - {}", file);
        }
    }

    if !skipped.is_empty() {
        println!("Skipped:");
        for file in skipped {
            println!("  - {}", file);
        }
    }

    Ok(())
}

pub fn get_repo_root(invocation_dir: &Path) -> Result<PathBuf, String> {
    let root = PathBuf::from(run_git(invocation_dir, ["rev-parse", "--show-toplevel"])?);
    Ok(normalize_resolved_path(&root))
}

pub fn get_primary_worktree_root(invocation_dir: &Path) -> Result<PathBuf, String> {
    let common_dir = get_git_common_dir(invocation_dir)?;
    let primary = common_dir
        .parent()
        .map(Path::to_path_buf)
        .unwrap_or(common_dir);
    Ok(normalize_resolved_path(&primary))
}

pub fn get_shared_wrangler_state_path(invocation_dir: &Path) -> Result<PathBuf, String> {
    let primary = get_primary_worktree_root(invocation_dir)?;
    // Join via portable string so Windows `\` does not leak into later display.
    Ok(PathBuf::from(format!(
        "{}/.wrangler/state",
        portable_path_string(&primary).trim_end_matches('/')
    )))
}

pub fn is_worktree_checkout(invocation_dir: &Path) -> Result<bool, String> {
    let repo_root = get_repo_root(invocation_dir)?;
    let primary_root = get_primary_worktree_root(invocation_dir)?;
    Ok(!path_eq(&repo_root, &primary_root))
}

fn get_git_common_dir(invocation_dir: &Path) -> Result<PathBuf, String> {
    match run_git(
        invocation_dir,
        ["rev-parse", "--path-format=absolute", "--git-common-dir"],
    ) {
        Ok(path) => Ok(normalize_resolved_path(Path::new(&path))),
        Err(_) => {
            let common_dir_raw = run_git(invocation_dir, ["rev-parse", "--git-common-dir"])?;
            let common_dir = PathBuf::from(&common_dir_raw);
            if common_dir.is_absolute() {
                Ok(normalize_resolved_path(&common_dir))
            } else {
                Ok(normalize_resolved_path(&invocation_dir.join(common_dir)))
            }
        }
    }
}

/// Canonicalize when possible and strip platform noise so PathBufs are usable
/// with child processes and comparable across git vs filesystem sources.
fn normalize_resolved_path(path: &Path) -> PathBuf {
    let cleaned = PathBuf::from(portable_path_string(path));
    canonicalize_for_subprocess(&cleaned)
}

fn run_git<const N: usize>(invocation_dir: &Path, args: [&str; N]) -> Result<String, String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(invocation_dir)
        .output()
        .map_err(|error| format!("Failed to run git {}: {}", args.join(" "), error))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let message = if stderr.is_empty() {
            format!(
                "git {} exited with status {}",
                args.join(" "),
                output.status
            )
        } else {
            stderr
        };
        return Err(message);
    }

    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if stdout.is_empty() {
        return Err(format!("git {} returned an empty response", args.join(" ")));
    }
    Ok(stdout)
}

enum LinkOutcome {
    Linked(String),
    Unchanged(String),
    Skipped(String),
}

fn link_from_primary_worktree(
    rel_path: &str,
    primary_root: &Path,
    repo_root: &Path,
) -> Result<LinkOutcome, String> {
    let source = primary_root.join(rel_path);
    let target = repo_root.join(rel_path);
    let Some(target_dir) = target.parent() else {
        return Err(format!(
            "Could not resolve parent directory for {}",
            target.display()
        ));
    };

    if !source.exists() {
        return Ok(LinkOutcome::Skipped(format!(
            "{} (missing in primary worktree)",
            rel_path
        )));
    }

    fs::create_dir_all(target_dir)
        .map_err(|error| format!("Failed to create {}: {}", target_dir.display(), error))?;

    if let Ok(metadata) = fs::symlink_metadata(&target) {
        if metadata.file_type().is_symlink() {
            let current_target = fs::read_link(&target)
                .map_err(|error| format!("Failed to inspect {}: {}", target.display(), error))?;
            let resolved_target = if current_target.is_absolute() {
                current_target
            } else {
                target_dir.join(current_target)
            };
            if path_eq(&resolved_target, &source) {
                return Ok(LinkOutcome::Unchanged(rel_path.to_string()));
            }
            fs::remove_file(&target)
                .map_err(|error| format!("Failed to replace {}: {}", target.display(), error))?;
        } else {
            return Ok(LinkOutcome::Skipped(format!(
                "{} (real file exists)",
                rel_path
            )));
        }
    }

    create_file_symlink(&source, &target)?;
    Ok(LinkOutcome::Linked(rel_path.to_string()))
}

#[cfg(windows)]
fn create_file_symlink(source: &Path, target: &Path) -> Result<(), String> {
    std::os::windows::fs::symlink_file(source, target).map_err(|error| {
        format!(
            "Failed to create symlink {} -> {}: {}",
            target.display(),
            source.display(),
            error
        )
    })
}

#[cfg(not(windows))]
fn create_file_symlink(source: &Path, target: &Path) -> Result<(), String> {
    std::os::unix::fs::symlink(source, target).map_err(|error| {
        format!(
            "Failed to create symlink {} -> {}: {}",
            target.display(),
            source.display(),
            error
        )
    })
}

fn path_eq(left: &Path, right: &Path) -> bool {
    path_compare_key(left) == path_compare_key(right)
}

/// Stable absolute-path string for JSON / logs on Linux, WSL, and Windows:
/// - forward slashes only
/// - no `\\?\` / `\\.\` verbatim prefixes
/// - UNC kept as `//server/share`
/// - no trailing slash (except `/` or `C:/`)
pub fn portable_path_string(path: impl AsRef<Path>) -> String {
    let raw = path.as_ref().to_string_lossy();
    let mut value = raw.replace('\\', "/");

    // Windows extended-length / device paths (after `\` → `/`).
    if let Some(rest) = value.strip_prefix("//?/UNC/") {
        value = format!("//{rest}");
    } else if let Some(rest) = value.strip_prefix("//./UNC/") {
        value = format!("//{rest}");
    } else if let Some(rest) = value.strip_prefix("//?/") {
        value = rest.to_string();
    } else if let Some(rest) = value.strip_prefix("//./") {
        value = rest.to_string();
    }

    // Collapse accidental duplicate slashes, keep UNC `//` prefix.
    value = collapse_duplicate_slashes(&value);

    // Uppercase Windows drive letter for stable display (`c:/` → `C:/`).
    if looks_like_windows_drive(&value) {
        let mut chars = value.chars();
        let drive = chars.next().unwrap().to_ascii_uppercase();
        let rest: String = chars.collect();
        value = format!("{drive}{rest}");
    }

    trim_trailing_slash(&value)
}

/// Comparison key: portable form + lowercase + WSL `/mnt/c/...` ↔ `C:/...`.
fn path_compare_key(path: &Path) -> String {
    let mut value = portable_path_string(path).to_ascii_lowercase();
    value = wsl_mnt_to_windows_drive(&value);
    value
}

fn wsl_mnt_to_windows_drive(path: &str) -> String {
    // `/mnt/c/Users/...` → `c:/Users/...` so WSL and Windows paths compare equal.
    let Some(rest) = path.strip_prefix("/mnt/") else {
        return path.to_string();
    };
    let mut chars = rest.chars();
    let Some(drive) = chars.next() else {
        return path.to_string();
    };
    if !drive.is_ascii_alphabetic() {
        return path.to_string();
    }
    let after: String = chars.collect();
    if after.is_empty() {
        return format!("{drive}:/");
    }
    if let Some(stripped) = after.strip_prefix('/') {
        return format!("{drive}:/{stripped}");
    }
    path.to_string()
}

fn looks_like_windows_drive(path: &str) -> bool {
    let bytes = path.as_bytes();
    bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':'
}

fn collapse_duplicate_slashes(path: &str) -> String {
    if path.starts_with("//") && !path.starts_with("//?/") && !path.starts_with("//./") {
        // UNC or protocol-style: keep leading `//`, collapse the rest.
        let rest = path.trim_start_matches('/');
        return format!("//{}", rest.split('/').filter(|s| !s.is_empty()).collect::<Vec<_>>().join("/"));
    }
    let mut out = String::with_capacity(path.len());
    let mut prev_slash = false;
    for ch in path.chars() {
        if ch == '/' {
            if !prev_slash {
                out.push('/');
            }
            prev_slash = true;
        } else {
            out.push(ch);
            prev_slash = false;
        }
    }
    out
}

fn trim_trailing_slash(path: &str) -> String {
    if path == "/" {
        return path.to_string();
    }
    // Keep `C:/` drive roots.
    if looks_like_windows_drive(path) {
        let stripped = path.trim_end_matches('/');
        if stripped.len() == 2 {
            // `C:` alone → `C:/`
            return format!("{stripped}/");
        }
        return stripped.to_string();
    }
    path.trim_end_matches('/').to_string()
}

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

    #[test]
    fn print_worktree_paths_succeeds_from_repo_root() {
        let invocation_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../..")
            .canonicalize()
            .expect("workspace root");
        // Must not require --app when multiple workers exist at monorepo root.
        print_worktree_paths(&invocation_dir).expect("paths should resolve from monorepo root");
        let report =
            collect_worktree_path_report(&invocation_dir).expect("relative path report");
        assert_eq!(report.repo_root, ".");
        assert_eq!(report.primary_worktree_root, ".");
        assert!(!report.is_worktree_checkout);
        assert_eq!(report.shared_wrangler_state_path, ".wrangler/state");
        assert!(!report.shared_wrangler_state_path.contains('\\'));
        assert!(!report.shared_wrangler_state_path.contains('?'));
    }

    #[test]
    fn report_from_nested_app_dir_is_still_repo_relative() {
        let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../..")
            .canonicalize()
            .expect("workspace root");
        let nested = repo.join("apps").join("web");
        let report = collect_worktree_path_report(&nested).expect("report from apps/web");
        assert_eq!(report.repo_root, ".");
        assert_eq!(report.primary_worktree_root, ".");
        assert!(!report.is_worktree_checkout);
        assert_eq!(report.shared_wrangler_state_path, ".wrangler/state");
    }

    #[test]
    fn path_relative_to_handles_sibling_and_equal() {
        let base = Path::new(r"C:\Users\floris\Documents\GitHub\xbp");
        let same = Path::new(r"\\?\C:\Users\floris\Documents\GitHub\xbp");
        let sibling = Path::new(r"C:\Users\floris\Documents\GitHub\xbp-worktree");
        assert_eq!(path_relative_to(base, same), ".");
        assert_eq!(path_relative_to(base, sibling), "../xbp-worktree");
    }

    #[test]
    fn path_eq_ignores_windows_verbatim_prefix() {
        let left = Path::new(r"\\?\C:\Users\floris\Documents\GitHub\xbp");
        let right = Path::new(r"C:/Users/floris/Documents/GitHub/xbp");
        assert!(path_eq(left, right));
    }

    #[test]
    fn path_eq_matches_wsl_mnt_and_windows_drive() {
        let wsl = Path::new("/mnt/c/Users/floris/Documents/GitHub/xbp");
        let win = Path::new(r"C:\Users\floris\Documents\GitHub\xbp");
        assert!(path_eq(wsl, win));
    }

    #[test]
    fn portable_path_string_is_consistent_across_styles() {
        assert_eq!(
            portable_path_string(r"\\?\C:\Users\floris\xbp"),
            "C:/Users/floris/xbp"
        );
        assert_eq!(
            portable_path_string(r"c:\Users\floris\xbp\"),
            "C:/Users/floris/xbp"
        );
        assert_eq!(
            portable_path_string(r"\\?\UNC\server\share\repo"),
            "//server/share/repo"
        );
        assert_eq!(
            portable_path_string("/home/floris/xbp/"),
            "/home/floris/xbp"
        );
        assert_eq!(portable_path_string("/"), "/");
        assert_eq!(portable_path_string("C:"), "C:/");
    }

    #[test]
    fn portable_paths_have_no_mixed_separators() {
        let mixed = portable_path_string(r"C:\Users/floris/Documents/GitHub\xbp\.wrangler\state");
        assert!(!mixed.contains('\\'));
        assert!(mixed.contains('/'));
        assert_eq!(mixed, "C:/Users/floris/Documents/GitHub/xbp/.wrangler/state");
    }
}