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

Looping over files with spaces. #609

Closed
preritdas opened this issue Sep 4, 2022 · 7 comments · Fixed by #611
Closed

Looping over files with spaces. #609

preritdas opened this issue Sep 4, 2022 · 7 comments · Fixed by #611

Comments

@preritdas
Copy link

preritdas commented Sep 4, 2022

Hi, I've been looking through #216 and related information, including the use of IFS in BASH. Unfortunately, after countless attempts, I can't seem to get it to work with this action. I'm not sure if it's an issue with this action, a behavior of GitHub actions, or an error in my implementation.

I'm opening this issue because I tried the same script on my Ubuntu server and it worked like a charm.

Attempt

name: pdf render

on:
  push:
    paths:
      - '**/**.md'


jobs:
  render:

    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v29.0.3
        with:
          files: "**/**.md"
          separator: ","

      - name: List all changed files
        run: |
          echo ${{ steps.changed-files.outputs.all_changed_files }}
          IFS=","
          echo ".$IFS."
          for file in ${{ steps.changed-files.outputs.all_changed_files }}
          do echo $file was changed
          done
          unset IFS

Response

Folder/Sub Folder/First Markdown File.md,Folder/Other Subfolder/Another Markdown File.md
.,.
Folder/Sub was changed
Folder/First was changed
Markdown was changed
File.md was changed
Folder/Other was changed
Subfolder/Another was changed
Markdown was changed
File.md was changed

Expected

Folder/Sub Folder/First Markdown File.md,Folder/Other Subfolder/Another Markdown File.md
.,.
Folder/Sub Folder/First Markdown File.md was changed
Folder/Other Subfolder/Another Markdown File.md was changed

It seems the IFS is being changed because when I echo ".$IFS." after setting IFS=",", the shell prints .,. as expected. But when I loop, no change.

As I mentioned earlier, I tried this on my own Ubuntu server and it worked

output="Folder/Sub Folder/First Markdown File.md,Folder/Other Subfolder/Another Markdown File.md"
IFS=","
for file in $output
do echo $file
done
unset IFS

Which outputted...

Folder/Sub Folder/First Markdown File.md was changed
Folder/Other Subfolder/Another Markdown File.md was changed

Here is a shell recording of this working on WSL Ubuntu.

asciicast

I apologize if I missed something basic and opened an issue when unnecessary. That said, I've tried countless variations of the script and looked through pretty much every mention of "space" and "IFS" in this repo and codebase, and haven't been able to resolve the issue. When the same script worked on my Ubuntu server, I decided it best to open this "issue." Thanks sincerely for any help.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2022

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

@jackton1
Copy link
Member

jackton1 commented Sep 4, 2022

@preritdas Can you try using IFS=$',' instead ?

@preritdas
Copy link
Author

Didn't work, sadly. Same code but IFS=$',' instead of IFS=",".

Output:

Folder Space/Sub Folder/File With Spaces.md
.,.
Folder was changed
Space/Sub was changed
Folder/File was changed
With was changed
Spaces.md was changed

@jackton1
Copy link
Member

jackton1 commented Sep 5, 2022

@preritdas Can you also put quotes around the output


      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v29.0.3
        with:
          files: "**/**.md"
          separator: ","

      - name: List all changed files
        run: |
          echo “${{ steps.changed-files.outputs.all_changed_files }}“
          IFS=$','
          for file in “${{ steps.changed-files.outputs.all_changed_files }}“
          do echo “$file was changed”
          done
          unset IFS

@preritdas
Copy link
Author

No cigar unfortunately. When wrapping the output in quotes, the entire output is treated as one file. Output below.

Folder Space/Sub Folder/Markdown File.md Another Folder/Another Sub/Another Markdown.md Folder Three/More Stuff/Important File.md Nothing To/See Here/Nuclear Codes.md
.,.
Folder Space/Sub Folder/Markdown File.md Another Folder/Another Sub/Another Markdown.md Folder Three/More Stuff/Important File.md Nothing To/See Here/Nuclear Codes.md was changed

@jackton1 jackton1 linked a pull request Sep 5, 2022 that will close this issue
@jackton1
Copy link
Member

jackton1 commented Sep 5, 2022

@preritdas Seems you'll need to tweak the code you have to resolve the issue.

For example, you'll need to use

...
      - name: List all modified files
         run: |
           IFS=$',' read -a MODIFIED_FILES_ARRAY <<< "${{ steps.changed-files-comma.outputs.modified_files }}"
           for file in "${MODIFIED_FILES_ARRAY[@]}"; do
             echo $file
           done
           unset IFS
         shell:
           bash

See this PR for more information.

@jackton1 jackton1 closed this as completed Sep 5, 2022
@preritdas
Copy link
Author

Yep, that does the trick. I appreciate the help.

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

Successfully merging a pull request may close this issue.

2 participants