diff --git a/R/loadfast.R b/R/loadfast.R index 5f9255f..02b606a 100644 --- a/R/loadfast.R +++ b/R/loadfast.R @@ -59,7 +59,7 @@ load_fast <- function(path = ".", helpers = TRUE, attach_testthat = NULL, full = desc_path <- file.path(abs_path, "DESCRIPTION") if (!file.exists(desc_path)) stop("DESCRIPTION file not found at: ", desc_path) desc_fields <- read.dcf(desc_path) - pkg_name <- if ("Package" %in% colnames(desc_fields)) trimws(desc_fields[1L, "Package"]) else "" + pkg_name <- if ("Package" %in% colnames(desc_fields)) unname(trimws(desc_fields[1L, "Package"])) else "" if (!nzchar(pkg_name)) stop("No valid 'Package' field found in DESCRIPTION") pkg_env_name <- paste0("package:", pkg_name) @@ -257,6 +257,9 @@ load_fast <- function(path = ".", helpers = TRUE, attach_testthat = NULL, full = ns_env[[".__NAMESPACE__."]] <- info info[["spec"]] <- c(name = pkg_name, version = "0.0.0") setNamespaceInfo(ns_env, "exports", new.env(hash = TRUE, parent = baseenv())) + dimpenv <- new.env(parent = baseenv(), hash = TRUE) + attr(dimpenv, "name") <- paste0("lazydata:", pkg_name) + setNamespaceInfo(ns_env, "lazydata", dimpenv) setNamespaceInfo(ns_env, "imports", list(base = TRUE)) setNamespaceInfo(ns_env, "path", abs_path) setNamespaceInfo(ns_env, "dynlibs", NULL) diff --git a/test_loadfast.R b/test_loadfast.R index 1b02b21..e427f1a 100644 --- a/test_loadfast.R +++ b/test_loadfast.R @@ -169,6 +169,14 @@ check("isNamespace() returns TRUE", quote( isNamespace(ns) )) +check("lazydata env exists in namespace info", quote( + is.environment(ns[[".__NAMESPACE__."]][["lazydata"]]) +)) + +check("lazydata env is named correctly", quote( + attr(ns[[".__NAMESPACE__."]][["lazydata"]], "name") == "lazydata:devpackage" +)) + # --- Search path --- check("package:devpackage is on the search path", quote( "package:devpackage" %in% search() @@ -1153,6 +1161,22 @@ check("same-name: second path is now the active namespace path", quote( ) )) +# -------------------------------------------------------------------------- +# 3f2: base::attachNamespace compatibility +# -------------------------------------------------------------------------- +cat("\n--- 3f2: base::attachNamespace compatibility ---\n\n") + +tmp_attach <- tempfile("loadfast_attach_") +copy_baseline(tmp_attach) +rename_package(tmp_attach, "pkgattach") +ns_attach <- load_fast(tmp_attach, helpers = FALSE, attach_testthat = FALSE) + +check("base::attachNamespace does not fail due to missing lazydata env", quote({ + detach("package:pkgattach", unload = FALSE) + base::attachNamespace(ns_attach) + "package:pkgattach" %in% search() +})) + # -------------------------------------------------------------------------- # 3g: Same-name different-path cache flip-flop and renv.lock path scoping # --------------------------------------------------------------------------