Repository navigation
Description
Although there is a .lintr file in the lintr root, linting is not done as part of the current travis-CI workflow (or the soon to be introduced GHA CI workflow).
If people are keen to integrate lintr into their dev workflow, they're probably going to look at lintr as a good example of how to set this up. For those who read docs, the current docs explain how to set up lintr for running on Travis; they might need expanding to explain how to use lintr on GHA / other providers. For those who don't read docs, it would be good if {lintr} had exemplary lint-running code that they can copy/paste into their own projects (or perhaps a use_lintr hook could be constructed for {usethis} that makes setting up linting trivial).
Nonetheless, the current linting setup fails:
With master: f878ec9
if we run Rscript -e "devtools::load_all(); lint_package()" in the lintr root, we get the following:
Loading lintr
Error: Malformed file!
Execution halted
If we run Rscript -e "devtools::load_all(); lint_dir('R')" we get a long list of lints. (and also for lint_dir("tests"))
Some of these lints
- are due to bugs in lintr (some fixed in recent but unmerged PRs:
paren_brace_linterfalse positive within regex strings: "(match_group){quantifier}" #441, Equals na comment #546 - are due to the "exclusions" not being updated when example/test code is added that is designed to fail the linters (@AshesITR 's recent directory-exclusion PR should ease fixing this kind of stuff)
- would have been fixed if linting had been a consistent part of the lintr PR workflow.
Should we fixup the lintr PR process so that linting is always run?
And if we do, how do we change the current code base so that linting the current code comes back green?
What we shouldn't do is a monolithic PR using {styler} or something that fixes hundreds of function(arg=123) to function(arg = 123); when these wholesale-change commits introduce bugs they are hell to fix.
I'd suggest we rewrite the .lintr:
- exclude all test & doc directories for now
- use the smallest collection of the default linters that come back green on
<lintr>/R - apply these linters to any newly submitted PR
- then do individual PRs to gradually add further default-linters back in
An alternative would be to write up a .lintr that defines the linters that we definitely do want to apply to lintr and write up a CI workflow script that applies these linters to just those files that were modified in a submitted PR; and if a lint crops up on a line that was touched by the PR author then linting should return red from CI.
Activity
AshesITR commentedon Nov 25, 2020
Here are some stats on babe9f7 (current HEAD of #539) for
lintr::lint_package():By linter
R/ by linter
By filename
By directory
AshesITR commentedon Nov 25, 2020
From the stats I can see a few things that we could make as a first step PR:
tests/testthat/knitr_formats- those files are all designed to lint.testsas suggestedR/in a one-by-one fashion as can be done without altering the parse tree (i.e. purely stylistic lints) making sure to create a new commit for each touched fileBy making each file a single commit, potential bugs can be efficiently pinpointed with
git bisectshould the need arise, thereby minimising risk.Since syntactic lints, such as
cyclocomp_linter,object_name_linterorobject_usage_lintercan be a different beast with regards to the potential for new bugs, we should be more careful with fixing those.russHyde commentedon Nov 25, 2020
Can we flag as 'good-first-issue' all single-file lint fixes.
MichaelChirico commentedon Nov 26, 2020
Would PRs by file or by linter be better? If the fix is going to introduce a bug, I guess it would be easy to introduce the same bug to multiple files, and easier to fix that same fix in multiple places at once.
AshesITR commentedon Nov 26, 2020
For style issues I really don't mimd a large PR as long as there are commits to bisect.
For synatctic linters like cyclocomp I think we really should go function by function.
russHyde commentedon Nov 26, 2020
Thanks for your analysis @AshesITR . Based on this, the following is a minimal .lintr that returns green on the current codebase:
russHyde commentedon Nov 26, 2020
But ultimately, we'd aim to get this back to the following (the original settings, but disregarding all test-examples)
AshesITR commentedon Nov 26, 2020
Do you think it would be useful to keep a long-running PR going with the final
.lintrfile and periodically mergemasterinto it to check how the de-linting goes?If so, we'd need:
tests/Number 3. could alternatively be organized per file.
However, @MichaelChirico's suggestion would have the advantage that the associated PRs can add their linter to the .lintr file immediately.
cc @jimhester
AshesITR commentedon Nov 26, 2020
@dragosmg Do you have an idea if we could somehow (e.g. by setting
options(lintr.linter_file)somewhere and doing some git magic to find changed files only) apply a different.lintrconfig to changed files in new PRs?It would be awesome if the final set of linters could be automatically applied to all modified files in a PR without having to have the rest of the code lint red.
russHyde commentedon Nov 27, 2020
Are there any default linters that we don't want to apply to lintr? Some linters may need commented code to explain what they do, so maybe the commented-code linter should be dropped
AshesITR commentedon Nov 27, 2020
The commented code can trivially be "fooled" by commenting the code with a special sequence. I like to use this:
On the other hand: WDYT about using more than the default set? Such as T_and_F_symbol_linter?
AshesITR commentedon Nov 27, 2020
The following functions have
cyclocomp_linterhits:get_source_expressions.R)get_source_expressions.R)path_linters.R)unneeded_concatenation_linter.R)dragosmg commentedon Nov 29, 2020
On @russHyde's point about {usethis} and a potential
use_lintr()function.r-lib/actionshas a lintr workflow example (https://github.com/r-lib/actions/blob/master/examples/lint.yaml). It can be accessed viausethis::use_github_action("lint"). Maybe this could be included in the documentation.dragosmg commentedon Nov 30, 2020
@AshesITR is this still needed? If yes, I suggest we create a separate issue and look into this.
AshesITR commentedon Nov 30, 2020
Hi @dragosmg I still think this would be useful, yes.
lorenzwalthert commentedon Jan 26, 2021
Just FYI when
scope < 'tokens', {styler} can verify that no bugs are introduced because only spaces and line breaks are modified and the parse tree remains identical. That means we have strong guarantees here. SeeRound trip validationinhelp(style_pkg, package = 'styler'). You can also use {styler} to just style spaces, then spaces and indention, then add line breaks, then tokens. Much easier to review one by one than one bulk update, I agree. Also, you can do this by file withstyle_file().Edit: The guarantees currently don't hold for roxygen code examples.
MichaelChirico commentedon May 26, 2022
I think this issue can be closed -- check #1186 for the last vestige of it