tj-actions / changed-files

:octocat: Github action to retrieve all (added, copied, modified, deleted, renamed, type changed, unmerged, unknown) files and directories.
MIT License
1.85k stars 191 forks source link

[BUG] Bad JSON escape sequence #2350

Closed TimatGDC closed 1 week ago

TimatGDC commented 1 week ago

Is there an existing issue for this?

Does this issue exist in the latest version?

Describe the bug?

I was trying to run the workflow using your matrix example:

name: 🕵️ Detect Credentials on PR

on:
  pull_request:

jobs:
  changed-files:
    name: Get changed files
    runs-on: ubuntu-24.04
    outputs:
      matrix: ${{ steps.changed-files.outputs.all_changed_files }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v45
        with:
          matrix: true
          since_last_remote_commit: true
      - name: List all changed files
        run: echo '${{ steps.changed-files.outputs.all_changed_files }}'

  matrix-job:
    name: Run Matrix Job
    runs-on: ubuntu-24.04
    needs: [changed-files]
    strategy:
      matrix: 
        files: ${{ fromJSON(needs.changed-files.outputs.matrix) }}
      max-parallel: 4
      fail-fast: false
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Test
        run: |
          echo ${{ matrix.files }}

The goal is to list only the changed file in this PR.

My team uses Next.js, and as a convention, it generate folder with parenthesis. See here. But, when i test it, i ran into an issue: Bad JSON escape sequence

To Reproduce

  1. Create a folder with parenthesis, for example (auth)
  2. Create a file inside that folder, write on it and push it to activate the workflow
  3. There will be a Bad JSON escape sequence error due to the escaped parenthesis in the path of the folder

What OS are you seeing the problem on?

ubuntu-24.04

Expected behavior?

In the list of changed files:

[".github/workflows/\(folder\)/testfile.txt",".github/workflows/other-test-file.txt",".github/workflows/plateform-auto-detect-credential-pr.yml"]

.github/workflows/\(folder\)/testfile.txt" should be .github/workflows/(folder)/testfile.txt"

Relevant log output

# Log for "List All Changed Files"

[".github/workflows/\(folder\)/testfile.txt",".github/workflows/other-test-file.txt",".github/workflows/plateform-auto-detect-credential-pr.yml"]

# Error For Matrix Job

Error when evaluating 'strategy' for job 'matrix-job'. .github/workflows/plateform-auto-detect-credential-pr.yml (Line: 31, Col: 16): Error parsing fromJson,.github/workflows/plateform-auto-detect-credential-pr.yml (Line: 31, Col: 16): Bad JSON escape sequence: \(. Path '', line 1, position 22.,.github/workflows/plateform-auto-detect-credential-pr.yml (Line: 31, Col: 16): Unexpected value ''

Has all relevant logs been included?

Anything else?

No response

Code of Conduct

tj-actions-bot commented 1 week ago

Thanks for reporting this issue, don't forget to star this project if you haven't already to help us reach a wider audience.

jackton1 commented 1 week ago

Hi @TimatGDC, if you use parenthesis () in the folder name, you would need to set safe_output to false to prevent unsafe shell characters from being escaped.

TimatGDC commented 1 week ago

Oh you're right, did not see that one. I was searching for "escaping" in the doc, and not "sanitize", my bad 😅 . Thanks for the help