Skip to content

Commit 417fd6d

Browse files
committed
use: Push scoping from find_modules to use_module
This is part of the refactoring of `_GO_USE_MODULES` to provide correct `@go` command script scope for plugin modules.
1 parent 6e85347 commit 417fd6d

File tree

1 file changed

+12
-11
lines changed
  • lib/internal

1 file changed

+12
-11
lines changed

lib/internal/use

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ _@go.find_plugin_module() {
7878
}
7979

8080
_@go.find_module() {
81-
# If a script imports a plugin module, and that module (`__go_use_caller`)
82-
# tries to import another module from the same plugin, this block will adjust
83-
# the search parameters accordingly.
84-
if [[ "$__go_use_caller" =~ ^$_GO_PLUGINS_DIR/.*/lib/ ]]; then
85-
local _GO_SCRIPTS_DIR="${__go_use_caller%/lib/*}/bin"
86-
local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/bin}"
87-
fi
8881
__go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name"
8982

9083
if [[ ! -f "$__go_module_file" ]]; then
@@ -104,9 +97,17 @@ _@go.use_modules() {
10497
local loaded_module
10598
local loaded_file
10699
local module_index
107-
local __go_use_caller="${BASH_SOURCE[2]}:${BASH_LINENO[1]} ${FUNCNAME[2]}"
100+
local current_caller="${BASH_SOURCE[2]}:${BASH_LINENO[1]} ${FUNCNAME[2]}"
108101
local prev_caller
109102

103+
# If a script imports a plugin module, and that module (`current_caller`)
104+
# tries to import another module from the same plugin, this block will adjust
105+
# the search parameters accordingly.
106+
if [[ "$current_caller" =~ ^$_GO_PLUGINS_DIR/.*/lib/ ]]; then
107+
local _GO_SCRIPTS_DIR="${current_caller%/lib/*}/bin"
108+
local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/bin}"
109+
fi
110+
110111
for __go_module_name in "$@"; do
111112
__go_module_file="$_GO_CORE_DIR/lib/$__go_module_name"
112113

@@ -132,7 +133,7 @@ _@go.use_modules() {
132133
# functions and variables get redefined. Keeping a flat module name
133134
# namespace allows us to detect such potential collisions and issue a
134135
# warning below.
135-
if [[ "$__go_module_file" =~ ^$_GO_PLUGINS_DIR ]]; then
136+
if [[ "$__go_module_file" =~ ^$_GO_PLUGINS_DIR/ ]]; then
136137
__go_module_name="${__go_module_file##*/plugins/}"
137138
__go_module_name="${__go_module_name/\/bin\///}"
138139
__go_module_name="${__go_module_name/\/lib\///}"
@@ -147,7 +148,7 @@ _@go.use_modules() {
147148
# This may happen if a plugin appears more than once in a project tree.
148149
if [[ "$__go_module_file" != "$loaded_file" ]]; then
149150
@go.printf '%s\n' "WARNING: Module: $__go_module_name" \
150-
"imported at: $__go_use_caller" \
151+
"imported at: $current_caller" \
151152
"from file: $__go_module_file" \
152153
"previously imported at: $prev_caller" \
153154
"from file: $loaded_file" >&2
@@ -160,7 +161,7 @@ _@go.use_modules() {
160161
# Prevent self- and circular importing by registering info before sourcing.
161162
_GO_IMPORTED_MODULES+=("$__go_module_name")
162163
_GO_IMPORTED_MODULE_FILES+=("$__go_module_file")
163-
_GO_IMPORTED_MODULE_CALLERS+=("$__go_use_caller")
164+
_GO_IMPORTED_MODULE_CALLERS+=("$current_caller")
164165

165166
if ! . "$__go_module_file"; then
166167
@go.printf 'ERROR: Failed to import %s module from %s at:\n' \

0 commit comments

Comments
 (0)