Answer
Cabald’s sandbox should *not* automatically roll back on every failed build. The tool should only revert when a failure is unequivocally a build error that corrupts the sandbox’s dependency graph or compiled artefacts. For expected failures (e.g., a deliberately broken test package) the sandbox should stay intact so that developers can inspect the artefacts.
Signals that warrant a rollback
- Non‑zero exit code from
cabal build or cabal configure when the error originates from the build system itself (e.g., missing compiler, missing library, or a syntax error in a .hs file).
- Errors reported by the build tool that are not test failures, such as "Could not find module …" or “Linker error: …”.
- Failure to generate a
plan.json or other sandbox metadata, which indicates the sandbox’s dependency graph was not fully resolved.
- Detection of a corrupted or partially written
*.cabal-sandbox directory (e.g., missing cabal.project.freeze after a failed configure).
In contrast, a test failure that exits with status 0, or a build that succeeds but the test suite fails, should not trigger a rollback.
Recording sandbox state for safe revert
The sandbox is a directory tree under dist-newstyle (or the legacy .cabal-sandbox folder). A lightweight snapshot can be achieved by:
- Copying the sandbox directory to a temporary location before the build starts. Only the metadata files (
plan.json, cabal.project.freeze, cabal.project) and the compiled artefacts (*.o, *.hi) need to be preserved; source files can be ignored.
- Recording a hash of the snapshot directory. If a rollback is required, the tool replaces the current sandbox with the snapshot and logs the restoration.
- Using a lightweight version‑control snapshot (e.g.,
git add .cabal-sandbox and a commit) if the project is under Git; the commit can be reverted on failure.
These approaches add negligible overhead because copying a few kilobytes of metadata is far cheaper than re‑resolving all dependencies.
Rollback policy: optional and configurable
Automatic rollback should be an opt‑in feature:
- Command‑line flag:
--rollback-on-failure (default off).
- Project configuration: a
cabal.project option rollback-on-failure: true that applies to all builds in that directory.
- Environment variable:
CABAL_ROLLBACK_ON_FAILURE=1 for temporary overrides.
When enabled, Cabal will perform the snapshot before the build and restore it only if the failure signals listed above are detected. The user can inspect the sandbox after a rollback and re‑run cabal build manually if desired.
Missing diagnostic detail
To fine‑tune the recommendation, could you confirm whether the build failure was caused by a compiler/linker error (non‑zero exit) or by a test failure that exits with status 0?