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

This action logs the contents of every scanned Java file #25

Open
damien-swarm opened this issue Oct 11, 2022 · 2 comments
Open

This action logs the contents of every scanned Java file #25

damien-swarm opened this issue Oct 11, 2022 · 2 comments

Comments

@damien-swarm
Copy link

damien-swarm commented Oct 11, 2022

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 :(

@axel-op
Copy link
Owner

axel-op commented Oct 29, 2022

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
Copy link
Owner

axel-op commented Oct 30, 2022

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

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

2 participants