Skip to content

Regular expression patterns are mistaken for directories #797

@prplecake

Description

@prplecake

Describe the bug

When the agent comes up with some PowerShell to find files, it'll often come up with something like this:

     # Find all logger writes in catch blocks that are NOT Error level
     $projectPath = "C:\Users\prplecake\Projects\[redacted]"
     $files = Get-ChildItem -Path $projectPath -Recurse -Include "*.cs" -File | Where-Object {
         $_.FullName -notlike "*\obj\*" -and
         $_.FullName -notlike "*\bin\*" -and
         $_.FullName -like "$projectPath\*"
     }

     $results = @()

     foreach ($file in $files) {
         $lines = Get-Content $file.FullName
         $inCatch = $false
         $catchStart = 0
         $braceCount = 0

         for ($i = 0; $i -lt $lines.Count; $i++) {
             $line = $lines[$i]

             if ($line -match '\bcatch\b') {
                 $inCatch = $true
                 $catchStart = $i + 1
                 $braceCount = 0
             }

             if ($inCatch) {
                 # Count braces to track scope
                 $openBraces = ([regex]::Matches($line, '\{')).Count
                 $closeBraces = ([regex]::Matches($line, '\}')).Count
                 $braceCount += $openBraces - $closeBraces

                 # Check for logger statements that are NOT Error
                 if ($line -match '(logger|_logger|Log)\.(Information|Warning|Debug|Verbose|Fatal)\(') {
                     $results += [PSCustomObject]@{
                         File = $file.FullName.Replace("$projectPath\", "")
                         Line = $i + 1
                         Content = $line.Trim()
                     }
                 }

                 if ($braceCount -le 0 -and $i -gt $catchStart) {
                     $inCatch = $false
                 }
             }
         }
     }

     if ($results.Count -gt 0) {
         Write-Host "Found $($results.Count) logger statements in catch blocks that are NOT Error level:"
         Write-Host ""
         $results | Format-Table -Wrap -AutoSize
     } else {
         Write-Host "No non-Error logger statements found in catch blocks."
     }

and then later, Copilot prompts me for additional directory access:

○ Find non-Error logger statements in catch blocks
   $
   # Find all logger writes in catch blocks that are NOT Error level
   $projectPath = "C:\Users\prplecake\Projects\[redacted]"
   $files = Get-ChildItem -Path $projectPath -Recurse -Include "*.cs" -File | Where-Object {
       $_.FullName -notlike "*\obj\*" -and ...
    └ 51 lines...

 ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ Allow directory access                                                                                             │
 │                                                                                                                    │
 │ Copilot is attempting to read the following paths outside your allowed directory list.                             │
 │                                                                                                                    │
 │ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
 │ │ \bcatch\b, \{, \}                                                                                              │ │
 │ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
 │                                                                                                                    │
 │ Do you want to add these directories to the allowed list?                                                          │
 │                                                                                                                    │
 │ ❯ 1. Yes                                                                                                           │
 │   2. No (Esc)                                                                                                      │
 │                                                                                                                    │
 │ Confirm with number keys or ↑↓ keys and Enter, Cancel with Esc                                                     │
 ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
‌

Affected version

0.0.366 Commit: 0c18d21

Steps to reproduce the behavior

  1. Make some request that causes Copilot to want to write a PowerShell script with regular expressions in it.
  2. Let Copilot run the script.
  3. Get prompted for additional directory access for those regular expression patterns.

Expected behavior

  1. Don't consider everything between two \ as a path. or
  2. Add an option to "No these aren't directories, continue anyway."

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions