修文件探查脚本在"自动查找目录"分支崩溃,并把整个脚本端到端跑通

现场报"集合的大小是固定的":-Roots 是 [string[]] 定长数组,我的代码却往它 Add,
而这个分支(自动查找目录)此前从未被执行过——演练只加载函数、不跑主流程,所以没暴露。

- 要探查的目录改放自己的 ArrayList,不再改参数本身;
- 用 -File 传多个目录时可能被当成一个字符串,按逗号再拆一次(并去掉引号与空白);
- 本机把整个脚本端到端跑了两遍(自动查找 + 指定目录),并核对了报告文件落盘;
函数级演练(含 .pcj 解析)继续通过。
1个文件已修改
19 ■■■■■ 已修改文件
scripts/j22-introspect-files.ps1 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
scripts/j22-introspect-files.ps1
@@ -143,6 +143,8 @@
try {
    # ---------------------------------------------------------------- 1. 要找哪些目录
    # 要探查的目录放进自己的列表:-Roots 是 [string[]] 定长数组,不能往里 Add。
    $targets = New-Object System.Collections.ArrayList
    if (-not $Roots -or $Roots.Count -eq 0) {
        Say '=== 1. 自动查找目录 ==='
        $found = New-Object System.Collections.ArrayList
@@ -164,17 +166,26 @@
        }
        foreach ($directory in $found) {
            Say ("  候选目录:" + $directory)
            [void]$Roots.Add($directory)
            [void]$targets.Add($directory)
        }
        if ($Roots.Count -eq 0) {
        if ($targets.Count -eq 0) {
            Say '  没有找到任何候选目录。请手工指定,例如:-Roots ''D:\<软件目录>'''
        }
    }
    else { Say '=== 1. 指定目录 ===' }
    else {
        Say '=== 1. 指定目录 ==='
        foreach ($root in $Roots) {
            # 用 -File 从命令行传数组时,多个目录可能被当成一个字符串('D:\a,D:\b'),按逗号再拆一次。
            foreach ($single in @($root -split ',')) {
                $trimmed = $single.Trim().Trim('"')
                if ($trimmed) { Say ("  指定目录:" + $trimmed); [void]$targets.Add($trimmed) }
            }
        }
    }
    # ---------------------------------------------------------------- 2. 方案候选文件
    $candidates = New-Object System.Collections.ArrayList
    foreach ($root in $Roots) {
    foreach ($root in $targets) {
        if (-not (Test-Path -LiteralPath $root)) { Say ("  目录不存在:" + $root); continue }
        Say ''
        Say ("=== 2. 方案候选文件:" + $root + " ===")