Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unexpected token 'var' #220

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

Closed
tjbaker opened this issue Nov 30, 2021 · 10 comments
Closed

Unexpected token 'var' #220

tjbaker opened this issue Nov 30, 2021 · 10 comments

Comments

@tjbaker
Copy link

tjbaker commented Nov 30, 2021

I am trying to comment on a PR via a workflow and keep getting this exception:

SyntaxError: Unexpected token 'var'
    at new AsyncFunction (<anonymous>)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4942:56)
    at main (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4997:26)
    at Module.272 (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4981:1)
Error: Unhandled error: SyntaxError: Unexpected token 'var'
    at __webpack_require__ (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:24:31)
    at startup (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:43:19)
    at /home/runner/work/_actions/actions/github-script/v5/dist/index.js:49:18
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:52:10)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)

The code is really simple

      - name: Comment on PR
        uses: actions/github-script@v5
        if: failure()
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "foo"
            })

Github support asked me to log a bug report here as well.

@joshmgross
Copy link
Member

👋 Hey @tjbaker

What event is this workflow triggering on?

It's possible that context.issue.number isn't valid.

If it's helpful, you can print out the full context.issue:

      - name: Comment on PR
        uses: actions/github-script@v5
        if: failure()
        with:
          script: |
            console.log(JSON.stringify(context.issue))
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "foo"
            })

@tjbaker
Copy link
Author

tjbaker commented Dec 8, 2021

It is triggered on pull_request

on:
  pull_request:
    branches: [ master ]

I can't try the additional debugging just yet...

@tjbaker
Copy link
Author

tjbaker commented Dec 9, 2021

Ok, I think the actual problem is with using a multiline output from a previous step's stdout as the body in createComment.

The problem is trying to make hashicorp/setup-terraform work with github-script@v5 and github.rest.issues.createComment. There is an example in the readme, but it is against actions/[email protected]
https://github.com/hashicorp/setup-terraform

@cemo
Copy link

cemo commented Jan 7, 2022

@tjbaker did you resolve your issue?

@joshmgross
Copy link
Member

Ok, I think the actual problem is with using a multiline output from a previous step's stdout as the body in createComment.

If you need to pass into multi-line outputs into the script block, you'll likely need to pass those values in as environment variables. Otherwise, newlines will be interpreted literally in the script and won't be valid JavaScript.

Example:

- uses: actions/github-script@v5
  env: 
    MULTILINE_VALUE: ${{ steps.my_step.outputs.value }}
  with:
    script: |
        github.issues.createComment({
          issue_number: context.issue.number,
          owner: context.repo.owner,
          repo: context.repo.repo,
          body: process.env.MULTILINE_VALUE
        })

@jsumners
Copy link

I am having this issue where the error message is:

SyntaxError: Invalid or unexpected token
    at new AsyncFunction (<anonymous>)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:4797:16)
...

My step looks like:

      - name: Post comment
        uses: actions/github-script@v6
        with:
          script: |
            const comment = '
              Data for `${{ steps.get_id.outputs.an_id }}`:

              ```json
              ${{ steps.get_data.outputs.result }}
              ```';
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: comment
            })

Where steps.get_data.outputs.result is a large JSON object with formatting.

@joshmgross
Copy link
Member

@jsumners are you able to pass that JSON object as an environment variable instead?

@jsumners
Copy link

No. I moved on to writing the data to a file with JavaScript and then curl -X POST ... -d @the.file in order to get around this problem.

@joshmgross
Copy link
Member

joshmgross commented Feb 15, 2022

Going to close this issue out. If anyone sees an error like this, it usually means that the JavaScript is invalid in some way.

Usually this occurs when using the Actions expression syntax (${{ }}) in input a value that's then interpreted by the action as invalid JavaScript (ex. newlines, semi-colons, quotes, etc).

The workaround is to process this input indirectly, rather than inserting it directly into your script. This can be done by writing it to a file or using an env variable and reading it with process.env, see #220 (comment)

Feel free to open a new issue if you're still having issues after trying those workarounds.

@salecharohit
Copy link

So I encountered this same issue and was able to resolve it using below as per @joshmgross's recommendation

  - name: Plan Output
    uses: actions/github-script@v6
    if: github.event_name == 'pull_request'
    env:
      PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
    with:
      github-token: ${{ secrets.GITHUB_TOKEN }}
      script: |
        const output = `## Terraform Prod Infra Plan
        #### Terraform Initialization \`${{ steps.init.outcome }}\`
        #### Terraform Validation \`${{ steps.validate.outcome }}\`
        #### Terraform Plan \`${{ steps.plan.outcome }}\`
        
        <details><summary>Show Plan</summary>
        ${process.env.PLAN}
        </details>

        *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;

        github.rest.issues.createComment ({
          issue_number: context.issue.number,
          owner: context.repo.owner,
          repo: context.repo.repo,
          body: output
        })

eskultety added a commit to eskultety/hermeto that referenced this issue Dec 9, 2024
This workflow is a direct consequence of the asynchronous release
schedule of pydantic and pydantic core and the fact that pydantic is
always pinned to a particular pydantic-core version. Dependabot doesn't
see these transitive relations and so can't properly update the
versions in this case (it always assumes the latest for every
dependency). This will naturally lead to broken CI making these version
updates impossible to merge.

Since our project directly only cares about pydantic and not
pydantic-core, we can ignore pydantic-core updates (future patch) and
run a dedicated workflow on every dependabot pull request that would
check whether changes to our requirements files are needed. If changes
are needed, the GitHub actions bot will comment on the pull request
that a change to these files are needed and provide a patch to the
reviewer to apply and update the pull request.

The workflow is only executed when changes to the requirements files
are proposed (realistically only by dependabot). Note that it's not
possible to specify the source branch as the workflow trigger, only the
target branch, and so that could not have been used as a better filter
for dependabot-proposed pull requests specifically. It is run using a
Python Alpine docker image, saves the git diff produced by pip-compile
to the default github actions environment followed by a github script
action that will pop the diff out of the environment and use it to
comment on the pull request.

References:
- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/using-conditions-to-control-job-execution
- https://github.com/actions/github-script?tab=readme-ov-file#comment-on-an-issue
- actions/github-script#247 (comment)
- actions/github-script#220 (comment)

Signed-off-by: Erik Skultety <[email protected]>
eskultety added a commit to eskultety/hermeto that referenced this issue Jan 3, 2025
This workflow is a direct consequence of the asynchronous release
schedule of pydantic and pydantic core and the fact that pydantic is
always pinned to a particular pydantic-core version. Dependabot doesn't
see these transitive relations and so can't properly update the
versions in this case (it always assumes the latest for every
dependency). This will naturally lead to broken CI making these version
updates impossible to merge.

Since our project directly only cares about pydantic and not
pydantic-core, we can ignore pydantic-core updates (future patch) and
run a dedicated workflow on every dependabot pull request that would
check whether any additional changes (i.e. transitive dependency
version locks) to our requirements files are needed. If so, then the
GitHub actions bot will comment on the pull request that a change to
these files is needed and will provide a patch to the reviewer to apply
and update the pull request.

The workflow is only executed when changes to the requirements files
are proposed (realistically only by dependabot). Note that it's not
possible to specify the source branch as the workflow trigger, only the
target branch, and so that could not have been used as a better filter
for dependabot-proposed pull requests specifically. It is run using a
Python Alpine docker image, saves the git diff produced by pip-compile
to the default github actions environment followed by a github script
action that will pop the diff out of the environment and use it to
comment on the pull request.

References:
- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/using-conditions-to-control-job-execution
- https://github.com/actions/github-script?tab=readme-ov-file#comment-on-an-issue
- actions/github-script#247 (comment)
- actions/github-script#220 (comment)

Signed-off-by: Erik Skultety <[email protected]>
github-merge-queue bot pushed a commit to hermetoproject/hermeto that referenced this issue Jan 6, 2025
This workflow is a direct consequence of the asynchronous release
schedule of pydantic and pydantic core and the fact that pydantic is
always pinned to a particular pydantic-core version. Dependabot doesn't
see these transitive relations and so can't properly update the
versions in this case (it always assumes the latest for every
dependency). This will naturally lead to broken CI making these version
updates impossible to merge.

Since our project directly only cares about pydantic and not
pydantic-core, we can ignore pydantic-core updates (future patch) and
run a dedicated workflow on every dependabot pull request that would
check whether any additional changes (i.e. transitive dependency
version locks) to our requirements files are needed. If so, then the
GitHub actions bot will comment on the pull request that a change to
these files is needed and will provide a patch to the reviewer to apply
and update the pull request.

The workflow is only executed when changes to the requirements files
are proposed (realistically only by dependabot). Note that it's not
possible to specify the source branch as the workflow trigger, only the
target branch, and so that could not have been used as a better filter
for dependabot-proposed pull requests specifically. It is run using a
Python Alpine docker image, saves the git diff produced by pip-compile
to the default github actions environment followed by a github script
action that will pop the diff out of the environment and use it to
comment on the pull request.

References:
- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/using-conditions-to-control-job-execution
- https://github.com/actions/github-script?tab=readme-ov-file#comment-on-an-issue
- actions/github-script#247 (comment)
- actions/github-script#220 (comment)

Signed-off-by: Erik Skultety <[email protected]>
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

No branches or pull requests

5 participants