Skip to content

Commit d014b4b

Browse files
committed
Use rprojroot rather than built in functions to find the rproj (if there is one)
We were running into catastrophic performance taking 20+ minutes on CRAN's check machines with the previous code, likely because when searching for Rprojects a parent directory containing thousands of files was being searched. I believe using rprojroot should avoid this.
1 parent 00e74ad commit d014b4b

File tree

4 files changed

+25
-43
lines changed

4 files changed

+25
-43
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Imports:
3232
jsonlite,
3333
knitr,
3434
rex,
35+
rprojroot,
3536
stats,
3637
utils,
3738
xml2 (>= 1.0.0),

R/lint.R

+14-25
Original file line numberDiff line numberDiff line change
@@ -370,39 +370,28 @@ reorder_lints <- function(lints) {
370370
)]
371371
}
372372

373-
has_description <- function(path) {
374-
desc_info <- file.info(file.path(path, "DESCRIPTION"))
375-
!is.na(desc_info$size) && desc_info$size > 0.0 && !desc_info$isdir
376-
}
377-
378373
find_package <- function(path) {
379-
path <- normalizePath(path, mustWork = FALSE)
380-
381-
while (!has_description(path)) {
374+
if (!dir.exists(path)) {
382375
path <- dirname(path)
383-
if (is_root(path)) {
384-
return(NULL)
385-
}
386376
}
387-
388-
path
389-
}
390-
391-
find_rproj_at <- function(path) {
392-
head(list.files(path = path, pattern = "\\.Rproj$", full.names = TRUE), 1L)
377+
tryCatch(
378+
rprojroot::find_root(path = path, criterion = rprojroot::is_r_package),
379+
error = function(e) NULL
380+
)
393381
}
394382

395-
find_rproj <- function(path) {
396-
path <- normalizePath(path, mustWork = FALSE)
397-
398-
while (length(res <- find_rproj_at(path)) == 0L) {
383+
find_rproj_or_package <- function(path) {
384+
if (!dir.exists(path)) {
399385
path <- dirname(path)
400-
if (is_root(path)) {
401-
return(NULL)
402-
}
403386
}
387+
tryCatch(
388+
rprojroot::find_root(path = path, criterion = rprojroot::is_rstudio_project | rprojroot::is_r_package),
389+
error = function(e) NULL
390+
)
391+
}
404392

405-
res
393+
find_rproj_at <- function(path) {
394+
head(Sys.glob(file.path(path, "*.Rproj")), n = 1L)
406395
}
407396

408397
is_root <- function(path) {

R/settings.R

+5-13
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,12 @@ find_default_encoding <- function(filename) {
108108
return(NULL)
109109
}
110110

111-
pkg_path <- find_package(filename)
112-
rproj_file <- find_rproj(filename)
113-
pkg_enc <- get_encoding_from_dcf(file.path(pkg_path, "DESCRIPTION"))
114-
rproj_enc <- get_encoding_from_dcf(rproj_file)
115-
116-
if (!is.null(rproj_file) && !is.null(pkg_path) && startsWith(rproj_file, pkg_path)) {
117-
# Check precedence via directory hierarchy.
118-
# Both paths are normalized so checking if rproj_file is within pkg_path is sufficient.
119-
# Let Rproj file take precedence
120-
return(rproj_enc %||% pkg_enc)
121-
} else {
122-
# Let DESCRIPTION file take precedence if .Rproj file is further up the directory hierarchy
123-
return(pkg_enc %||% rproj_enc)
111+
root_path <- find_rproj_or_package(filename)
112+
rproj_enc <- get_encoding_from_dcf(find_rproj_at(root_path))
113+
if (!is.null(rproj_enc)) {
114+
return(rproj_enc)
124115
}
116+
rproj_enc
125117
}
126118

127119
get_encoding_from_dcf <- function(file) {

tests/testthat/test-settings.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,16 @@ test_that("it has a smart default for encodings", {
122122
read_settings(NULL)
123123
expect_equal(settings$encoding, "UTF-8")
124124

125-
proj_file <- file.path("dummy_projects", "project", "metropolis-hastings-rho.R")
126-
pkg_file <- file.path("dummy_packages", "cp1252", "R", "metropolis-hastings-rho.R")
125+
proj_file <- test_path("dummy_projects", "project", "metropolis-hastings-rho.R")
126+
pkg_file <- test_path("dummy_packages", "cp1252", "R", "metropolis-hastings-rho.R")
127127

128128
expect_equal(
129-
normalizePath(find_rproj(proj_file), winslash = "/"),
130-
normalizePath(file.path("dummy_projects", "project", "project.Rproj"), winslash = "/")
129+
normalizePath(find_rproj_at(find_rproj_or_package(proj_file)), winslash = "/"),
130+
normalizePath(test_path("dummy_projects", "project", "project.Rproj"), winslash = "/")
131131
)
132132
expect_equal(
133133
normalizePath(find_package(pkg_file), winslash = "/"),
134-
normalizePath(file.path("dummy_packages", "cp1252"), winslash = "/")
134+
normalizePath(test_path("dummy_packages", "cp1252"), winslash = "/")
135135
)
136136

137137
expect_equal(find_default_encoding(proj_file), "ISO8859-1")

0 commit comments

Comments
 (0)