Skip to content

Commit 92bc468

Browse files
committed
use: Detect module path before sourcing
Closes #25. Now any modules containing errors will be identified as such, rather than being reported as unknown.
1 parent 5ea7820 commit 92bc468

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/internal/use

Lines changed: 17 additions & 5 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,28 @@ 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
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+
fi
89+
if [[ ! -f "$__go_module_file" ]]; then
90+
__go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name"
91+
fi
92+
if [[ ! -f "$__go_module_file" ]]; then
8793
@go.printf "ERROR: Unknown module: $__go_module_name" >&2
8894
exit 1
8995
fi
96+
97+
if ! . "$__go_module_file"; then
98+
@go.printf "ERROR: Module contains errors: $__go_module_file" >&2
99+
exit 1
100+
fi
90101
done
91102

92103
unset __go_loaded_module
104+
unset __go_module_file
93105
unset __go_module_name

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 contains errors: ${TEST_MODULES[1]}")
82+
local IFS=$'\n'
83+
assert_failure "${expected[*]}"
84+
}

0 commit comments

Comments
 (0)