googlejavaformat-action icon indicating copy to clipboard operation
googlejavaformat-action copied to clipboard

This action logs the contents of every scanned Java file

Open damien-swarm opened this issue 3 years ago • 2 comments

I hope I'm just using this plugin wrong, but when I tried using it with --set-exit-if-changed:

  java_build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-java@v3
      with:
        java-version: 17
        distribution: temurin
        cache: maven
    - name: Check Java formatting
      uses: axel-op/googlejavaformat-action@v3
      with:
        files: |
          **/*.java
          !**/generated/**
        args: "--set-exit-if-changed"

it logs the contents of every single scanned Java file to the Github actions logs (tens of thousands of lines). This is not useful and makes it harder to find actual errors. In fact, in my most recent run, I got Error: Google Java Format failed with exit code 1 without any "Actual vs Expected" errors :(

damien-swarm avatar Oct 11 '22 23:10 damien-swarm

Hello @damien-swarm,

This action is a wrapper around google-java-format which is a JAR executable developed by Google. The action downloads it, executes it, and looks at the return code. If the executable returns a status code != 0, then the action fails.

The behavior you describe is controlled by the executable, not by this action. You can replicate it by downloading the program on your computer and running it on the command line.

If we look at the code of google-java-format, we see that when we don't explicitly ask to update the files "in place", the default behavior is to print their updated content on stdout. More precisely:

  • if we add the --replace parameter, the content of the file is updated "in place";
  • if we add the --dry-run parameter, the path of the file is printed;
  • else, the content of the file is printed.

Note that files already formatted properly are not printed.

So, to solve your issue, you could either:

  • print only the paths of the incorrectly formatted files using --dry-run;
  • or reformat the files using --replace, and print their diff in a subsequent step of your workflow.

axel-op avatar Oct 29 '22 19:10 axel-op

Here's an example showing how you can print the diff of every file modified by the action (I also added it in the README):

name: Format

on: [ push, pull_request ]

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3 # v2 minimum required
      - uses: axel-op/googlejavaformat-action@v3
        with:
          args: "--replace"
          skip-commit: true
      - name: Print diffs
        # "--exit-code" will make this command exit with code 1 if there is a diff
        run: git --no-pager diff --exit-code

axel-op avatar Oct 30 '22 00:10 axel-op