Skip to content

Commit 22bace2

Browse files
authored
Merge pull request #26 from mbland/module-import
use: Detect module path before sourcing
2 parents 2981cf2 + 8833762 commit 22bace2

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

lib/internal/use

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
# @go modules --imported
6969

7070
declare __go_module_name
71+
declare __go_module_file
7172
declare __go_loaded_module
7273

7374
for __go_module_name in "$@"; do
@@ -77,17 +78,30 @@ for __go_module_name in "$@"; do
7778
fi
7879
done
7980

80-
# Prevent self- and circular importing.
81+
# Prevent self- and circular importing by registering name before sourcing.
8182
_GO_IMPORTED_MODULES+=("$__go_module_name")
83+
__go_module_file="$_GO_CORE_DIR/lib/$__go_module_name"
8284

83-
# Convert <plugin>/<module> to _GO_SCRIPTS_DIR/plugins/<plugin>/lib/<module>
84-
if ! . "$_GO_CORE_DIR/lib/$__go_module_name" 2>/dev/null &&
85-
! . "$_GO_SCRIPTS_DIR/plugins/${__go_module_name/\///lib/}" 2>/dev/null &&
86-
! . "$_GO_SCRIPTS_DIR/lib/$__go_module_name" 2>/dev/null; then
87-
@go.printf "ERROR: Unknown module: $__go_module_name" >&2
85+
if [[ ! -f "$__go_module_file" ]]; then
86+
# Convert <plugin>/<module> to plugins/<plugin>/lib/<module>
87+
__go_module_file="$_GO_SCRIPTS_DIR/plugins/${__go_module_name/\///lib/}"
88+
89+
if [[ ! -f "$__go_module_file" ]]; then
90+
__go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name"
91+
92+
if [[ ! -f "$__go_module_file" ]]; then
93+
@go.printf "ERROR: Unknown module: $__go_module_name\n" >&2
94+
exit 1
95+
fi
96+
fi
97+
fi
98+
99+
if ! . "$__go_module_file"; then
100+
@go.printf "ERROR: Module import failed for: $__go_module_file\n" >&2
88101
exit 1
89102
fi
90103
done
91104

92105
unset __go_loaded_module
106+
unset __go_module_file
93107
unset __go_module_name

libexec/modules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
# 'f*/' All modules in all plugins that begin with 'f'
4444
# 'f*/b*' Modules beginning with 'b' in all plugins that begin with 'f'
4545
#
46-
# For detailed information about the module system, run `{{go}} {{cmd}} help`
46+
# For detailed information about the module system, run `{{go}} {{cmd}} --help`
4747
# without a `<module-name>` argument.
4848

4949
_@go.modules_help() {

tests/modules/use.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ teardown() {
7171
local IFS=$'\n'
7272
assert_success "${EXPECTED[*]}"
7373
}
74+
75+
@test "$SUITE: error if module contains errors" {
76+
echo "This is a totally broken module." >> "${TEST_MODULES[1]}"
77+
run "$TEST_GO_SCRIPT" "${IMPORTS[@]}"
78+
79+
local expected=("${IMPORTS[0]##*/} loaded"
80+
"${TEST_MODULES[1]}: line 2: This: command not found"
81+
"ERROR: Module import failed for: ${TEST_MODULES[1]}")
82+
local IFS=$'\n'
83+
assert_failure "${expected[*]}"
84+
}

0 commit comments

Comments
 (0)