Skip to content

[Bug] helper.emptyDirectory 的 excludes 不会传入递归子目录,嵌套的排除文件仍会被删除 #19745

Backlog

Description

@ywx-sss

问题描述

@tarojs/helper 的 emptyDirectory(dirPath, opts) 支持用 excludes 排除某些文件/目录,但递归处理子目录时调用的是:

emptyDirectory(curPath)

没有把 opts 传下去。因此在子目录里的被排除文件仍然会被删除。

复现步骤

  1. 建目录:root/a/keep.txt、root/a/del.txt、root/top.txt
  2. 调用 emptyDirectory(root, { excludes: ['keep.txt'] })
  3. 检查 root/a/keep.txt

期望行为

excludes 应该对整个目录树生效,root/a/keep.txt 保留,del.txt 和 top.txt 删除。

实际行为

root/a/keep.txt 也被删除,只剩空目录。

本地用一个和 utils.ts 完全相同的 emptyDirectory 实现复现:

exists a/keep.txt: false
exists a/del.txt: false
exists top.txt: false

代码位置

packages/taro-helper/src/utils.ts:369-393:

if (canRemove) {
  emptyDirectory(curPath)
  fs.rmdirSync(curPath)
}

opts 在递归调用里丢失。只把 opts 传下去还不够:CodeRabbit 指出文件删除分支仍会绕过 excludes,保留嵌套排除项还会让祖先目录非空、原有“吞异常重试”可能死循环;全局正则还有 lastIndex 状态问题。

修复 PR:https://github.com/NervJS/taro/pull/19746(文件级 excludes、保留祖先目录、有界重试、重置 lastIndex,并补测试)。

环境

  • Taro 4.3.3(main)
  • Windows
  • Node 24
  • @tarojs/helper 的 emptyDirectory

补充

#12788 是希望 needClearOutput 支持 excludes 的特性请求;这里不是同一个问题,而是 excludes 在嵌套目录下不生效。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions