Laravel Go✓ Human-reviewed

Private temporary workspaces in Laravel Go

Replace temporary-directory's lifecycle with os.MkdirTemp and explicit deferred cleanup, including cancellation, worker lifetime, and filesystem boundary checks.

Approved 2026-12-04T12:03:49.465674Z · Submission 28

Private temporary workspaces in Laravel Go

Scope and pinned references

Reference package: Spatie's MIT-licensed spatie/temporary-directory 2.4.0, commit 32cbb9645b28839cf4f476708e99a2c70e6802c9. Inspect src/TemporaryDirectory.php and its tests. Preserve the MIT attribution if adapting source. Laravel Go baseline: 56c7fd3691dc4ccb636119308d6fcf919e308518, module go.laravel.com; use the Go version required by the target app's go.mod.

Check get-recipe's current port catalog; if it lists a Go port, evaluate that option before rewriting anything. This recipe takes the other path: inline one private workspace lifecycle using the standard library. It is not a complete recreation of the PHP fluent API and has not yet been tried in your app.

Source behavior and deliberate differences

Upstream creates a random named directory under a supplied/system root, checks existing paths, supports force replacement and nested path() creation, and optionally deletes when destroyed. PHP destruction is not a Go lifecycle boundary. Use a function/job-owned directory, explicit error handling, and defer; exclude force deletion and user-selected locations. os.MkdirTemp uses private permissions before umask and does not create a missing parent root.

Implementation in the operation that owns the work

Keep the root provisioned, outside public storage, with trusted ownership and ancestors. The following standalone helper illustrates cleanup error handling; integrate the pattern into the existing job rather than building a package compatibility layer:

func withWorkspace(root string, work func(string) error) (err error) {
    dir, err := os.MkdirTemp(root, "export-")
    if err != nil {
        return err
    }
    defer func() {
        err = errors.Join(err, os.RemoveAll(dir))
    }()
    return work(dir)
}

Imports are os and errors. Callers create only application-owned paths such as filepath.Join(dir, "result.json"), close every file, and read or persist output before returning. errors.Join preserves both operation and cleanup errors. If the work panics, deferred cleanup runs during unwinding, but its returned error is not delivered through the normal return path; follow the app's panic reporting policy.

  1. Inventory all PHP calls, especially force(), empty(), named directories, and nested paths. Adapt only the behavior the actual export needs.
  2. A root must be trusted configuration, never a request field. Only fixed filenames or validated single components may be joined beneath it. Reject .., separators, absolute paths, and drive-prefixed paths; filepath.Clean alone does not establish containment.
  3. Pass the request/job context to the work, check cancellation, and wait for every goroutine to stop using the workspace before returning. defer in a function that merely starts a goroutine cleans up too early. Do not pass transaction/request context to work that outlives it.
  4. For HTTP streams, own the workspace inside the callback that finishes sending bytes. For queued work, create it in the worker. For durable results, move/copy to private persistent storage before cleanup and return a durable identifier, not the temporary path.
  5. Use argument-based subprocess APIs, not shell interpolation. Do not add untrusted archive extraction to this bounded recipe.
  6. Process kills do not run defers. If cleanup after crashes is required, use an age-based janitor limited to this dedicated root, with explicit exclusions for active jobs.

Trial checklist

Create tests around the real export/worker using t.TempDir() as the parent, not a fake object-store disk:

  • Concurrent exports use distinct directories, return the correct different outputs, and leave no owned children.
  • Force a failure after a file is written; verify both the original error and cleanup. Inject a cleanup failure where the OS allows it and check it is reported.
  • A symlink inside the workspace points at an outside sentinel; removal must preserve the sentinel. Verify target-platform behavior rather than assuming every filesystem is identical.
  • Missing/read-only root, invalid filename, traversal, and canceled work fail without deleting unrelated files.
  • A delayed goroutine or stream finishes reading before removal; all open files are closed on Windows as well as Unix.

Run gofmt on the adaptation, go test ./..., go test -race ./..., and go vet ./... on the target app. Keep no hidden dependency on the old PHP wrapper. Record the Go/framework versions, OS, and test output with the change; do not call the adaptation production-tested before those checks run.