amber-lang / amber-lang/amber

[BUG] `temp_dir_create` auto-delete overwrites previous EXIT traps

Aperta
#1,153 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
Rust
Stelle
5.2k
Fork
145
Merge medio
5g 3h
PR unite (30g)
7

Descrizione

## 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:

```amber
$ trap 'rm -rf {filename}' EXIT $
```

Shells maintain one handler per signal, so every new `trap ... EXIT` replaces the previous handler.

## Reproduction

```amber
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:

```bash
__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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.