已合并
fix: add shebang and set -e to runtime set_env.sh #3870
fix: add shebang and set -e to runtime set_env.sh #3870
已合并
sinat_31531339创建于 19 天前
1 个文件变更+14-0
Mscripts/package/runtime/set_env.sh+14-0
@@ -1,3 +1,4 @@
1#!/bin/bash
1# -----------------------------------------------------------------------------------------------------------2# -----------------------------------------------------------------------------------------------------------
2# Copyright (c) 2025 Huawei Technologies Co., Ltd.3# Copyright (c) 2025 Huawei Technologies Co., Ltd.
3# This program is free software, you can redistribute it and/or modify it under the terms and conditions of4# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
@@ -8,6 +9,13 @@
8# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
9# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
10 11 
12# 本脚本通常以 source 方式加载,记录调用方原始 errexit 状态,避免 set -e 泄漏到调用方 shell。
13case "$-" in
14 *e*) __setenv_errexit_was_set="y" ;;
15 *) __setenv_errexit_was_set="n" ;;
16esac
17set -e
atomgit-bot
atomgit-botatomgit-bot19 天前

🟡 Medium Priority

变更在第 12 行新增了 set -e。该脚本的主要使用方式是通过 source 引入(README 及所有 example 均使用 source <path>/set_env.sh),而非直接执行。

问题链: 新增 set -e → 脚本被 source 到当前 shell → set -e 在当前 shell 中生效 → 脚本结束后 set -e 未恢复 → 当前 shell 后续任何命令返回非零都会导致 shell 退出。

触发条件:用户在交互式终端执行 source /usr/local/Ascend/cann/set_env.sh 后,输入任何会失败的命令(如 ls /nonexistentgrep pattern nofile),shell 会立即退出,可能丢失未保存的工作状态。

说明:脚本内的业务逻辑本身对 set -e 是安全的(函数内的 if 条件分支、|| true 保护、$() 命令替换等均在 set -e 豁免范围内)。问题仅在于未在脚本结束前恢复 -e 选项的原始状态。

likedislike
18 
11append_env() {19append_env() {
12 local name="$1"20 local name="$1"
13 local value="$2"21 local value="$2"
@@ -134,3 +142,9 @@ setenv_main() {
134}142}
135 143 
136setenv_main "$@"144setenv_main "$@"
145 
146# 恢复调用方原始 errexit 状态,避免污染 source 本脚本的 shell。
147if [ "$__setenv_errexit_was_set" = "n" ]; then
148 set +e
149fi
150unset __setenv_errexit_was_set