rbuildlintr

How to get non-zero exit status with lintr::lint() in order to fail a build


I am trying to help our group of developers obtain similar code styles by using the lintr package when writing R code. To automate this step, I want our builds to fail if there are any mistakes (bad styles) in the code. We are using Jenkins for our build pipeline if that matters.

I know that we could use the expect_lint_free function, but we are not making packages, only script files. The output from the lint function looks fine, but the build passes even when the linter returns improvement proposals. How do I get a non-zero exit status that will make the Jenkins build fail?

The (simplified) code run on the command line

Rscript -e "lintr::lint('my_script.R')"

returns no error but lots of proposals.

Note: I am not interested in "Create an R package" solutions at the moment.


Solution

  • The lintr::lint() function returns results in a list with class "lints". You have issues if its length is greater than zero, so you can do

    Rscript -e "quit(save = 'no', status = length(lintr::lint('my_script.R')))"