Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core)!: $(...).[last,first,all]() defaulting to the first output instead of the output that connects the nodes #9760

Merged
merged 3 commits into from
Jun 19, 2024

Conversation

despairblue
Copy link
Contributor

@despairblue despairblue commented Jun 14, 2024

Summary

This PR makes $(...).[last|first|all]() default to the the output the node was connected to.

Before it would default to the first output.

Example Workflow

{
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "6944c3a4f9d5af1d486eb3bd76ceeae78d947e96a0d9e6c404bb1bcec6a36054"
  },
  "nodes": [
    {
      "parameters": {
        "path": "e9586c98-e50e-43fb-9c6b-2ff1deaaa0c9",
        "options": {}
      },
      "id": "669a68fd-cc30-45ff-88f4-6a5455bcf66a",
      "name": "Webhook1",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        180,
        700
      ],
      "webhookId": "e9586c98-e50e-43fb-9c6b-2ff1deaaa0c9"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "1fff886f-3d13-4fbf-b0fb-7e2f845937c0",
              "leftValue": "={{ true }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "588e25bd-940c-498b-91e3-d768f45cc616",
      "name": "true",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        460,
        460
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "1fff886f-3d13-4fbf-b0fb-7e2f845937c0",
              "leftValue": "={{ false }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "dddc1d50-495c-403e-a17a-5305a3b277a8",
      "name": "false",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        460,
        660
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "1fff886f-3d13-4fbf-b0fb-7e2f845937c0",
              "leftValue": "={{ true }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "b794c9be-7d16-47fb-9a5d-d3816c44e247",
      "name": "false1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        460,
        860
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "41531ffa-e84c-4843-a401-45b93d520f97",
              "name": "test",
              "value": "={{ $('false').last().json.code }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "56928338-14d7-4e70-8979-d091fc8da23e",
      "name": "Edit fields 2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        840,
        660
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "41531ffa-e84c-4843-a401-45b93d520f97",
              "name": "test",
              "value": "={{ $('true').last().json.code }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "c71938b7-fe93-4a6a-8c90-10ef02f1d56f",
      "name": "Edit fields 1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        840,
        460
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "41531ffa-e84c-4843-a401-45b93d520f97",
              "name": "test",
              "value": "={{ $('false1').last().json.code }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "aab4f1c7-465b-474b-b769-4afd10232f1a",
      "name": "Edit fields 3",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        840,
        860
      ]
    }
  ],
  "connections": {
    "Webhook1": {
      "main": [
        [
          {
            "node": "true",
            "type": "main",
            "index": 0
          },
          {
            "node": "false",
            "type": "main",
            "index": 0
          },
          {
            "node": "false1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Edit fields 3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "true": {
      "main": [
        [
          {
            "node": "Edit fields 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "false": {
      "main": [
        null,
        [
          {
            "node": "Edit fields 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "false1": {
      "main": [
        null,
        [
          {
            "node": "Edit fields 3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {
    "Webhook1": [
      {
        "name": "First item",
        "code": 1
      },
      {
        "name": "Second item",
        "code": 2
      }
    ]
  }
}

Before it would always evaluate output 0 (true branch)
image

Now it will take the output that connects the nodes. If the user wants another output they'd have to specify if like $(...).last(1)
image

Workflows that rely on something like the third example will break with this change.
Workflows that rely on last, first and all always returning the first output will also break.

The ticket is marked as a bug, meaning this was never the intended behavior. I marked this as a breaking change regardless.

We'd also want to update the docs to make it clear how those functions work: https://docs.n8n.io/code/builtin/output-other-nodes/

Related Linear tickets, Github issues, and Community forum posts

https://linear.app/n8n/issue/PAY-1400/bug-dollarlast-uses-wrong-branch
n8n-io/n8n-docs#2167

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)

… instead of the output that connects the nodes
@despairblue despairblue changed the title fix(core): $(...).[last,first,all]() defaulting to the first output instead of the output that connects the nodes fix(core)!: $(...).[last,first,all]() defaulting to the first output instead of the output that connects the nodes Jun 14, 2024
@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Jun 14, 2024
@despairblue despairblue marked this pull request as ready for review June 14, 2024 21:18
krynble
krynble previously approved these changes Jun 18, 2024
Copy link
Contributor

@krynble krynble left a comment

Choose a reason for hiding this comment

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

Thanks for addressing this bug and adding the tests! Great work!

Copy link

cypress bot commented Jun 18, 2024

4 flaky tests on run #5580 ↗︎

0 395 0 0 Flakiness 4

Details:

🌳 🖥️ browsers:node18.12.0-chrome107 🤖 despairblue 🗃️ e2e/*
Project: n8n Commit: b8db4f7dbe
Status: Passed Duration: 04:46 💡
Started: Jun 19, 2024 9:32 AM Ended: Jun 19, 2024 9:37 AM
Flakiness  5-ndv.cy.ts • 2 flaky tests

View Output Video

Test Artifacts
NDV > should not retrieve remote options when required params throw errors Screenshots Video
NDV > Stop listening for trigger event from NDV Screenshots Video
Flakiness  10-undo-redo.cy.ts • 1 flaky test

View Output Video

Test Artifacts
Undo/Redo > should undo/redo adding connected nodes Test Replay Screenshots Video
Flakiness  30-editor-after-route-changes.cy.ts • 1 flaky test

View Output Video

Test Artifacts
Editor actions should work > after saving a new workflow Test Replay Screenshots Video

Review all test suite changes for PR #9760 ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

Copy link
Contributor

@ivov ivov left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this! If this is a breaking change, let's please update the breaking changes doc as well. Else let's remove ! from the title.

@ivov
Copy link
Contributor

ivov commented Jun 18, 2024

Follow-up: https://linear.app/n8n/issue/PAY-1692

@despairblue despairblue requested a review from ivov June 19, 2024 09:18
Copy link
Contributor

✅ All Cypress E2E specs passed

@despairblue despairblue merged commit 4ac9266 into master Jun 19, 2024
26 checks passed
@despairblue despairblue deleted the pay-1400-bug-last-uses-wrong-branch branch June 19, 2024 13:10
adrian-martinez-onestic pushed a commit to onesdata/n8n-fork that referenced this pull request Jun 20, 2024
adrian-martinez-onestic pushed a commit to onesdata/n8n-fork that referenced this pull request Jun 20, 2024
@janober
Copy link
Member

janober commented Jun 20, 2024

Got released with [email protected]

1 similar comment
@janober
Copy link
Member

janober commented Jun 20, 2024

Got released with [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants