[PR #22054] fix(Sort Node): Fix intermittent "Sort code doesn't return" error #22016

Open
opened 2025-11-20 06:22:44 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/n8n-io/n8n/pull/22054
Author: @pemontto
Created: 11/19/2025
Status: 🔄 Open

Base: masterHead: fix-code-sort


📝 Commits (2)

  • a213ff5 fix(Sort Node): Fix intermittent "Sort code doesn't return" error
  • a749e2c test(Sort): Broaden test coverage for sort code execution

📊 Changes

2 files changed (+195 additions, -1 deletions)

View changed files

📝 packages/nodes-base/nodes/Transform/Sort/test/Sort.test.ts (+192 -0)
📝 packages/nodes-base/nodes/Transform/Sort/utils.ts (+3 -1)

📄 Description

Summary

Fixes #12391

The Sort node's code validation was failing intermittently due to a stateful regex bug. The returnRegExp regex was defined with the global flag (/g), causing it to maintain a lastIndex property that persisted between calls to .test(). This created an alternating pass/fail pattern where valid sort code would be incorrectly rejected on every other execution.

Fixed by:

  • Removing the /g flag from returnRegExp (only need single match check)
  • Adding defensive lastIndex = 0 reset before validation

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

Note

Removes stateful regex usage in sortByCode validation to prevent intermittent "doesn't return" errors and adds unit tests covering validation and sorting behavior.

  • Fix:
    • In packages/nodes-base/nodes/Transform/Sort/utils.ts, replace returnRegExp with non-global /\breturn\b/ and defensively reset lastIndex before .test() to avoid stateful behavior across executions.
  • Tests:
    • Add packages/nodes-base/nodes/Transform/Sort/test/Sort.test.ts covering:
      • Multiple consecutive executions to ensure stable validation.
      • Return statement validation (simple, conditional, multiple, missing, false positives).
      • Sorting behavior (ascending, descending, empty, single item).

Written by Cursor Bugbot for commit a749e2c633. This will update automatically on new commits. Configure here.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/n8n-io/n8n/pull/22054 **Author:** [@pemontto](https://github.com/pemontto) **Created:** 11/19/2025 **Status:** 🔄 Open **Base:** `master` ← **Head:** `fix-code-sort` --- ### 📝 Commits (2) - [`a213ff5`](https://github.com/n8n-io/n8n/commit/a213ff561a33dfa76e60bc138e8c11debcc0fcdf) fix(Sort Node): Fix intermittent "Sort code doesn't return" error - [`a749e2c`](https://github.com/n8n-io/n8n/commit/a749e2c633e6b49f815cdde707eb60daf1235ed4) test(Sort): Broaden test coverage for sort code execution ### 📊 Changes **2 files changed** (+195 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/nodes-base/nodes/Transform/Sort/test/Sort.test.ts` (+192 -0) 📝 `packages/nodes-base/nodes/Transform/Sort/utils.ts` (+3 -1) </details> ### 📄 Description ## Summary Fixes #12391 The Sort node's code validation was failing intermittently due to a stateful regex bug. The `returnRegExp` regex was defined with the global flag (`/g`), causing it to maintain a `lastIndex` property that persisted between calls to `.test()`. This created an alternating pass/fail pattern where valid sort code would be incorrectly rejected on every other execution. Fixed by: - Removing the `/g` flag from `returnRegExp` (only need single match check) - Adding defensive `lastIndex = 0` reset before validation ## Related Linear tickets, Github issues, and Community forum posts ## Review / Merge checklist - [ ] PR title and summary are descriptive. ([conventions](../blob/master/.github/pull_request_title_conventions.md)) <!-- **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** --> - [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [ ] Tests included. <!-- A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. --> - [ ] PR Labeled with `release/backport` (if the PR is an urgent fix that needs to be backported) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Removes stateful regex usage in `sortByCode` validation to prevent intermittent "doesn't return" errors and adds unit tests covering validation and sorting behavior. > > - **Fix**: > - In `packages/nodes-base/nodes/Transform/Sort/utils.ts`, replace `returnRegExp` with non-global `/\breturn\b/` and defensively reset `lastIndex` before `.test()` to avoid stateful behavior across executions. > - **Tests**: > - Add `packages/nodes-base/nodes/Transform/Sort/test/Sort.test.ts` covering: > - Multiple consecutive executions to ensure stable validation. > - Return statement validation (simple, conditional, multiple, missing, false positives). > - Sorting behavior (ascending, descending, empty, single item). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit a749e2c633e6b49f815cdde707eb60daf1235ed4. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the
pull-request
label 2025-11-20 06:22:45 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/n8n#22016
No description provided.