Skip to content

make @file references in custom slash commands more robust - #2203

Merged
rekram1-node merged 3 commits into
anomalyco:devfrom
aspiers:fix-fileRegex
Sep 2, 2025
Merged

rekram1-node merged 3 commits into
anomalyco:devfrom
aspiers:fix-fileRegex

Conversation

@aspiers

@aspiers aspiers commented Aug 23, 2025 •

Copy link
Copy Markdown
Contributor

The regular expression which was extracting file paths prefixed by @ from custom command text was too simplistic and created bugs. This PR makes it correctly handle several cases. See https://regex101.com/r/F6KAgH/ for examples.

Fixes #2198.

@aspiers

aspiers commented Aug 23, 2025

Copy link
Copy Markdown
Contributor Author

Looks like there's a broken workflow file? https://github.com/sst/opencode/actions/runs/17176454619/workflow

Also I'm seeing brokenness with bun test in general; would appreciate some help in #dev on Discord if possible... 🙏

@rekram1-node

Copy link
Copy Markdown
Collaborator

feel free to dm

@thdxr

thdxr commented Aug 29, 2025

Copy link
Copy Markdown
Member

hey sorry i was slow to look at this - looks good can you resolve the conflict?

@art-shen art-shen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly believe that regexes are a dead end here, and Markdown should be properly parsed instead to fix the error once and for all.

For example, afaik this would fail again with the same initial problem on my example command from #2327, wouldn't it?

Seems like the new regex would help with the first occurrence but still fail for the second). Because by design, regexes aren't suitable to properly handle languages like Markdown or any other complex syntax.


Example snippet from my command (I'm symlinking the commands from Claude Code):

  1. Run this to show a context to the user:
     `gh pr list --state open --json number,title,headRefName,author,comments,reviews,additions,deletions,isDraft,updatedAt --jq '.[] | "\("#" + (.number | tostring) | .[0:4] + " " * (4 - length))  \(if .isDraft then "📝 " else "✨ " end)\(.title | .[0:80] + " " * (80 - (.[0:80] | length)))  @\(.author.login | .[0:12] + " " * (12 - (.[0:12] | length)))  \(.headRefName | .[0:30] + " " * (30 - (.[0:30] | length)))  💬\(.comments | length)/🧵\((.reviews | length | tostring) + " " * (2 - (.reviews | length | tostring | length)))  +\((.additions | tostring) + " " * (4 - (.additions | tostring | length)))/-\((.deletions | tostring) + " " * (4 - (.deletions | tostring | length)))  \(.updatedAt[0:10])"'`
     Shows: `PR# ✨/📝 Title(80ch) @author branch(30ch) 💬comments/🧵reviews +adds/-dels date`

From this specific case

  1. it was trying to interpret @\(.author.login as a file reference (would be probably solved with this PR)
  2. but the same problem is for the @author on the next line in the middle the inline code.

@aspiers

aspiers commented Aug 30, 2025

Copy link
Copy Markdown
Contributor Author

@amal I've replied in #2198 to explain why I agree that Markdown parsing is a good longer-term step but disagree that regexes are a dead-end. I'll try to rebase now.

@aspiers

aspiers commented Aug 30, 2025

Copy link
Copy Markdown
Contributor Author

@thdxr Resolved conflicts, made everything cleaner, and added test cases for paths beginning ~/ and . (i.e. hidden files).

Adam Spiers and others added 3 commits September 1, 2025 21:02
This will make it easier to test, and also allow adding more sophisticated
extraction logic without bloating `session/index.ts`.
This is a @valid/path/to/a/file, and it should also match at the beginning of a
line:

@another-valid/path/to/a/file

but references to email addresses and other strings are not:

   - Adds a "Co-authored-by:" footer which clarifies which AI agent
     helped create this commit, using an appropriate `noreply@...`
     email address.

However currently the fileRegex is `/@([^\s]+)/g` which is much too inclusive
and triggers false positives.

As a first approximation, we can use a negative look-behind assertion to filter
out any matches for which the `@` is immediately preceded by an alphanumeric
character, and then also impose some heuristics regarding use of backticks,
commands, periods, and whitespace.

This can be seen as functional at: https://regex101.com/r/F6KAgH/5

Fixes anomalyco#2198.
@rekram1-node
rekram1-node merged commit 47d4c87 into anomalyco:dev Sep 2, 2025
@aspiers
aspiers deleted the fix-fileRegex branch September 2, 2025 13:35
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request May 5, 2026
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026
meSingh pushed a commit to meSingh/opencode that referenced this pull request Sep 6, 2026
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.

fileRegex for @ file references within custom commands is far too inclusive

4 participants