Description
Calling temp_dir_create(..., true, ...) multiple times only automatically removes the last created directory. Earlier directories remain after the script exits.
Each call installs a new EXIT trap:
$ trap 'rm -rf {filename}' EXIT $
Shells maintain one handler per signal, so every new trap ... EXIT replaces the previous handler.
Reproduction
import { temp_dir_create } from "std/fs"
main {
echo(temp_dir_create("amber-auto-delete-one-XXXX", true, true)?)
echo(temp_dir_create("amber-auto-delete-two-XXXX", true, true)?)
echo(temp_dir_create("amber-auto-delete-three-XXXX", true, true)?)
echo(temp_dir_create("amber-auto-delete-four-XXXX", true, true)?)
}
After the script exits, the first three directories remain and only the fourth is removed.
Expected behavior
All directories created with auto_delete = true should be removed when the script exits.
Actual behavior
Only the most recently created directory is removed because its EXIT trap replaces the previous traps.
Suggested solution
Maintain a shared collection of temporary directories and install a single EXIT trap that removes every registered directory.
Conceptually, the generated shell code could behave like:
__amber_temp_dirs+=("$filename")
trap 'rm -rf "${__amber_temp_dirs[@]}"' EXIT
The implementation should maintain a global variable, which gets appended to it in temp_dir_create and trap reapplied.
Additional context
Tests that create multiple temporary directories currently require explicit cleanup. Removing that cleanup can leave temporary directories behind even when auto_delete and force_delete are both enabled. This problem has been discovered in #1146
Description
Calling
temp_dir_create(..., true, ...)multiple times only automatically removes the last created directory. Earlier directories remain after the script exits.Each call installs a new
EXITtrap:Shells maintain one handler per signal, so every new
trap ... EXITreplaces the previous handler.Reproduction
After the script exits, the first three directories remain and only the fourth is removed.
Expected behavior
All directories created with
auto_delete = trueshould be removed when the script exits.Actual behavior
Only the most recently created directory is removed because its
EXITtrap replaces the previous traps.Suggested solution
Maintain a shared collection of temporary directories and install a single
EXITtrap that removes every registered directory.Conceptually, the generated shell code could behave like:
The implementation should maintain a global variable, which gets appended to it in
temp_dir_createand trap reapplied.Additional context
Tests that create multiple temporary directories currently require explicit cleanup. Removing that cleanup can leave temporary directories behind even when
auto_deleteandforce_deleteare both enabled. This problem has been discovered in #1146