Skip to content

add on.exit() after setwd() in java_quick_install and java_download #54

@e-kotov

Description

@e-kotov

There is some code that is mainly intended for testing and running examples.

if (temp_dir) {
temp_dir <- tempdir()
setwd(temp_dir)
if (!dir.exists("rJavaEnv_cache")) {
dir.create("rJavaEnv_cache", recursive = TRUE)
}
cache_path <- file.path(temp_dir, "rJavaEnv_cache")
if (!dir.exists("rJavaEnv_project")) {
dir.create("rJavaEnv_project", recursive = TRUE)
}
project_path <- file.path(temp_dir, "rJavaEnv_project")
} else {
cache_path <- getOption("rJavaEnv.cache_path")
}

and

# override cache_path if temp_dir is set to TRUE
if (temp_dir) {
temp_dir <- tempdir()
setwd(temp_dir)
if (!dir.exists("rJavaEnv_cache")) {
dir.create("rJavaEnv_cache", recursive = TRUE)
}
cache_path <- file.path(temp_dir, "rJavaEnv_cache")
}

After running goodpractice::gp("rJavaEnv") I got a suggestion:

✖ avoid calling setwd(), it changes the global environment. If you need it, consider using on.exit() to restore the
    working directory.

    R/java_download.R:36:5
    R/java_quick_install.R:28:5
    vignettes/rJavaEnv-step-by-step.qmd:27:1
    vignettes/rJavaEnv.qmd:48:1

So even though this code is not critical for the functioning of the package, it would indeed be nice to stick to the good practices and add the appropriate on.exit() call to restore the working directory.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions