Skip to content

Commit

Permalink
improve handling of import::from (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Feb 9, 2024
1 parent 7977c37 commit 2b8eb06
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
18 changes: 12 additions & 6 deletions R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -1318,16 +1318,22 @@ renv_dependencies_discover_r_import <- function(node, stack, envir) {

# the '.from' argument is the package name, either a character vector of length one or a symbol
from <- matched$.from
if (is.symbol(from))
from <- as.character(from)

ok <-
is.character(from) &&
length(from) == 1
if (is.symbol(from)) {
co <- node[[".character_only"]]
if (!identical(co, TRUE))
from <- as.character(from)
}

ok <- is.character(from) && length(from) == 1L
if (!ok)
return(FALSE)

# '.from' can also be an R script; if it appears to be a path, then ignore it
# https://github.com/rstudio/renv/issues/1743
if (grepl("\\.[rR]$", from, perl = TRUE) &&
grepl("[/\\]", from))
return(FALSE)

envir[[from]] <- TRUE
TRUE

Expand Down
7 changes: 6 additions & 1 deletion tests/testthat/resources/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Capture packages (a, b, ...), not functions or other objects (f1, f2, ...)
# Do not capture packages in invalid calls (x1, x2, ...)

# valid uses of import::from
import::from(a, f1, f2, f3)
import::from(f1, .from = b)

Expand All @@ -24,3 +23,9 @@ import::into(f1) # no package specified at all

# ignore usages that aren't namespace-prefixed
from(A)

# ignore .character_only with symbol
import::from(B, .character_only = TRUE)

# ignore things that look like scripts
import::from("./module.R")

0 comments on commit 2b8eb06

Please sign in to comment.