Skip to content

fix(middleware): reset lastIndex before testing a command pattern - #3090

Open
alter wants to merge 1 commit into
slackapi:mainfrom
alter:fix/command-pattern-stateful-regex
Open

alter wants to merge 1 commit into
slackapi:mainfrom
alter:fix/command-pattern-stateful-regex

Conversation

@alter

@alter alter commented Sep 19, 2026

Copy link
Copy Markdown

Closes #1058

matchesPattern in src/middleware/builtin.ts calls pattern.test(candidate) directly on the
RegExp a caller passed to app.command(). RegExp#test() advances lastIndex on a pattern that
carries the g or y flag, and that state lives on the RegExp object itself, not in the call.
Since the same pattern object is reused for every incoming command (the middleware closure
captures it once), a command registered with a g or y flagged pattern matches on the first
invocation, fails to match on the second, matches again on the third, and so on, alternating
forever. The affected listener silently never runs on the failing invocations, so ack() never
gets called and Slack shows the interaction as timed out.

Fix resets pattern.lastIndex to 0 right before calling test(), so every invocation starts from
the same state regardless of what a previous invocation left behind. This only matters for
patterns with the g or y flag; resetting lastIndex on a pattern without either flag is a no-op
since test() ignores it. The two other places in builtin.ts that match a pattern
(matchConstraints and matchMessage) use String#match() instead of RegExp#test(), which does
not carry this same statefulness, so they were not affected and were not touched.

Tested:

  1. New test in test/unit/middleware/builtin.spec.ts: registers matchCommandName(/hi/g) once,
    invokes the resulting middleware twice with the same matching command, and asserts next()
    gets called both times. Confirmed it fails against unmodified code (second invocation
    never calls next()) and passes after the fix.
  2. npm test (build, lint, type tests, unit test coverage): 500 passing, 0 failing, same as
    before this change plus the 1 new test.

@alter
alter requested a review from a team as a code owner September 19, 2026 12:06
@changeset-bot

changeset-bot Bot commented Sep 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2e5657b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@salesforce-cla

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @alter to sign the Salesforce Inc. Contributor License Agreement.

@alter

alter commented Sep 19, 2026

Copy link
Copy Markdown
Author

recheck

@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.38%. Comparing base (672c9fb) to head (2e5657b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3090   +/-   ##
=======================================
  Coverage   94.38%   94.38%           
=======================================
  Files          43       43           
  Lines        7426     7431    +5     
  Branches      704      704           
=======================================
+ Hits         7009     7014    +5     
  Misses        408      408           
  Partials        9        9           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AmyScript
AmyScript force-pushed the fix/command-pattern-stateful-regex branch from 91acd67 to 2e5657b Compare September 22, 2026 15:25

@AmyScript AmyScript 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.

Thanks for the fix! The test is a proper regression test, two invocations with fresh args, assserting next is calledOnce on each which failed before the change and passes after. LGTM!

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: bolt does not trigger handlers consistently when handler pattern contains stateful regex flags (/y or /g)

2 participants