Skip to content

[BUG] temp_dir_create auto-delete overwrites previous EXIT traps #1153

Description

@Ph0enixKM

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions