lorenzwalthert / precommit

pre-commit hooks for R projects
https://lorenzwalthert.github.io/precommit/
GNU General Public License v3.0
253 stars 48 forks source link

Error in loadNamespace(x) : there is no package called ‘precommit’ #602

Open rressler opened 3 weeks ago

rressler commented 3 weeks ago

I am on a silicon mac have installed the latest versions of pre-commit (using Homebrew) and precommit. 26 October 2024.

The first three hooks in the default .pre-commit-config.yaml all fail. with the error: Error in loadNamespace(x) : there is no package called ‘precommit’

I am able to get the hooks to work by adding an RScript to ensure the renv::load() occurs. I thought this was supposed to happen with activate.R.

I am able to fix the first hook by adding an entry value.

 -   id: style-files
        args: [--style_pkg=styler, --style_fun=tidyverse_style]
        entry: Rscript -e "renv::load(); styler::style_dir()"

I am able to fix the second hook, but one file at a time. I could use regex to get more but you get the idea.

 id: spell-check
        args: [--langs=en-US]
        entry: Rscript -e "renv::load();spelling::spell_check_files('./app/app.R')"
        entry: Rscript -e "renv::load();spelling::spell_check_files('./R/varsutils_functions.R')"

I am able to fix the lintr hook as well.

    -   id: lintr
        args: [--warn_only]
        entry: Rscript -e "renv::load(); lintr::lint_dir()"

I would like to be able to use the default pre-commit-config.yaml file without having to force renv to load for those hooks that use precommit in their code.

Originally, precommit was only installed in this repo. I then installed it for all users. It made no difference.

I checked the paths to packages as well.

> .libPaths()
[1] "/Users/rressler/Library/CloudStorage/OneDrive-american.edu/my_aws_repos/shiny_varsutils_aws/renv/library/macos/R-4.4/aarch64-apple-darwin20"
[2] "/Users/rressler/Library/Caches/org.R-project.R/R/renv/sandbox/macos/R-4.4/aarch64-apple-darwin20/f7156815"  

Am I missing something in linking precommit, pre-commit, and renv? Please let me know.

Thanks

SaranjeetKaur commented 3 weeks ago

A related issue is also reported on renv: https://github.com/rstudio/renv/issues/1983

rressler commented 3 weeks ago

Thanks for noting that. There are several packages can be in the renv package library but do not appear in the lock.file, e.g. lintr and style in my case as they do not appear in a require or library call in the code since they are just used as part of development. One can force precommit to appear in the lock.file with a one line script that just uses library(precommit), but that does not change the behavior.

The hooks in question use the code arguments <- precommit::precommit_docopt(doc)which I assume triggers the error that precommit cannot be found.

lorenzwalthert commented 3 weeks ago

options(precommit.executable = "/opt/homebrew/bin/pre-commit")

Ok, that should not be necessary. If this is not already part of the search heuristic, we should add this location.

Am I missing something in linking precommit, pre-commit, and renv? Please let me know.

Not from the docs, since it's not written there (sorry), but from https://github.com/rstudio/renv/issues/1983:

author of {precommit} here. I know docs could be better but {precommit} manages it's own renv that is stored outside of your git root. Your project {renv} it's lockfile etc has nothing to do with it. If you search the issues in the https://github.com/lorenzwalthert/precommit, you should find a few similar issues and a link to the wiki on how you should be able to resolve the issue: https://github.com/lorenzwalthert/precommit/wiki/Packages-are-not-found-after-R-upgrade. Also, if you are interested in how {precommit} uses {renv}, please see here: https://github.com/lorenzwalthert/precommit/wiki/What-happens-under-the-hood-when-I-install-the-hooks%3F

lorenzwalthert commented 3 weeks ago

Can you please try the steps explained in https://github.com/lorenzwalthert/precommit/wiki/Packages-are-not-found-after-R-upgrade and report back?

rressler commented 3 weeks ago

Thanks for the references. I saw some older references from 2021 but was not sure if they were still valid or things had changed. The extra detail helped.

Bottom line though it still fails.

AU-macbook:shiny_varsutils_aws rressler$ pre-commit run --all-files --verbose
style-files..............................................................Failed
- hook id: style-files
- duration: 0.83s
- exit code: 1

- One or more packages recorded in the lockfile are not installed.
- Use `renv::status()` for more details.
- One or more packages recorded in the lockfile are not installed.
- Use `renv::status()` for more details.
Error in packageVersion("precommit") : 
  there is no package called ‘precommit’
Execution halted
- One or more packages recorded in the lockfile are not installed.
- Use `renv::status()` for more details.
- One or more packages recorded in the lockfile are not installed.
- Use `renv::status()` for more details.
Error in packageVersion("precommit") : 
  there is no package called ‘precommit’
Execution halted

I went through the steps multiple times, restarting RStudio in between just to try to make sure things were clean.

run pre-commit clean to delete the pre-commit cache.
run pre-commit install --install-hooks

I confirmed the cache at ~/.cache/pre-commit/ was deleted and then recreated.

I checked the renv.lock file in the new cache and precommit is not listed. Should it be?

After the commit failed I tried to purge from the cache.

env::purge('precommit')
The following packages will be purged from the cache:
- precommit 0.4.3 [Hash: 3b9c9c622ecd61e92199e523652f36e4]

Do you want to proceed? [Y/n]: y

- Removed 1 package from the cache.

I then tried the reinstall. However, it installed in the normal R location, not in the cache, or in my project renv (where it already exists).

> renv::install("precommit")
# Downloading packages -------------------------------------------------------
- Downloading precommit from CRAN ...           OK [426.8 Kb in 0.16s]
Successfully downloaded 1 package in 1.5 seconds.

The following package(s) will be installed:
- precommit [0.4.3]
These packages will be installed into "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library".
Do you want to proceed? [Y/n]: y

# Installing packages --------------------------------------------------------
- Installing precommit ...                      OK [installed binary and cached]
Successfully installed 1 package in 0.16 seconds.

Close RStudio and open it with my project again Project '~/Library/CloudStorage/OneDrive-american.edu/my_aws_repos/shiny_varsutils_aws' loaded. [renv 1.0.10]

Try to commit and it fails with the above error message.

I am happy to try something else. I understand what the activate.R is trying to do but I am not familiar enough with it to troubleshoot how it is actually working in my situation.

Thanks,

lorenzwalthert commented 3 weeks ago

Thanks for your help and thorough docs.

Just to confirm, are the error messages are about precommit not being present in the lockfile referring to the cache, not the project in which I am invoking the precommit checks?

Yes. But {precommit} itself won't be listed in that lockfile since it is placed in a clone of lorenzwalthert/precommit and it should install the package upon renv::restore().

Do I need to worry about the messages that One or more packages recorded in the lockfile are not installed.?

These I have seen as well before, but they seem unrelated.

I checked the renv.lock file in the new cache and precommit is not listed. Should it be?

{precommit} itself won't be listed in that lockfile since it is placed in a clone of lorenzwalthert/precommit and it should install the package upon renv::restore().

I see two ways forward:

rressler commented 3 weeks ago

Thanks for the additional info. I think we have success!!

  1. I opened a terminal window outside RStudio at the user root directory. ran pre-commit clean to delete the pre-commit cache. Success.

  2. Had to change the terminal working directory to the my project directory or the following failed as it needs to run in a git repository ran pre-commit install --install-hooks. Success!

    rressler@AU-macbook shiny_varsutils_aws % pre-commit install --install-hooks
    pre-commit installed at .git/hooks/pre-commit
    [INFO] Initializing environment for https://github.com/lorenzwalthert/precommit.
    [INFO] Installing environment for https://github.com/lorenzwalthert/precommit.
    [INFO] Once installed this environment will be reused.
    [INFO] This may take a few minutes...
    rressler@AU-macbook shiny_varsutils_aws % 
  3. Closed and reopened RStudio to the project of interest.

  4. Ran pre-commit run --all-files --verbose and while it generated a lot of warnings and messages, the hooks all ran and seemed to be doing what they should. Real Success!!

I included the specific output as well as the precommit yaml and my .lintr file below in case that helps.

Thanks so much! My problem appears to be solved but I will not close this yet in case you want to make comments. You can feel free to close or I can close in a few days.

Thanks so much!! Richard


Ran it with the verbose flag. AU-macbook:shiny_varsutils_aws rressler$ pre-commit run --all-files --verbose and got the following results.

style-files..............................................................Passed
- hook id: style-files
- duration: 1.4s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
NULL
[1] "cache root set to " "styler-perm"       
Styling  4  files:
 R/varsutils_functions.R ✔ 
 .Rprofile               ✔ 
 app/app.R               ✔ 
 data_raw/create_Rdata.R ✔ 
────────────────────────────────────────
Status  Count   Legend 
✔       4       File unchanged.
ℹ       0       File changed.
✖       0       Styling threw an error.
────────────────────────────────────────
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
NULL
[1] "cache root set to " "styler-perm"       
Styling  1  files:
 utils/add_words.R ✔ 
────────────────────────────────────────
Status  Count   Legend 
✔       1       File unchanged.
ℹ       0       File changed.
✖       0       Styling threw an error.
────────────────────────────────────────

AU-macbook:shiny_varsutils_aws rressler$ pre-commit run --all-files --verbose
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/pre-commit-ci/pre-commit-ci-config.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/pre-commit-ci/pre-commit-ci-config.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
style-files..............................................................Passed
- hook id: style-files
- duration: 1.44s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
NULL
[1] "cache root set to " "styler-perm"       
Styling  4  files:
 R/varsutils_functions.R ✔ 
 .Rprofile               ✔ 
 app/app.R               ✔ 
 data_raw/create_Rdata.R ✔ 
────────────────────────────────────────
Status  Count   Legend 
✔       4       File unchanged.
ℹ       0       File changed.
✖       0       Styling threw an error.
────────────────────────────────────────
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
NULL
[1] "cache root set to " "styler-perm"       
Styling  1  files:
 utils/add_words.R ✔ 
────────────────────────────────────────
Status  Count   Legend 
✔       1       File unchanged.
ℹ       0       File changed.
✖       0       Styling threw an error.
────────────────────────────────────────

spell-check..............................................................Failed
- hook id: spell-check
- duration: 1.23s
- exit code: 1
- files were modified by this hook

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
The following spelling errors were found:
  WORD                   FOUND IN
amazonaws              aws_mac.yml:34,64
                       aws.yml:34,64
ArgumentList           xDockerfile_windows:10
arn                    aws_mac.yml:24,75
                       aws.yml:24,75
AssumeRoleWithAction   aws_mac.yml:24,75
                       aws.yml:24,75
aws                    Dockerfile:5,6
                       aws_mac.yml:22,24,25,30,31,34,73,75,76,80
                       aws.yml:22,24,25,30,31,34,73,75,76,80
bioconductor           settings.json:2
BuildKit               aws_mac.yml:42
                       aws.yml:42
buildx                 aws_mac.yml:40,44,45
                       aws.yml:40,44,45
Buildx                 aws_mac.yml:39
                       aws.yml:39
CMD                    Dockerfile:44
                       xDockerfile_windows:34
dev                    Dockerfile:17,18,19,20
dkr                    aws_mac.yml:34,64
                       aws.yml:34,64
ecr                    aws_mac.yml:31,34,64
                       aws.yml:31,34,64
ECR                    aws_mac.yml:1,4,33
                       aws.yml:1,4,33
ecs                    aws_mac.yml:80
                       aws.yml:80
ECS                    aws_mac.yml:2,4,78
                       aws.yml:2,4,78
exe                    xDockerfile_windows:9,10,11
FilePath               xDockerfile_windows:10
GitHubAction           aws_mac.yml:24,75
                       aws.yml:24,75
hashFiles              aws_mac.yml:56
                       aws.yml:56
http                   Dockerfile:7
https                  Dockerfile:30
                       xDockerfile_windows:9
iam                    aws_mac.yml:24,75
                       aws.yml:24,75
libcurl                Dockerfile:17
libssl                 Dockerfile:19
libxml                 Dockerfile:18
LinkingTo              settings.json:8
linux                  aws_mac.yml:44,62
                       aws.yml:44,62
ltsc                   xDockerfile_windows:5
macos                  aws_mac.yml:13
mcr                    xDockerfile_windows:5
microsoft              xDockerfile_windows:5
mybuilder              aws_mac.yml:44
                       aws.yml:44
OIDC                   aws_mac.yml:15,21,72
                       aws.yml:15,21,72
openssl                Dockerfile:17
os                     aws_mac.yml:56
                       aws.yml:56
OutFile                xDockerfile_windows:9
powershell             xDockerfile_windows:8
qemu                   aws_mac.yml:37
                       aws.yml:37
QEMU                   aws_mac.yml:36
                       aws.yml:36
renv                   Dockerfile:29,30,32,33,35,37
                       xDockerfile_windows:21,22,24,25,27,28
                       aws_mac.yml:56
                       aws.yml:56
repo                   aws_mac.yml:70
                       aws.yml:70
repos                  Dockerfile:30
rf                     Dockerfile:21
Rlib                   aws_mac.yml:55
                       aws.yml:55
runApp                 Dockerfile:44
                       xDockerfile_windows:34
servercore             xDockerfile_windows:5
shinyapp               aws_mac.yml:80
                       aws.yml:80
srv                    Dockerfile:12,25,26,27,33,44
                       xDockerfile_windows:14,17,18,19,25,34
stdin                  aws_mac.yml:34
                       aws.yml:34
sts                    aws_mac.yml:30
                       aws.yml:30
ubuntu                 aws_mac.yml:13
                       aws.yml:13
uname                  aws_mac.yml:50
                       aws.yml:50
Uri                    xDockerfile_windows:9
varsutils              Dockerfile:5,6
                       aws_mac.yml:64
                       aws.yml:64
vcs                    settings.json:15,16,17,18
ver                    Dockerfile:9
WebRequest             xDockerfile_windows:9
withouot               Dockerfile:3
WORKDIR                Dockerfile:12
                       xDockerfile_windows:14
zlib                   Dockerfile:20
All spelling errors found were copied to inst/WORDLIST assuming they were not spelling errors and will be ignored in the future. Please review the above list and for each word that is an actual typo:
 - fix it in the source code.
 - remove it again manually from inst/WORDLIST to make sure it's not
   ignored in the future.
 Then, try committing again.
 Hint: you can disable this behavior by providing a --read-only flag.
Error: Spell check failed
Execution halted

lintr....................................................................Passed
- hook id: lintr
- duration: 1.53s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
File `.Rprofile` is not lint free
/Users/rressler/Library/CloudStorage/OneDrive-american.edu/my_aws_repos/shiny_varsutils_aws/.Rprofile:2:33: warning: [absolute_path_linter] Do not use absolute paths.
options(precommit.executable = "/opt/homebrew/bin/pre-commit")
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.

readme-rmd-rendered..................................(no files to check)Skipped
- hook id: readme-rmd-rendered
parsable-R...............................................................Passed
- hook id: parsable-R
- duration: 1.06s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.

no-browser-statement.....................................................Passed
- hook id: no-browser-statement
- duration: 1.14s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.

no-debug-statement.......................................................Passed
- hook id: no-debug-statement
- duration: 1.09s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.

check for added large files..............................................Passed
- hook id: check-added-large-files
- duration: 0.07s
fix end of files.........................................................Passed
- hook id: end-of-file-fixer
- duration: 0.05s
check pre-commit.ci config...............................................Passed
- hook id: check-pre-commit-ci-config
- duration: 0.18s
Don't commit common R artifacts......................(no files to check)Skipped
- hook id: forbid-to-commit
AU-macbook:shiny_varsutils_aws rressler$ 

I then ran without the -verbose flag and got the following results.

AU-macbook:shiny_varsutils_aws rressler$ pre-commit run --all-files

style-files..............................................................Passed
spell-check..............................................................Passed
lintr....................................................................Passed
- hook id: lintr
- duration: 1.37s

- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.
File `.Rprofile` is not lint free
/Users/rressler/Library/CloudStorage/OneDrive-american.edu/my_aws_repos/shiny_varsutils_aws/.Rprofile:2:33: warning: [absolute_path_linter] Do not use absolute paths.
options(precommit.executable = "/opt/homebrew/bin/pre-commit")
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
- The project is out-of-sync -- use `renv::status()` for details.
- The project is out-of-sync -- use `renv::status()` for details.

readme-rmd-rendered..................................(no files to check)Skipped
parsable-R...............................................................Passed
no-browser-statement.....................................................Passed
no-debug-statement.......................................................Passed
check for added large files..............................................Passed
fix end of files.........................................................Passed
check pre-commit.ci config...............................................Passed
Don't commit common R artifacts......................(no files to check)Skipped

Complete config file follows:

# All available hooks: https://pre-commit.com/hooks.html
# R specific hooks: https://github.com/lorenzwalthert/precommit
repos:
-   repo: https://github.com/lorenzwalthert/precommit
    rev: v0.4.3.9001
    hooks:
    -   id: style-files
        args: [--style_pkg=styler, --style_fun=tidyverse_style]
    -   id: spell-check
        exclude: >
          (?x)^(
          .*\.[rR]|
          .*\.feather|
          .*\.jpeg|
          .*\.pdf|
          .*\.png|
          .*\.py|
          .*\.RData|
          .*\.rds|
          .*\.Rds|
          .*\.Rproj|
          .*\.sh|
          (.*/|)\.gitignore|
          (.*/|)\.gitlab-ci\.yml|
          (.*/|)\.lintr|
          (.*/|)\.pre-commit-.*|
          (.*/|)\.Rbuildignore|
                    (.*/|)\.R|
          (.*/|)\.Renviron|
          (.*/|)\.Rprofile|
          (.*/|)\.travis\.yml|
          (.*/|)appveyor\.yml|
          (.*/|)NAMESPACE|
          (.*/|)renv/settings\.dcf|
          (.*/|)renv\.lock|
          (.*/|)WORDLIST|
          \.github/workflows/.*|
          data/.*|
          )$
    -   id: lintr
        args: [--warn_only]
        verbose: true
    -   id: readme-rmd-rendered
    -   id: parsable-R
    -   id: no-browser-statement
    -   id: no-debug-statement
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
    -   id: check-added-large-files
        args: ['--maxkb=200']
    -   id: end-of-file-fixer
        exclude: '\.Rd'
-   repo: https://github.com/pre-commit-ci/pre-commit-ci-config
    rev: v1.6.1
    hooks:
    # Only required when https://pre-commit.ci is used for config validation
    -   id: check-pre-commit-ci-config
-   repo: local
    hooks:
    -   id: forbid-to-commit
        name: Don't commit common R artifacts
        entry: Cannot commit .Rhistory, .RData, .Rds or .rds.
        language: fail
        files: '\.(Rhistory|csv|RData|Rds|rds)$'
        exclude: '(?i)\.Rdata$' # <regex> to allow committing specific files
ci:
    autoupdate_schedule: monthly

Complete .lintr file

linters: linters_with_defaults(
  assignment_linter = NULL,
  commented_code_linter = NULL, # allow code comments
  indentation_linter = NULL,   # let styler handle indentation
  object_usage_linter = NULL,
  trailing_whitespace_linter =  NULL,
  absolute_path_linter()
  )
SaranjeetKaur commented 3 weeks ago

I am facing a slightly different issue with the installation (I am using a Windows machine).

Whether I run pre-commit install either via RStudio terminal or an external terminal, I get the error: 'pre-commit' is not recognized as an internal or external command, operable program or batch file.

When I try to run pip install pre-commit (both on RStudio terminal as well as an external terminal), I get the following error:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pre-commit in c:\users\...\python\python311\site-packages (3.7.1)
Requirement already satisfied: cfgv>=2.0.0 in c:\users\...\python\python311\site-packages (from pre-commit) (3.4.0)
Requirement already satisfied: identify>=1.0.0 in c:\users\...\python\python311\site-packages (from pre-commit) (2.5.36)
Requirement already satisfied: nodeenv>=0.11.1 in c:\users\...\python\python311\site-packages (from pre-commit) (1.9.1)
Requirement already satisfied: pyyaml>=5.1 in c:\users\...\python\python311\site-packages (from pre-commit) (6.0.1)
Requirement already satisfied: virtualenv>=20.10.0 in c:\users\...\python\python311\site-packages (from pre-commit) (20.26.3)
Requirement already satisfied: distlib<1,>=0.3.7 in c:\users\...\python\python311\site-packages (from virtualenv>=20.10.0->pre-commit) (0.3.8)
Requirement already satisfied: filelock<4,>=3.12.2 in c:\users\...\python\python311\site-packages (from virtualenv>=20.10.0->pre-commit) (3.15.4)
Requirement already satisfied: platformdirs<5,>=3.9.1 in c:\users\...\python\python311\site-packages (from virtualenv>=20.10.0->pre-commit) (4.2.2)

I have installed the following version of {precommit} on RStudio:

> packageVersion("precommit")
[1] ‘0.4.3’
> precommit::version_precommit()
[1] "3.7.1"