Sitelet https://github.com/actions/github-script/issues/401
Skip to content

Error: Unhandled error: SyntaxError: Unexpected token '{' #401

Description

@denys-parloa

Hello!

I use this workflow element (To Reproduce section) to display the terraform plan, the variable ${{ steps.show-plan.outputs.stdout }} contains a string with special characters plus json elements.

How to avoid this issue? Probably I need to encode the string somehow ..

Tried including result-encoding: string but same result.
By the way, about a month ago this workflow worked fine.

Describe the bug

SyntaxError: Unexpected token '{'
    at new AsyncFunction (<anonymous>)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15143:16)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15236:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15217:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15268:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15[271](https://github.com/parloa/parloa-infra/actions/runs/5422135137/jobs/9858405881#step:12:272):12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
Error: Unhandled error: SyntaxError: Unexpected token '{'
A clear and concise description of what the bug is.

To Reproduce

   - name: Update Pull Request
      uses: actions/github-script@v6
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        result-encoding: string
        script: |
          const output = `
          <details><summary>Show Plan</summary>
          \`\`\`\n
          ${{ steps.show-workspace.outputs.stdout }}
          ${{ steps.show-var-file.outputs.stdout }}
          ${{ steps.show-plan.outputs.stdout }}
          \`\`\`
          </details>
          `;
          
          github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: output
          })

Desktop (please complete the following information):

  • GitHub Action

Activity

joshmgross commented on Jul 7, 2023

@joshmgross
Contributor

The values you're injecting into that template string are likely invalid Javascript and aren't being escaped correctly.

I'd recommend setting those context values as environment variables and referencing them via process.env and the regular ${} template literal string interpolation.

   - name: Update Pull Request
      uses: actions/github-script@v6
      env:
        SHOW_WORKSPACE_OUTPUT: ${{ steps.show-workspace.outputs.stdout }}
        SHOW_VAR_FILE_OUTPUT: ${{ steps.show-var-file.outputs.stdout }}
        SHOW_PLAN_OUTPUT: ${{ steps.show-plan.outputs.stdout }}
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        result-encoding: string
        script: |
          const output = `
          <details><summary>Show Plan</summary>
          \`\`\`\n
          ${process.env.SHOW_WORKSPACE_OUTPUT}
          ${process.env.SHOW_VAR_FILE_OUTPUT}
          ${process.env.SHOW_PLAN_OUTPUT}
          \`\`\`
          </details>
          `;
          
          github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: output
          })

denys-parloa commented on Jul 10, 2023

@denys-parloa
Author

thank you!
it works.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions