diff --git a/cmd/devbox/disk_other.go b/cmd/devbox/disk_other.go new file mode 100644 index 0000000..c192163 --- /dev/null +++ b/cmd/devbox/disk_other.go @@ -0,0 +1,6 @@ +//go:build !unix + +package main + +// freeBytes is not implemented on non-unix platforms; callers skip the check silently. +func freeBytes(_ string) (uint64, bool) { return 0, false } diff --git a/cmd/devbox/disk_unix.go b/cmd/devbox/disk_unix.go new file mode 100644 index 0000000..9d89372 --- /dev/null +++ b/cmd/devbox/disk_unix.go @@ -0,0 +1,15 @@ +//go:build unix + +package main + +import "syscall" + +// freeBytes returns the number of bytes available to an unprivileged process +// on the filesystem containing path. Returns ok=false if the stat fails. +func freeBytes(path string) (uint64, bool) { + var s syscall.Statfs_t + if err := syscall.Statfs(path, &s); err != nil { + return 0, false + } + return s.Bavail * uint64(s.Bsize), true // ponytail: Bsize is int32 on macOS, int64 on Linux; always positive +} diff --git a/cmd/devbox/disk_unix_test.go b/cmd/devbox/disk_unix_test.go new file mode 100644 index 0000000..dae224f --- /dev/null +++ b/cmd/devbox/disk_unix_test.go @@ -0,0 +1,15 @@ +//go:build unix + +package main + +import "testing" + +func TestFreeBytesBasic(t *testing.T) { + free, ok := freeBytes(t.TempDir()) + if !ok { + t.Fatal("freeBytes on TempDir must return ok=true on unix") + } + if free == 0 { + t.Fatal("freeBytes returned 0; expected > 0 for a real filesystem") + } +} diff --git a/cmd/devbox/main.go b/cmd/devbox/main.go index 7888f1b..0d6ada0 100644 --- a/cmd/devbox/main.go +++ b/cmd/devbox/main.go @@ -557,15 +557,19 @@ func ignoreCmd() *cobra.Command { Short: "🙈 append a pattern to ./.devignore (run from inside a mount)", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + pat := args[0] + if _, err := ignore.Compile([]string{pat}); err != nil { + return fmt.Errorf("invalid ignore pattern %q: %w", pat, err) + } f, err := os.OpenFile(".devignore", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) if err != nil { return err } defer f.Close() - if _, err := fmt.Fprintln(f, args[0]); err != nil { + if _, err := fmt.Fprintln(f, pat); err != nil { return err } - fmt.Fprintf(cmd.OutOrStdout(), "🙈 added %q to ./.devignore (applies on next sync)\n", args[0]) + fmt.Fprintf(cmd.OutOrStdout(), "🙈 added %q to ./.devignore (applies on next sync)\n", pat) return nil }, } @@ -1101,6 +1105,23 @@ func doctorCmd() *cobra.Command { } } + // Disk space: warn (not fail) when a mount dir or the config dir has < 1 GiB free. + diskPaths := []string{dir} + for _, m := range d.Mounts { + diskPaths = append(diskPaths, m.Local) + } + for _, p := range diskPaths { + free, ok := freeBytes(p) + if !ok { + continue // unsupported platform or stat failed; skip silently + } + if free < 1<<30 { + check(false, true, "disk space", fmt.Sprintf("%s has only %d MiB free", p, free>>20)) + } else { + check(true, false, "disk space", fmt.Sprintf("%s has %d MiB free", p, free>>20)) + } + } + if failed { if !asJSON { fmt.Fprintln(out) diff --git a/cmd/devbox/main_test.go b/cmd/devbox/main_test.go index 59bd2f0..1f44eca 100644 --- a/cmd/devbox/main_test.go +++ b/cmd/devbox/main_test.go @@ -176,6 +176,22 @@ func TestWritePidArbitration(t *testing.T) { } } +// TestIgnoreCmdValidatesPattern proves `ignore` accepts valid patterns and writes to .devignore. +func TestIgnoreCmdValidatesPattern(t *testing.T) { + t.Chdir(t.TempDir()) + var buf bytes.Buffer + cmd := ignoreCmd() + cmd.SetOut(&buf) + cmd.SetArgs([]string{"*.go"}) + if err := cmd.Execute(); err != nil { + t.Fatalf("valid pattern *.go rejected: %v", err) + } + b, _ := os.ReadFile(".devignore") + if !strings.Contains(string(b), "*.go") { + t.Fatalf(".devignore missing pattern, got: %q", string(b)) + } +} + // TestStatusDaemonNotRunning proves that `status` (non-JSON) warns the user // when the device is joined but the daemon is not running. func TestStatusDaemonNotRunning(t *testing.T) { diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 1391bb9..d3d7be9 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -385,15 +385,17 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) { } func (d *Daemon) report(m config.Mount, pr syncer.PullResult) { - if len(pr.Written)+len(pr.Deleted)+len(pr.Conflicts)+len(pr.Skipped) == 0 { - return + if len(pr.Written)+len(pr.Deleted)+len(pr.Conflicts)+len(pr.Skipped) > 0 { + d.logf("📥 %s: %d written, %d deleted, %d conflicts, %d skipped", + m.Share, len(pr.Written), len(pr.Deleted), len(pr.Conflicts), len(pr.Skipped)) + for _, cf := range pr.Conflicts { + d.logf(" 💥 conflict copy: %s", cf) + } + for _, sk := range pr.Skipped { + d.logf(" ⚠️ skipped (filesystem clash): %s", sk) + } } - d.logf("📥 %s: %d written, %d deleted, %d conflicts, %d skipped", - m.Share, len(pr.Written), len(pr.Deleted), len(pr.Conflicts), len(pr.Skipped)) - for _, cf := range pr.Conflicts { - d.logf(" 💥 conflict copy: %s", cf) - } - for _, sk := range pr.Skipped { - d.logf(" ⚠️ skipped (filesystem clash): %s", sk) + for _, w := range pr.Warnings { + d.logf("⚠️ %s: %s", m.Share, w) } } diff --git a/internal/syncer/hooks_integration_test.go b/internal/syncer/hooks_integration_test.go index 25a1aba..7bc2b5b 100644 --- a/internal/syncer/hooks_integration_test.go +++ b/internal/syncer/hooks_integration_test.go @@ -65,6 +65,37 @@ func TestPostPullHookFires(t *testing.T) { } } +// TestPostPullHookWarningOnFailure proves a failing post-pull hook surfaces in +// PullResult.Warnings without aborting the sync. +func TestPostPullHookWarningOnFailure(t *testing.T) { + db, _ := meta.Open(":memory:") + defer db.Close() + store, _ := blobstore.NewDisk(t.TempDir()) + srv := httptest.NewServer(hub.NewServer(db, store).Handler()) + defer srv.Close() + guard, _ := secret.New(nil) + ig, _ := LoadIgnore(t.TempDir()) + + A := joinDevice(t, db, srv.URL, "alice") + B := joinDevice(t, db, srv.URL, "bob") + _ = A.Publish("s") + rootA := t.TempDir() + writeFile(t, rootA, "app.txt", "v1\n") + if _, _, err := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard, nil); err != nil { + t.Fatal(err) + } + + rootB := t.TempDir() + writeExecHook(t, rootB, "post-pull", "#!/usr/bin/env bash\nexit 1\n") + _, pr, err := Sync(B, rootB, "s", "", "", "bob", 2, ig, guard, quietRunner(rootB, "s")) + if err != nil { + t.Fatalf("sync must not fail when post-pull exits non-zero: %v", err) + } + if len(pr.Warnings) == 0 { + t.Fatal("expected Warnings from the failing post-pull hook, got none") + } +} + func TestPrePushHookVetoes(t *testing.T) { db, _ := meta.Open(":memory:") defer db.Close() diff --git a/internal/syncer/pull.go b/internal/syncer/pull.go index a1ae455..cb7c7a5 100644 --- a/internal/syncer/pull.go +++ b/internal/syncer/pull.go @@ -22,6 +22,7 @@ type PullResult struct { Conflicts []string // conflict copies created (local edits preserved beside canonical) Skipped []string // paths a filesystem error skipped (e.g. file<->dir clash) Vetoed bool // a pre-pull hook aborted applying inbound changes + Warnings []string // non-fatal hook errors (e.g. post-pull timed out) } // Pull fetches the hub head manifest for share, three-way merges it against the @@ -152,9 +153,13 @@ func Pull(c *transport.Client, root, share, subpath, base, host string, now int6 } if len(res.Conflicts) > 0 { - _ = hk.Run(hooks.OnConflict, res.Conflicts, head) + if err := hk.Run(hooks.OnConflict, res.Conflicts, head); err != nil { + res.Warnings = append(res.Warnings, err.Error()) + } + } + if err := hk.Run(hooks.PostPull, append(append([]string{}, res.Written...), res.Deleted...), head); err != nil { + res.Warnings = append(res.Warnings, err.Error()) } - _ = hk.Run(hooks.PostPull, append(append([]string{}, res.Written...), res.Deleted...), head) res.Base = head return res, nil