Search code examples
rgithub-actions

lintr how to set to fail when there's annotations?


I've set up lintr package with GitHub Actions:

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

name: lint

jobs:
  lint:
    runs-on: macOS-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v2

      - uses: r-lib/actions/setup-r@master

      - uses: actions/cache@v1
        with:
          path: ~/Library/Application Support/renv
          key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
          restore-keys: |
            ${{ runner.os }}-renv-

      - name: Install lintr
        run: install.packages("lintr")
        shell: Rscript {0}

      - name: Lint
        run: lintr::lint_dir(linters = lintr::with_defaults(assignment_linter = NULL, line_length_linter = NULL, spaces_left_parentheses_linter = NULL), pattern = '[.]R$|[.]Rmd')
        shell: Rscript {0}

And formatting errors are shown as Annotations:

enter image description here

But, those annotations doesn't fails PR check which is actually what you want when you lint code.

enter image description here

I thought of setting an exit 1 on warnings. Is that the correct approach?


Solution

  • Prior to the code that runs lint_dir , you could add options(error_on_lint=TRUE) :

    run: "options(error_on_lint=TRUE); lintr::lint_dir(linters = lintr ..."
    

    It might be useful to put your lintr settings into a .lintr file in the directory that you run lintr against:

    .lintr

    linters: with_defaults(
        assignment_linter = NULL, line_length_linter = NULL, spaces_left_parentheses_linter = NULL
      )
    error_on_lint: TRUE
    

    Then the lint_dir code in your GHA would look like:

    run: lintr::lint_dir(pattern=....)