diff --git a/NEWS.md b/NEWS.md index 37d7ede9f..22516bd33 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # usethis (development version) +* `use_standalone()` now again works outside packages (#2087). + * `pr_resume()` (without a specific `branch`) and `pr_fetch()` (without a specific `number`) no longer error when a branch name contains curly braces (#2107, @jonthegeek). # usethis 3.2.1 diff --git a/R/use_standalone.R b/R/use_standalone.R index 6d13d1f9d..a37cd440a 100644 --- a/R/use_standalone.R +++ b/R/use_standalone.R @@ -79,6 +79,15 @@ use_standalone <- function(repo_spec, file = NULL, ref = NULL, host = NULL) { dependencies <- standalone_dependencies(lines, path) + if (!is_package()) { + imports <- dependencies$imports + n <- cli::qty(length(imports$pkg)) + ui_bullets(c( + "!" = "You'll need the following {n} package{?s}: {.pkg {imports$pkg}}." + )) + return(invisible()) + } + for (dependency in dependencies$deps) { use_standalone(repo_spec, dependency, ref = ref, host = host) } diff --git a/tests/testthat/_snaps/use_standalone.md b/tests/testthat/_snaps/use_standalone.md index 1b1a447b6..247107b7d 100644 --- a/tests/testthat/_snaps/use_standalone.md +++ b/tests/testthat/_snaps/use_standalone.md @@ -64,6 +64,14 @@ [4] "# ----------------------------------------------------------------------" [5] "#" +# from a non-package project throws an informative message + + Code + use_standalone("r-lib/rlang", "types-check", ref = "73182fe94") + Message + v Writing 'R/import-standalone-types-check.R'. + ! You'll need the following package: rlang. + # can offer choices Code diff --git a/tests/testthat/test-use_standalone.R b/tests/testthat/test-use_standalone.R index f014b101a..3f47f9feb 100644 --- a/tests/testthat/test-use_standalone.R +++ b/tests/testthat/test-use_standalone.R @@ -54,6 +54,17 @@ test_that("can import standalone file with dependencies", { expect_match(imports, "rlang") }) +test_that("from a non-package project throws an informative message", { + skip_if_offline("github.com") + create_local_project() + withr::local_options(list(usethis.quiet = FALSE)) + + # NOTE: Check ref after r-lib/rlang@standalone-dep has been merged + expect_snapshot( + use_standalone("r-lib/rlang", "types-check", ref = "73182fe94") + ) +}) + test_that("can use full github url", { skip_if_offline("github.com") create_local_package()