nshki / chusaku

Annotate your Rails controllers with route info.
https://rubygems.org/gems/chusaku
MIT License
89 stars 5 forks source link

Unexpected Github Actions Output #31

Closed Numie closed 2 years ago

Numie commented 2 years ago

Absolutely loving this gem! In order to use in production, we want to implement a Github Actions check to validate that route annotations are up to date. When attempting to implement the workflow, I ran chusaku in the root directory but reverted the changes in one file to test the workflow.

Expected output:

Chusaku has finished running.
This was a dry run so no files were changed.
Exited with status code 1.

Output when running locally:

$ bundle exec chusaku --dry-run --exit-with-error-on-annotation
Chusaku has finished running.
This was a dry run so no files were changed.
Exited with status code 1.

Output in Github Actions Workflow:

Run bundle exec chusaku --dry-run --exit-with-error-on-annotation
  bundle exec chusaku --dry-run --exit-with-error-on-annotation
  shell: /usr/bin/bash -e {0}
Nothing to annotate.

Github Action Workflow:

name: Chusaku Check

on: pull_request

jobs:
  chusaku:
    name: Chusaku
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3.0.2
      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true

      - name: Run Chusaku
        run: bundle exec chusaku --dry-run --exit-with-error-on-annotation

Running Ruby 2.7.6 and Rails 6.0.4.8. Got the same output in the Github Actions workflow when omitting the --dry-run flag for what it's worth.

nshki commented 2 years ago

Hi there! I'm so glad that you're finding the gem useful, and thanks so much for opening this issue.

I went ahead and spun up a brand new Rails v6.0.4.8 project with Ruby 2.7.6 and tried the GA workflow that you have with one new controller action. It did error out, but not in the way you described, as can be seen in this screenshot:

Screenshot 2022-06-24 at 08-21-36 Issue 31 · nshki_chusaku-test@f5bec1f

This particular error was from not having Yarn packages installed, so I used the following workflow config and gave it a re-run:

name: Chusaku

on: pull_request

jobs:
  chusaku:
    name: Chusaku
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3.0.2
      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      - uses: actions/setup-node@v3
      - name: Run Yarn
        run: yarn install
      - name: Run Chusaku
        run: bundle exec chusaku --dry-run --exit-with-error-on-annotation

Screenshot 2022-06-24 at 08-25-36 Issue 31 · nshki_chusaku-test@f75fa03

This time, it did work as expected.

Your local output and workflow output definitely suggest that you checked in the expected file changes, but just to make sure, it may be worth double-checking the state of your controller(s) and routes on the PR that you're seeing this issue on.

Numie commented 2 years ago

Thanks so much for looking into this so quickly. I think I've confirmed that the controller is currently un-annotated in CI:

Screen Shot 2022-06-24 at 2 12 32 PM

For reference, here is the same controller after running chusaku locally:

Screen Shot 2022-06-24 at 2 14 28 PM

Based on my reading of the repo, it seems like the only way to return the Nothing to annotate. message is if the @changed_files var is empty. I think the screenshots show the empty @changed_files is not because the files are already annotated. Maybe the controllers aren't being picked up inside the workflow somehow? I'd be grateful for any other insight you have.

nshki commented 2 years ago

Got it, that's really helpful context, thanks for that.

Would love to see if we can produce a minimum set of code to replicate this issue. The fact that it works locally but not on GitHub Actions is a big hint, but it's still hard to discern the exact bug.

If you don't mind, would you be able to see if you could replicate this issue on a fresh Rails repo? I'll see if I can replicate on the fresh repo I set up in the meantime.

Numie commented 2 years ago

Update here: After some debugging, the Nothing to annotate. message is due to Chusaku::Routes.call returning an empty hash in the Github workflow, and that is caused by Rails.application.routes.routes being empty inside the workflow. I'm still unclear why things worked in your workflow but continue to fail in mine, but I will continue to investigate.

Numie commented 2 years ago

Adding bundle exec rails routes prior to running chusaku in the workflow resolves the problem. I think we can probably close this issue. Thanks again for the great work on the gem.

nshki commented 2 years ago

✨ I'm glad you got it working! Really bizarre that running the command first fixes the issue for you. If this comes up again please let me know and I'll dig more into it!