-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change proj_activate so it accepts relative paths #954
Comments
I spoke with Thomas about this issue last night, and I have a reprex that should work even when RStudio is running. (Respectfully, this is one of the reasons why reprexes are important. It took me a while to understand under what circumstances there is an issue and what the issue actually was). Fundamentally, the issue is that Moving Line 297 in 2a3d134
I also think set working directory -> set project also makes more sense, in terms of execution order. My suggestion is that Line 290 in 2a3d134
That will produce an absolute path and avoid the issue entirely. One more thing: the test in this patch wouldn't work because 1) it doesn't create a project, just an empty directory and 2) it doesn't mock-disable RStudio, so it can't meaningfully run in tests run in the RStudio IDE. Reprex: # create a temporary directory
parent_dir <- tempfile()
dir.create(parent_dir)
old <- setwd(parent_dir)
# create a new project
dir.create("child_dir")
file.create("child_dir/.here")
#> [1] TRUE
library(usethis)
# relative directory fails
testthat::with_mock("rstudioapi::isAvailable" = function(x) FALSE, proj_activate("child_dir"))
#> ✔ Changing working directory to 'child_dir/'
#> Error: Directory '/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpaCmKIH/file396e542cd201/child_dir/child_dir' does not exist.
# reset directory and project after failed attempt
setwd(parent_dir)
proj_set(NULL)
#> ✔ Setting active project to '<no active project>'
# absolute directory succeeds
testthat::with_mock("rstudioapi::isAvailable" = function(x) FALSE, proj_activate(normalizePath("child_dir")))
#> ✔ Changing working directory to '/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpaCmKIH/file396e542cd201/child_dir/'
#> ✔ Setting active project to '/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpaCmKIH/file396e542cd201/child_dir'
setwd(old)
unlink(parent_dir, recursive = TRUE) Created on 2020-03-03 by the reprex package (v0.3.0) |
This is great news
…On Tue, Mar 3, 2020, 12:10 PM Malcolm Barrett ***@***.***> wrote:
I spoke with Thomas about this issue last night, and I have a reprex that
should work even when RStudio is running. (Respectfully, this is one of the
reasons why reprexes are important. It took me a while to understand under
what circumstances there is an issue and what the issue actually was).
Fundamentally, the issue is that proj_set() uses the working directory to
set the project directory, but proj_activate() changes the working
directory to path when RStudio is not detected.
Moving proj_set(), as Thomas did, does work, but there is a second, minor
issue. proj_activate() checks path against the working directory, but if
path is relative, comparing it to the absolute working directory will
seem different even if it's not. That will then needlessly reset the
working directory (
https://github.com/r-lib/usethis/blob/2a3d1347e3bee25fbaaca766b42bc93de2de0563/R/proj.R#L297
)
I also think set working directory -> set project also makes more sense,
in terms of execution order. My suggestion is that path get processed
with proj_path_prep() instead of this line
https://github.com/r-lib/usethis/blob/2a3d1347e3bee25fbaaca766b42bc93de2de0563/R/proj.R#L290
That will produce an absolute path and avoid the issue entirely.
One more thing: the test in this patch wouldn't work because 1) it doesn't
create a project, just an empty directory and 2) it doesn't mock-disable
RStudio, so it can't meaningfully run in tests run in the RStudio IDE.
Reprex:
# create a temporary directory
parent_dir <- tempfile()
dir.create(parent_dir)
old <- setwd(parent_dir)
# create a new project
dir.create("child_dir")
file.create("child_dir/.here")
#> [1] TRUE
library(usethis)
# relative directory fails
testthat::with_mock("rstudioapi::isAvailable" = function(x) FALSE, proj_activate("child_dir"))
#> ✔ Changing working directory to 'child_dir/'
#> Error: Directory '/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpaCmKIH/file396e542cd201/child_dir/child_dir' does not exist.
# reset directory and project after failed attempt
setwd(parent_dir)
proj_set(NULL)
#> ✔ Setting active project to '<no active project>'
# absolute directory succeeds
testthat::with_mock("rstudioapi::isAvailable" = function(x) FALSE, proj_activate(normalizePath("child_dir")))
#> ✔ Changing working directory to '/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpaCmKIH/file396e542cd201/child_dir/'
#> ✔ Setting active project to '/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpaCmKIH/file396e542cd201/child_dir'
setwd(old)
unlink(parent_dir, recursive = TRUE)
Created on 2020-03-03 by the reprex package <https://reprex.tidyverse.org>
(v0.3.0)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#954?email_source=notifications&email_token=ACVCMLR5O4KAY3IV5RZ5OUTRFVP2TA5CNFSM4JXRLSLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENU63IY#issuecomment-594144675>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACVCMLWQIRME5NRCEBJVXCTRFVP2TANCNFSM4JXRLSLA>
.
|
Hello,
Instead of going through the process of creating a reproducible example, can you just look at this diff?
Unfortunately the tests cannot be run for some reason, so if there are any regressions that is possibly going to need someone to look at the tests themselves.
Here is the output of the tests:
Code created by my friend
_@thomaslevine.com
He prefers to not use proprietary software so I am giving him a hand.If you seriously need a reprex I am sure that can be produced, but obviously it's merely a formality at this point since we already have a diff.
The text was updated successfully, but these errors were encountered: