Skip to content

fix(core): escape literal wildcards and anchor patch insertions - #41335

Open
chirag-gamer wants to merge 2 commits into
anomalyco:devfrom
chirag-gamer:wildcard-patch-fix
Open

fix(core): escape literal wildcards and anchor patch insertions#41335
chirag-gamer wants to merge 2 commits into
anomalyco:devfrom
chirag-gamer:wildcard-patch-fix

Conversation

@chirag-gamer

Copy link
Copy Markdown

Issue for this PR

Closes #41333

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes the two things from #41333.

First, the wildcard matcher in packages/core/src/util/wildcard.ts (and the legacy copy in packages/opencode/src/util/wildcard.ts) had no way to match a literal *, ?, or \ in a permission resource. The only escape character was collapsed to a path separator before the regex was built, so \* and \? always behaved as wildcards. The change protects \*, \?, and \\ before normalization so they match literally; plain \ still works as a path separator. One consequence worth flagging: a Windows-style pattern like C:\Windows\* now means "a file literally named *" instead of "everything in C:\Windows", so patterns should use forward slashes (C:/Windows/*). The tests were updated to use /.

Second, apply_patch ignored the @@ anchor for pure insertions. A chunk with a change context but no - or context lines (oldLines.length === 0) was always appended at the end of the file, so @@ after the imports followed by only + lines landed at the bottom. Insertions now go right after the located context line; chunks without an anchor still append at EOF.

How did you verify your code works?

  • bun test test/wildcard.test.ts test/patch.test.ts in packages/core: 13 pass, 0 fail.
  • bun test test/util/wildcard.test.ts in packages/opencode: the 9 wildcard tests pass (the new escape cases plus the existing glob and slash tests). The package test preload in this checkout fails on a missing drizzle-orm subpath, but the wildcard tests themselves all pass.
  • Checked the escape cases from the issue directly (config\?.json, \*, a\\b) against the updated function to confirm literal matching and no regression on the trailing * command rule.

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Patterns like \* and \? were always treated as wildcards because the only
escape character was collapsed to a path separator before regex escaping.
Protect escaped globs (\\*, \\?, \\\\) first so permission rules can match
resources containing literal *, ? or backslash.
A chunk with a located changeContext but no old lines used to be inserted
at the end of the file regardless of the anchor. Insert after the context
line instead, keeping end-of-file append for chunks without an anchor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wildcard matcher can't escape literal * or ?, and apply_patch ignores @@ anchor for insertions

1 participant