Gábor Csárdi
2022-Jan-21 16:26 UTC
[Rd] isNamespaceLoaded() while the namespace is loading
We ran into a bug in our package that seems to boil down to isNamespaceLoaded() returning TRUE for namespaces that R is currently loading. We had something like this in an .onLoad function: if (isNamespaceLoaded("upstream")) { upstream::upstream_function() } Which seems OK, unless upstream (recursively) imports the package having this code. Should that happen, the loading of upstream triggers the loading of this package as well and isNamespaceLoaded() seems to return TRUE, even though the namespace of upstream is not fully loaded yet, and we get an error that looks like this: Error : .onLoad failed in loadNamespace() for 'foo', details: call: NULL error: 'upstream_function' is not an exported object from 'namespace:upstream' I wonder if isNamespaceLoaded() returning TRUE is correct in this case, or returning FALSE would be better. Or maybe it would make sense to have a way to query the packages that are being loaded currently? AFAICT this works, but it does use some implementation details from loadNamespace(), so it does not seem like a proper solution: dynGet("__NameSpacesLoading__", NULL) Another workaround is something like this: is_loaded <- function(pkg) { if (!isNamespaceLoaded(pkg)) return(FALSE) tryCatch({ loadNamespace(pkg) TRUE }, error = function(err) FALSE) } which forces an error for currently loading namespaces by triggering a (fake) recursive dependency. Thanks, Gabor
Lionel Henry
2023-Mar-16 17:49 UTC
[Rd] isNamespaceLoaded() while the namespace is loading
Hello, We've run into this issue multiple times and it's often a head scratcher when it happens. We are using workarounds but it would be great to fix this for R 4.3. Would an R core member have time to review the patch that we supplied in https://bugs.r-project.org/show_bug.cgi?id=18489 ? Best, Lionel On 1/21/22, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:> We ran into a bug in our package that seems to boil down to > isNamespaceLoaded() returning TRUE for namespaces that R is currently > loading. > > We had something like this in an .onLoad function: > > if (isNamespaceLoaded("upstream")) { > upstream::upstream_function() > } > > Which seems OK, unless upstream (recursively) imports the package > having this code. Should that happen, the loading of upstream triggers > the loading of this package as well and isNamespaceLoaded() seems to > return TRUE, even though the namespace of upstream is not fully loaded > yet, and we get an error that looks like this: > > Error : .onLoad failed in loadNamespace() for 'foo', details: > call: NULL > error: 'upstream_function' is not an exported object from > 'namespace:upstream' > > I wonder if isNamespaceLoaded() returning TRUE is correct in this > case, or returning FALSE would be better. Or maybe it would make sense > to have a way to query the packages that are being loaded currently? > > AFAICT this works, but it does use some implementation details from > loadNamespace(), so it does not seem like a proper solution: > > dynGet("__NameSpacesLoading__", NULL) > > Another workaround is something like this: > > is_loaded <- function(pkg) { > if (!isNamespaceLoaded(pkg)) return(FALSE) > tryCatch({ > loadNamespace(pkg) > TRUE > }, error = function(err) FALSE) > } > > which forces an error for currently loading namespaces by triggering a > (fake) recursive dependency. > > Thanks, > Gabor > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >