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

Handle invalid conda-meta/history value #1659

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Fixed segfault encountered in RStudio when rapidly switching
between R and Python chunks in a Quarto document (#1665).

- Improved behavior when the conda binary used to create an environment
could not be resolved (contributed by @tl-hbk, #1654, #1659)

# reticulate 1.39.0

- Python background threads can now run in parallel with the R session (#1641).
Expand Down
44 changes: 41 additions & 3 deletions R/conda.R
Original file line number Diff line number Diff line change
Expand Up @@ -1106,9 +1106,16 @@ get_python_conda_info <- function(python) {
else
file.path(root, "bin/conda")
} else {
# not base env, parse conda-meta history to find the conda binary
# that created it
conda <- python_info_condaenv_find(root)
# not base env
# in ArcGIS env, conda.exe lives in a parent directory
exe <- file.path(root, "../..", "Scripts", "conda.exe")
exe <- normalizePath(exe, winslash = "/", mustWork = FALSE)
if (is_windows() && file.exists(exe)) {
conda <- exe
} else {
# parse conda-meta history to find the conda binary that created it
conda <- conda_history_find(root)
}
}

conda <- normalizePath(conda, winslash = "/", mustWork = FALSE)
Expand All @@ -1123,6 +1130,37 @@ get_python_conda_info <- function(python) {
}


conda_history_find <- function(path) {
exe <- if (is_windows()) "conda.exe" else "conda"

# read history file
histpath <- file.path(path, "conda-meta/history")
if (!file.exists(histpath))
return(NULL)

history <- readLines(histpath, warn = FALSE)

# look for cmd line
pattern <- "^[[:space:]]*#[[:space:]]*cmd:[[:space:]]*"
lines <- grep(pattern, history, value = TRUE)
if (length(lines) == 0)
return(NULL)

# get path to conda script used
script <- sub(
"^#\\s+cmd: (.+?)\\s+(env\\s+create|create|rename)\\s+.*",
"\\1", lines[[1]], perl = TRUE
)
# on Windows, a wrapper script is recorded in the history,
# so instead attempt to find the real conda binary
if(is_windows())
conda <- file.path(dirname(script), exe)
else
conda <- script
normalizePath(conda, winslash = "/", mustWork = FALSE)

}

conda_prefix <- function(conda = "auto") {
conda <- normalizePath(conda_binary(conda),
winslash = "/", mustWork = TRUE)
Expand Down
52 changes: 1 addition & 51 deletions R/python-tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ python_info_condaenv <- function(path) {
python <- file.path(path, suffix)

# find path to conda associated with this env
conda <- python_info_condaenv_find(path)
conda <- get_python_conda_info(python)$conda

list(
python = python,
Expand All @@ -155,56 +155,6 @@ python_info_condaenv <- function(path) {

}

python_info_condaenv_find <- function(path) {

# first, check if we have a condabin
exe <- if (is_windows()) "conda.exe" else "conda"
conda <- file.path(path, "condabin", exe)
if (file.exists(conda))
return(conda)

if (is_windows()) {
# in Anaconda base env, conda.exe lives under Scripts
conda <- file.path(path, "Scripts", exe)
if (file.exists(conda))
return(conda)

# in ArcGIS env, conda.exe lives in a parent directory
conda <- file.path(path, "../..", "Scripts", exe)
conda <- normalizePath(conda, winslash = "/", mustWork = FALSE)
if (file.exists(conda))
return(conda)

}

# read history file
histpath <- file.path(path, "conda-meta/history")
if (!file.exists(histpath))
return(NULL)

history <- readLines(histpath, warn = FALSE)

# look for cmd line
pattern <- "^[[:space:]]*#[[:space:]]*cmd:[[:space:]]*"
lines <- grep(pattern, history, value = TRUE)
if (length(lines) == 0)
return(NULL)

# get path to conda script used
script <- sub(
"^#\\s+cmd: (.+?)\\s+(env\\s+create|create|rename)\\s+.*",
"\\1", lines[[1]], perl = TRUE
)
# on Windows, a wrapper script is recorded in the history,
# so instead attempt to find the real conda binary
if(is_windows())
conda <- file.path(dirname(script), exe)
else
conda <- script
normalizePath(conda, winslash = "/", mustWork = FALSE)

}

python_info_system <- function(path, python) {

list(
Expand Down
Loading