Skip to content

Commit

Permalink
Fix bug in finding the calling module’s path
Browse files Browse the repository at this point in the history
Fix a bug in the previous logic whereby a module calling `import` was
not found internally when looking for the module’s path, and the current
working directory was used instead.

Resolves #18 and closes #24.
  • Loading branch information
klmr committed Jun 19, 2014
1 parent f45472c commit 6807f1c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion R/find_module.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ find_module = function (module) {
file_pattern = sprintf('^%s\\.[rR]$', suffix)

search_path = if (parts[1] %in% c('.', '..'))
module_base_path(environment())
calling_module_path()
else
import_search_path()
candidate_paths = file.path(search_path, module_path)
Expand Down Expand Up @@ -44,6 +44,19 @@ find_module = function (module) {
normalizePath(unname(hits[1]))
}

calling_module_path = function () {
# Go up through caller hierarchy until the caller’s `parent.env()` is no
# longer the same as the `parent.env()` of this function. This indicates
# that we have reached the actual caller of `import`.
package_env = parent.env(environment())
n = 1

while (identical(parent.env(parent.frame(n)), package_env))
n = n + 1

module_base_path(parent.frame(n))
}

#' Return a list of paths to a module’s \code{__init__.r} files
#'
#' @param module character string of the fully qualified module name
Expand Down

0 comments on commit 6807f1c

Please sign in to comment.