Skip to content

[Linting Rule] Package Hooks

github-actions[bot] edited this page Oct 25, 2026 · 1 revision

Generated from 'wiki-linter.ts' on 2026-10-25, 09:29:25 UTC (v2.16.6), do not edit directly.

Package Hooks [overview]

bug cmd-check 37, 42–44, 82 quickfix usability

This rule ports the following steps of R CMD check (see the conformance table for the legend):

# Step Coverage Note
37 R code for possible problems (book) 🟩⭐ ⭐ also quick fixes, deprecated arguments, environments resolved through variables, hooks followed into callees
42 whether startup messages can be suppressed (book) 🔷⭐ ⭐ also follows the hooks into the package functions they call
output in package hooks not sent through packageStartupMessage
43 whether the namespace can be unloaded cleanly (book) 🔷 only hook arguments and misplaced library.dynam.unload calls
44 package namespace information (book) 🔶
82 whether the package can be unloaded cleanly (book) 🔷 only hook arguments and misplaced library.dynam.unload calls

This rule is a best-effort rule.

Finds problems in .onLoad, .onAttach, .onDetach, and .Last.lib: argument lists, attaching packages, output other than packageStartupMessage(), installed.packages(), misplaced library.dynam() calls, and the obsolete .First.lib. Follows calls into the package's own functions.
This linting rule is implemented in src/linter/rules/cmd-check/package-hooks.ts.

Configuration

Linting rules can be configured by passing a configuration object to the linter query as shown in the example below. The package-hooks rule accepts the following configuration options:

  • allowedOutput
    functions that may emit output in a startup hook nonetheless, as the user can silence them otherwise (default warning)
    R CMD check #37, #42
  • followCalls
    also report what the hook does through functions of the package it calls, which R CMD check does not see (default true)
    R CMD check #37, #42
  • startupMessageFunctions
    functions that print startup messages the way R wants them to, which are only allowed in .onAttach
    R CMD check #37, #42
  • unsafeCalls
    functions too costly or unsafe to call in a startup hook (default installed.packages)
    R CMD check #37

Examples

.onLoad <- function(libname, pkgname) {
	library(stats)
	cat("Loading")
}
.onAttach <- function(libname, pkgname) message("Welcome!")

The linting query can be used to run this rule on the above example:

[ { "type": "linter",   "rules": [ { "name": "package-hooks",     "config": {} } ] } ]

Results (prettified and summarized):

Query: linter (3 ms)
   ╰ Package Hooks (package-hooks):
       ╰ certain:
           ╰ .onLoad: search-path (library) at 2.2-15
           ╰ .onLoad: output (cat) at 3.2-15
           ╰ .onAttach: output (message) at 5.41-59 (1 quick fix(es) available)

More examples are the test cases in test/functionality/linter/cmd-check/lint-package-hooks.test.ts.

Clone this wiki locally