已合并
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创建于 17 天前
sinat_31531339
17 天前

修正 scripts/package/runtime/set_env.sh 两个缺陷:

  1. 补充缺失的 shell 头部 set -e
  2. 首行补充 #!/bin/bash
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 sinat_31531339 的贡献)
sinat_31531339sinat_31531339
17 天前 创建了 pull request,commit 48fbc46e
atomgit-bot
atomgit-bot
17 天前 评论:

变更摘要

此 PR 修复了 scripts/package/runtime/set_env.sh 脚本的两个缺陷:补充了缺失的 shebang 声明和 set -e 选项,以确保脚本在正确的 shell 环境下执行,并在任何命令失败时立即退出,避免错误被静默忽略。

主要改动

  • 添加 shebang 声明: 在文件首行新增 #!/bin/bash,明确指定脚本使用 Bash 解释器执行,避免在不同环境下因默认 shell 差异导致的兼容性问题。
  • 添加 set -e 选项: 在脚本主体逻辑之前加入 set -e,使得脚本中任何命令执行失败时立即退出,提升脚本的健壮性和错误可见性。
likedislike
atomgit-bot
atomgit-bot
17 天前 评论:

代码审查

审查总结

已审查文件: scripts/package/runtime/set_env.sh — 发现 1 个问题

问题分布:

优先级 数量
P0 0
P1 0
P2 1
P3 0

整体风险评估:中等。

变更本身意图正确——为该 source 型脚本补充 #!/bin/bash 声明和 set -e 严格错误处理。但 set -e 缺少保存/恢复机制,在脚本被 source 后会泄漏到调用方 shell,导致交互式会话在任意命令失败时意外退出。脚本内业务逻辑对 set -e 本身是安全的,问题仅在于副作用未清理。建议在脚本末尾根据原始 errexit 状态恢复,或采用函数作用域隔离。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
CANN-robotCANN-robot成员
17 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
17 天前 评论:

CLA Signature Pass

sinat_31531339, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robot
CANN-robot成员
17 天前 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/runtime 王涛, 卢煜坤 (2/2) 王涛, 卢煜坤 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

sinat_31531339, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robot成员
17 天前 将wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood设为评审人
CANN-robotCANN-robot成员
17 天前 将wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood设为审查人
sinat_31531339
sinat_31531339
17 天前 评论:

compile

likedislike
sinat_31531339sinat_31531339
17 天前 update merge request[project id: 8810053, iid: 3870, commit_id: 69d6af2acc3a15148e579bb1f0690c7f207fce6d] virtual merging success
CANN-robotCANN-robot成员
17 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
17 天前 评论:

流水线任务触发成功
任务链接 [0986f794f7d047219cadb4a6ca7f4c82][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test_acl ✅ SUCCESS >>>>>
UT_Test_rts_v201 ✅ SUCCESS >>>>>
UT_Test_rts_c ✅ SUCCESS >>>>>
UT_Test_rts_david ✅ SUCCESS >>>>>
UT_Test_rts_910b ✅ SUCCESS >>>>>
UT_Test_rts_common ✅ SUCCESS >>>>>
UT_Test_platform ✅ SUCCESS >>>>>
UT_Test_qs ✅ SUCCESS >>>>>
UT_Test_aicpusd ✅ SUCCESS >>>>>
UT_Test_tsd ✅ SUCCESS >>>>>
UT_Test_error_manager ✅ SUCCESS >>>>>
UT_Test_slog ✅ SUCCESS >>>>>
UT_Test_atrace ✅ SUCCESS >>>>>
UT_Test_msprof_part1 ✅ SUCCESS
UT_Test_msprof_part2 ✅ SUCCESS
UT_Test_adump ✅ SUCCESS >>>>>
UT_Test_mmpa ✅ SUCCESS >>>>>
UT_Test_camodel_check ✅ SUCCESS
API_Check ✅ SUCCESS >>>>>
PreSmoke_A900_npupool ✅ SUCCESS >>>>>
UT_Test_rts_report ✅ SUCCESS >>>>>
UT_Test_msprof_report ✅ SUCCESS >>>>>

[2026-07-27 22:05:26]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
17 天前 评论:

流水线任务触发成功
任务链接 [0884a38013c04a33bcbeb44e2538bdb3][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Check_Pr ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS

[2026-07-27 21:57:26]    CI执行结束

likedislike
atomgit-bot
atomgit-bot17 天前进行代码检视1
scripts/package/runtime/set_env.sh
@@ -1,7 +1,8 @@
1+#!/bin/bash
12# -----------------------------------------------------------------------------------------------------------
23# Copyright (c) 2025 Huawei Technologies Co., Ltd.
34# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
@@ -8,6 +9,8 @@
89# See LICENSE in the root of the software repository for the full text of the License.
910# -----------------------------------------------------------------------------------------------------------
1011 
12+set -e
atomgit-bot
atomgit-bot17 天前评论:

🟡 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
CANN-robotCANN-robot成员
17 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
17 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
17 天前 添加了label:ci-pipeline-passed
sinat_31531339sinat_31531339
17 天前 update merge request[project id: 8810053, iid: 3870, commit_id: 825d312c89e7d59e9085bcb3eee8bf08dad8aa02] virtual merging success
sinat_31531339sinat_31531339
17 天前 推送  1 个提交:f96d2318-fix: restore caller errexit state to avoid set -e leaking on source
sinat_31531339
sinat_31531339
17 天前 评论:

感谢审查。P2 建议已采纳并修复(commit f96d2318):

本脚本通常以 source 方式加载,直接 set -e 会泄漏到调用方交互 shell,导致后续任意命令失败即退出会话。修复方式:

  1. 脚本开头用 case "$-" in *e*) 记录调用方原始 errexit 状态;
  2. setenv_main 执行完毕后,若调用方原本未开启 errexitset +e 恢复,并 unset 临时变量。

已验证两种场景:

  • 调用方原本未开 errexit → source 后不泄漏;
  • 调用方原本已开 errexit → source 后保持开启,不被误关。

脚本内部业务逻辑此前已确认对 set -e 安全,本次仅清理其对调用方的副作用。

likedislike
CANN-robotCANN-robot成员
17 天前 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
17 天前 删除了label:ci-pipeline-passed
CANN-robot
CANN-robot成员
17 天前 评论:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed.

likedislike
sinat_31531339
sinat_31531339
16 天前 评论:

compile

likedislike
sinat_31531339sinat_31531339
16 天前 update merge request[project id: 8810053, iid: 3870, commit_id: a049e74b9231bb196824e12458c410294230dcfa] virtual merging success
CANN-robotCANN-robot成员
16 天前 删除了label:api-check-pass
CANN-robotCANN-robot成员
16 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
16 天前 评论:

流水线任务触发成功
任务链接 [7eb04f14758f4541b01eb0c826950aae][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test_acl ✅ SUCCESS >>>>>
UT_Test_rts_v201 ✅ SUCCESS >>>>>
UT_Test_rts_c ✅ SUCCESS >>>>>
UT_Test_rts_david ✅ SUCCESS >>>>>
UT_Test_rts_910b ✅ SUCCESS >>>>>
UT_Test_rts_common ✅ SUCCESS >>>>>
UT_Test_platform ✅ SUCCESS >>>>>
UT_Test_qs ✅ SUCCESS >>>>>
UT_Test_aicpusd ✅ SUCCESS >>>>>
UT_Test_tsd ✅ SUCCESS >>>>>
UT_Test_error_manager ✅ SUCCESS >>>>>
UT_Test_slog ✅ SUCCESS >>>>>
UT_Test_atrace ✅ SUCCESS >>>>>
UT_Test_msprof_part1 ✅ SUCCESS
UT_Test_msprof_part2 ✅ SUCCESS
UT_Test_adump ✅ SUCCESS >>>>>
UT_Test_mmpa ✅ SUCCESS >>>>>
UT_Test_camodel_check ✅ SUCCESS
API_Check ✅ SUCCESS >>>>>
PreSmoke_A900_npupool ✅ SUCCESS >>>>>
UT_Test_rts_report ✅ SUCCESS >>>>>
UT_Test_msprof_report ✅ SUCCESS >>>>>

[2026-07-28 10:59:38]    CI执行结束

likedislike
ykl999
ykl999成员
16 天前 评论:

/approve
/lgtm

likedislike
CANN-robotCANN-robot成员
16 天前 添加了label:approved
CANN-robot
CANN-robot成员
16 天前 评论:

流水线任务触发成功
任务链接 [ea3ac8c0499e4af7a4e6ad061a5a79a5][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Check_Pr ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS

[2026-07-28 10:51:38]    CI执行结束

likedislike
wangtao成员
16 天前 评论:

Code Review Summary

变更概要

本 PR 修正了 scripts/package/runtime/set_env.sh 的两个缺陷:

  1. 新增 shebang #!/bin/bash
  2. 添加 set -e 错误处理机制,并实现了 errexit 状态保存与恢复逻辑

审查结果

未发现任何问题

优点

  1. ✅ 补充了缺失的 shebang,使脚本可以作为独立可执行文件运行
  2. ✅ 添加了 set -e 错误处理机制,提高脚本健壮性
  3. ✅ 精心设计的 errexit 状态保存与恢复机制,避免污染调用方 shell 环境
  4. ✅ 代码清晰,注释充分,易于维护
  5. ✅ 向后兼容,不会破坏现有使用方式
  6. ✅ 变量命名规范,使用 __setenv_ 前缀避免冲突

代码质量评估

  • Shell 脚本编写规范,符合最佳实践
  • 考虑了 source 加载场景的特殊性,设计周到
  • 错误处理机制完善

结论

LGTM (Looks Good To Me) - 代码质量优秀,强烈建议合入。


本次审查未发现任何必须修改或建议修改的问题。

likedislike
CANN-robotCANN-robot成员
16 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
16 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
16 天前 添加了label:ci-pipeline-passed
wangtao成员
16 天前 评论:

/approve

likedislike
CANN-robotCANN-robot成员
16 天前 添加了label:lgtm
CANN-robotCANN-robot成员
16 天前 解决了最后一个问题
CANN-robotCANN-robot成员
16 天前 合入了pull request