Skip to content

Commit

Permalink
parse top-level NAMESPACE file for deps (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey authored Aug 14, 2023
1 parent 9f4cbe1 commit 0cf6b8a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# renv (development version)

* renv now parses package `NAMESPACE` files for imported dependencies. (#1637)

* renv no longer locks the sandbox by default.

* Fixed an issue where renv used the wrong library paths when attempting
to activate the watchdog.
to activate the watchdog. This could cause a 10 second delay when activating
the sandbox.


# renv 1.0.1
Expand Down
34 changes: 33 additions & 1 deletion R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ renv_dependencies_callback <- function(path) {
cbname <- list(
".Rprofile" = function(path) renv_dependencies_discover_r(path),
"DESCRIPTION" = function(path) renv_dependencies_discover_description(path),
"NAMESPACE" = function(path) renv_dependencies_discover_namespace(path),
"_bookdown.yml" = function(path) renv_dependencies_discover_bookdown(path),
"_pkgdown.yml" = function(path) renv_dependencies_discover_pkgdown(path),
"_quarto.yml" = function(path) renv_dependencies_discover_quarto(path),
Expand Down Expand Up @@ -434,7 +435,7 @@ renv_dependencies_find_dir_children <- function(path, root, depth) {

# remove hard-coded ignores
# (only keep DESCRIPTION files at the top level)
ignored <- c("packrat", "renv", "revdep", "vendor", if (depth) "DESCRIPTION")
ignored <- c("packrat", "renv", "revdep", "vendor", if (depth) c("DESCRIPTION", "NAMESPACE"))
children <- children[!basename(children) %in% ignored]

# compute exclusions
Expand Down Expand Up @@ -589,6 +590,37 @@ renv_dependencies_discover_description <- function(path,

}

renv_dependencies_discover_namespace <- function(path) {

tryCatch(
renv_dependencies_discover_namespace_impl(path),
error = warnify
)

}

renv_dependencies_discover_namespace_impl <- function(path) {

# parseNamespaceFile() expects to be called on an installed package,
# so we have to pretend our best here
library <- dirname(dirname(path))
package <- basename(dirname(path))
info <- parseNamespaceFile(
package = package,
package.lib = library,
mustExist = TRUE
)

# read package names from imports
packages <- map_chr(info$imports, `[[`, 1L)

renv_dependencies_list(
source = path,
packages = sort(unique(packages))
)

}

renv_dependencies_discover_description_impl <- function(dcf, field, path) {

# read field
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,25 @@ test_that("dependencies() notifies the user if directories contain lots of files
expect_snapshot(. <- renv_snapshot_dependencies(project))

})

test_that("dependencies() can parse NAMESPACE files", {

project <- renv_tests_scope()
desc <- heredoc("
Type: Package
Package: test
Version: 0.1.0
")
writeLines(desc, con = "DESCRIPTION")

namespace <- heredoc("
import(utils)
importFrom(tools, SIGQUIT)
import(graphics, except = c(abline))
")
writeLines(namespace, con = "NAMESPACE")

deps <- dependencies()
expect_setequal(deps$Package, c("graphics", "tools", "utils"))

})

0 comments on commit 0cf6b8a

Please sign in to comment.