[Bug] astrbot init 选不在当前路径安装,报错并残留data文件夹
What happened / 发生了什么
astrbot init安装,在提示是否安装在当前路径时,选择否,会报错初始化失败,并且会在当前目录下残留data目录
Reproduce / 如何复现?
astrbot init
___ _______.___________..______ .______ ______ .___________.
/ \ / | || _ \ | _ \ / __ \ | |
/ ^ \ | (----`---| |----`| |_) | | |_) | | | | | `---| |----`
/ /_\ \ \ \ | | | / | _ < | | | | | |
/ _____ \ .----) | | | | |\ \----.| |_) | | `--' | | |
/__/ \__\ |_______/ |__| | _| `._____||______/ \______/ |__|
Welcome to AstrBot CLI!
AstrBot CLI version: 4.25.3
Initializing AstrBot...
Install AstrBot to this directory? /home/initial01/Code/tmp [Y/n]: n
Error: Initialization failed:
AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器
安装采用uv包安装,版本为4.25.3 uv tool install astrbot --python 3.12 astrbot init # Only execute this command for the first time to initialize the environment
OS
Linux
Logs / 报错日志
(base) initial01@wsl:~/Code/tmp$ ls
(base) initial01@wsl:~/Code/tmp$ astrbot init
___ _______.___________..______ .______ ______ .___________.
/ \ / | || _ \ | _ \ / __ \ | |
/ ^ \ | (----`---| |----`| |_) | | |_) | | | | | `---| |----`
/ /_\ \ \ \ | | | / | _ < | | | | | |
/ _____ \ .----) | | | | |\ \----.| |_) | | `--' | | |
/__/ \__\ |_______/ |__| | _| `._____||______/ \______/ |__|
Welcome to AstrBot CLI!
AstrBot CLI version: 4.25.3
Initializing AstrBot...
Install AstrBot to this directory? /home/initial01/Code/tmp [Y/n]: n
Error: Initialization failed:
(base) initial01@wsl:~/Code/tmp$ ls
data
(base) initial01@wsl:~/Code/tmp$
Are you willing to submit a PR? / 你愿意提交 PR 吗?
- [x] #8854
Code of Conduct
- [x] I have read and agree to abide by the project's Code of Conduct。
Hi @Initial01LingXu! I'm Dosu and I'm helping the AstrBotTeam team.
这个 bug 的根因在 astrbot/cli/commands/cmd_init.py 中有两个问题:
1. 错误信息误导
当用户选择 "n" 时,click.confirm(..., abort=True) 会抛出 click.Abort 异常,但第 69-70 行的 except Exception 捕获了所有异常(包括 click.Abort),统一包装为 "Initialization failed: {e}" [1]。由于 click.Abort 的 str() 为空字符串,所以用户看到的是 Initialization failed: 后面没有任何说明。
修复方式是在通用的 except Exception 之前单独捕获 click.Abort:
except click.Abort:
click.echo("\nAborted.")
2. 残留 data 目录
虽然 click.confirm 在第 28 行 [2],理论上选 "n" 后 abort 会阻止后续目录创建,但实际上 get_astrbot_root() 的调用或者 FileLock 创建 lock 文件时,可能间接触发了 data 目录的创建。另一个可能性是 click.Abort 被 asyncio.run() 包装后行为异常,导致部分代码仍然执行。建议排查 get_astrbot_root() 是否在返回路径时有副作用创建了 data 目录。
总结:核心问题是 except Exception 不应该捕获 click.Abort,应单独处理。
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
很好找到了创建data目录以及其下方文件的所在代码,在astrbot/core/__init__.py
里面存放了立即执行的代码,切换至懒加载应该会影响一大批代码,并且重构困难;
我的龟龟,为啥要在__init__.py里写这种代码?考虑直接except click.Abort:里面删除下data文件夹得了
# 初始化数据存储文件夹
os.makedirs(get_astrbot_data_path(), exist_ok=True)
DEMO_MODE = os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")
astrbot_config = AstrBotConfig()
t2i_base_url = astrbot_config.get("t2i_endpoint", "https://t2i.soulter.top/text2img")
html_renderer = HtmlRenderer(t2i_base_url)
logger = LogManager.GetLogger(log_name="astrbot")
LogManager.configure_logger(logger, astrbot_config)
LogManager.configure_trace_logger(astrbot_config)
db_helper = SQLiteDatabase(DB_PATH)
# 简单的偏好设置存储, 这里后续应该存储到数据库中, 一些部分可以存储到配置中
sp = SharedPreferences(db_helper=db_helper)
# 文件令牌服务
file_token_service = FileTokenService()
pip_installer = PipInstaller(
astrbot_config.get("pip_install_arg", ""),
astrbot_config.get("pypi_index_url", None),
)
诸位,请看文件夹/astrbot/core/__init__.py
import os
from astrbot.core.config import AstrBotConfig
from astrbot.core.config.default import DB_PATH
from astrbot.core.db.sqlite import SQLiteDatabase
from astrbot.core.file_token_service import FileTokenService
from astrbot.core.utils.pip_installer import (
DependencyConflictError as DependencyConflictError,
)
from astrbot.core.utils.pip_installer import (
PipInstaller,
)
from astrbot.core.utils.requirements_utils import (
RequirementsPrecheckFailed as RequirementsPrecheckFailed,
)
from astrbot.core.utils.requirements_utils import (
find_missing_requirements as find_missing_requirements,
)
from astrbot.core.utils.requirements_utils import (
find_missing_requirements_or_raise as find_missing_requirements_or_raise,
)
from astrbot.core.utils.shared_preferences import SharedPreferences
from astrbot.core.utils.t2i.renderer import HtmlRenderer
from .log import LogBroker, LogManager # noqa
from .utils.astrbot_path import get_astrbot_data_path
# 初始化数据存储文件夹
os.makedirs(get_astrbot_data_path(), exist_ok=True)
DEMO_MODE = os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")
astrbot_config = AstrBotConfig()
t2i_base_url = astrbot_config.get("t2i_endpoint", "https://t2i.soulter.top/text2img")
html_renderer = HtmlRenderer(t2i_base_url)
logger = LogManager.GetLogger(log_name="astrbot")
LogManager.configure_logger(logger, astrbot_config)
LogManager.configure_trace_logger(astrbot_config)
db_helper = SQLiteDatabase(DB_PATH)
# 简单的偏好设置存储, 这里后续应该存储到数据库中, 一些部分可以存储到配置中
sp = SharedPreferences(db_helper=db_helper)
# 文件令牌服务
file_token_service = FileTokenService()
pip_installer = PipInstaller(
astrbot_config.get("pip_install_arg", ""),
astrbot_config.get("pypi_index_url", None),
)
这才是真正原因,只要某个py文件有
import astrbot.core
from astrbot.core import ***
from astrbot.core.*** import ***
__init__.py里东西就会神不知鬼不觉地执行了一次
我认为,这是不允许的,据我所见,astrbot很多代码大量充斥这种语句,对吧?后果可想而知。
故真正的修复方法是,请把这些移走,不要except click.Abort:,这种只能修表层。
诸位,请看文件夹
/astrbot/core/__init__.pyimport os
from astrbot.core.config import AstrBotConfig from astrbot.core.config.default import DB_PATH from astrbot.core.db.sqlite import SQLiteDatabase from astrbot.core.file_token_service import FileTokenService from astrbot.core.utils.pip_installer import ( DependencyConflictError as DependencyConflictError, ) from astrbot.core.utils.pip_installer import ( PipInstaller, ) from astrbot.core.utils.requirements_utils import ( RequirementsPrecheckFailed as RequirementsPrecheckFailed, ) from astrbot.core.utils.requirements_utils import ( find_missing_requirements as find_missing_requirements, ) from astrbot.core.utils.requirements_utils import ( find_missing_requirements_or_raise as find_missing_requirements_or_raise, ) from astrbot.core.utils.shared_preferences import SharedPreferences from astrbot.core.utils.t2i.renderer import HtmlRenderer
from .log import LogBroker, LogManager # noqa from .utils.astrbot_path import get_astrbot_data_path
初始化数据存储文件夹
os.makedirs(get_astrbot_data_path(), exist_ok=True)
DEMO_MODE = os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")
astrbot_config = AstrBotConfig() t2i_base_url = astrbot_config.get("t2i_endpoint", "https://t2i.soulter.top/text2img") html_renderer = HtmlRenderer(t2i_base_url) logger = LogManager.GetLogger(log_name="astrbot") LogManager.configure_logger(logger, astrbot_config) LogManager.configure_trace_logger(astrbot_config) db_helper = SQLiteDatabase(DB_PATH)
简单的偏好设置存储, 这里后续应该存储到数据库中, 一些部分可以存储到配置中
sp = SharedPreferences(db_helper=db_helper)
文件令牌服务
file_token_service = FileTokenService() pip_installer = PipInstaller( astrbot_config.get("pip_install_arg", ""), astrbot_config.get("pypi_index_url", None), ) 这才是真正原因,只要某个py文件有
import astrbot.core from astrbot.core import *** from astrbot.core.*** import ***
__init__.py里东西就会神不知鬼不觉地执行了一次 我认为,这是不允许的,据我所见,astrbot很多代码大量充斥这种语句,对吧?后果可想而知。 故真正的修复方法是,请把这些移走,不要except click.Abort:,这种只能修表层。
确实,不能只修改表层,只是一个小小问题,查到最后竟牵扯如此之重;另外查了下,这个core涉及387个文件1517次引用(○´・д・)ノ。
另外__init__.py里面初始化的变量肯定又被其他代码使用了,改这一处牵扯甚多,给跪了。