amber-lang / amber-lang/amber

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

未关闭
#1,153 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Rust
星标
5.2k
派生
145
平均合并
5 天 3 小时
30 天内合并 PR
7

描述

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

贡献指南

这个仓库没有索引到贡献指南

调研方向

Start at the std/fs implementation of temp_dir_create and inspect the tests that create multiple temporary directories. Trace how the generated shell installs the EXIT trap, then verify that all directories registered with auto_delete are removed on exit and that the relevant tests pass.

由索引模型根据 Issue 内容生成。

评估

技术栈
bash, rust
领域
compilers
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
63/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。