Skip to content
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

Working directory inside with_dir/local_dir does not match provided new directory #223

Closed
solmos opened this issue Mar 27, 2023 · 1 comment

Comments

@solmos
Copy link

solmos commented Mar 27, 2023

When using with_dir() or local_dir(), I find that the new working directory within these functions do not exactly match the provided path to the new argument.

Reproducible example:

my_dir <- tempdir()
withr::with_dir(my_dir, {
  print(my_dir)
  print(getwd())
  my_dir == getwd()
})
#>[1] "/var/folders/gx/vdqbfzvs66b1swy4qv7pch7c0000gp/T//RtmpyNFcnx"
#>[1] "/private/var/folders/gx/vdqbfzvs66b1swy4qv7pch7c0000gp/T/RtmpyNFcnx"
#>[1] FALSE

Is this the expected behaviour?

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.2.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.2.2 tools_4.2.2    withr_2.5.0   
@hadley
Copy link
Member

hadley commented Jan 9, 2024

This appears to be a "feature" of setwd()/`getwd():

tempdir()
#> [1] "/tmp/RtmpDe7XOY"

setwd(tempdir())
getwd()
#> [1] "/private/tmp/RtmpDe7XOY"

Created on 2024-01-09 with reprex v2.0.2.9000

In general, if you're trying to compare directory names, you should call normalizePath() on them:

normalizePath(tempdir())
#> [1] "/private/tmp/RtmpPlKlm9"

setwd(tempdir())
normalizePath(getwd())
#> [1] "/private/tmp/RtmpPlKlm9"

Created on 2024-01-09 with reprex v2.0.2.9000

@hadley hadley closed this as completed Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants