Skip to content

Commit 57e6c26

Browse files
committed
Create friction to discourage creating a project in home directory
Motivated by #1336
1 parent 7455051 commit 57e6c26

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

R/create.R

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ create_package <- function(path,
4444
if (check_name) {
4545
check_package_name(name)
4646
}
47-
check_not_nested(path_dir(path), name)
47+
challenge_nested_project(path_dir(path), name)
48+
challenge_home_directory(path)
4849

4950
create_directory(path)
5051
local_project(path, force = TRUE)
@@ -75,7 +76,8 @@ create_project <- function(path,
7576
open = rlang::is_interactive()) {
7677
path <- user_path_prep(path)
7778
name <- path_file(path_abs(path))
78-
check_not_nested(path_dir(path), name)
79+
challenge_nested_project(path_dir(path), name)
80+
challenge_home_directory(path)
7981

8082
create_directory(path)
8183
local_project(path, force = TRUE)
@@ -246,7 +248,7 @@ create_from_github <- function(repo_spec,
246248

247249
destdir <- user_path_prep(destdir %||% conspicuous_place())
248250
check_path_is_directory(destdir)
249-
check_not_nested(destdir, repo_name)
251+
challenge_nested_project(destdir, repo_name)
250252
repo_path <- path(destdir, repo_name)
251253
create_directory(repo_path)
252254
check_directory_is_empty(repo_path)
@@ -309,7 +311,7 @@ create_from_github <- function(repo_spec,
309311
# creates a backdoor we can exploit in tests
310312
allow_nested_project <- function() FALSE
311313

312-
check_not_nested <- function(path, name) {
314+
challenge_nested_project <- function(path, name) {
313315
if (!possibly_in_proj(path)) {
314316
return(invisible())
315317
}
@@ -331,3 +333,24 @@ check_not_nested <- function(path, name) {
331333
}
332334
invisible()
333335
}
336+
337+
challenge_home_directory <- function(path) {
338+
homes <- unique(c(path_home(), path_home_r()))
339+
if (!path %in% homes) {
340+
return(invisible())
341+
}
342+
343+
qualification <- if (is_windows()) {
344+
glue("a special directory, i.e. some applications regard it as ")
345+
} else {
346+
""
347+
}
348+
ui_line("
349+
{ui_path(path)} is {qualification}your home directory.
350+
It is generally a bad idea to create a new project here.
351+
You should probably create your new project in a subdirectory.")
352+
if (ui_nope("Do you want to create anyway?")) {
353+
ui_stop("Good move! Cancelling project creation.")
354+
}
355+
invisible()
356+
}

R/rstudio.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ rstudio_git_tickle <- function() {
170170
}
171171

172172
rstudio_config_path <- function(...) {
173-
if (.Platform$OS.type == "windows") {
173+
if (is_windows()) {
174174
# https://github.com/r-lib/usethis/issues/1293
175175
base <- rappdirs::user_config_dir("RStudio", appauthor = NULL)
176176
} else {

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ pluck_chr <- function(.x, ...) {
103103
pluck_int <- function(.x, ...) {
104104
as_integer(purrr::pluck(.x, ..., .default = NA))
105105
}
106+
107+
is_windows <- function() {
108+
.Platform$OS.type == "windows"
109+
}

tests/testthat/test-create.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,14 @@ test_that("create_*() works w/ non-existing rel path, open = TRUE, not in RStudi
137137
expect_equal(path_wd(), out_path)
138138
expect_equal(proj_get(), out_path)
139139
})
140+
141+
test_that("we discourage project creation in home directory", {
142+
local_interactive(FALSE)
143+
expect_usethis_error(create_package(path_home()), "create anyway")
144+
expect_usethis_error(create_project(path_home()), "create anyway")
145+
146+
if (is_windows()) {
147+
expect_usethis_error(create_package(path_home_r()), "create anyway")
148+
expect_usethis_error(create_project(path_home_r()), "create anyway")
149+
}
150+
})

0 commit comments

Comments
 (0)