From 68e3ed07d1fdbc45146c80dae5f98c5ad7abf41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=B0=B8=E8=B5=AB?= <1259085392@qq.com> Date: Fri, 26 Jun 2026 15:10:21 +0900 Subject: [PATCH] fix: resolve AstrBot runtime version from package metadata --- scripts/prepare-resources/version-sync.mjs | 19 +++++++++++++++++++ .../prepare-resources/version-sync.test.mjs | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/scripts/prepare-resources/version-sync.mjs b/scripts/prepare-resources/version-sync.mjs index 0937c7c..57a2e08 100644 --- a/scripts/prepare-resources/version-sync.mjs +++ b/scripts/prepare-resources/version-sync.mjs @@ -52,6 +52,21 @@ export const readAstrbotVersionFromPyproject = async ({ sourceDir }) => { export const resolveAstrbotRuntimeVersionPath = ({ sourceDir }) => path.join(sourceDir, 'astrbot', 'core', 'config', 'default.py'); +const readAstrbotPackageVersion = async ({ sourceDir }) => { + const initPath = path.join(sourceDir, 'astrbot', '__init__.py'); + if (!existsSync(initPath)) { + throw new Error(`Cannot find AstrBot package version file: ${initPath}`); + } + + const content = await readFile(initPath, 'utf8'); + const match = /^\s*__version__\s*=\s*["']([^"']+)["']\s*(?:#.*)?$/m.exec(content); + if (!match) { + throw new Error(`Cannot resolve AstrBot package __version__ from ${initPath}`); + } + + return match[1].trim(); +}; + export const readAstrbotRuntimeVersion = async ({ sourceDir }) => { const defaultConfigPath = resolveAstrbotRuntimeVersionPath({ sourceDir }); if (!existsSync(defaultConfigPath)) { @@ -61,6 +76,10 @@ export const readAstrbotRuntimeVersion = async ({ sourceDir }) => { const content = await readFile(defaultConfigPath, 'utf8'); const match = /^\s*VERSION\s*=\s*["']([^"']+)["']\s*(?:#.*)?$/m.exec(content); if (!match) { + if (/^\s*VERSION\s*=\s*__version__\s*(?:#.*)?$/m.test(content)) { + return readAstrbotPackageVersion({ sourceDir }); + } + throw new Error(`Cannot resolve AstrBot runtime VERSION from ${defaultConfigPath}`); } diff --git a/scripts/prepare-resources/version-sync.test.mjs b/scripts/prepare-resources/version-sync.test.mjs index e1e1d7d..21db59a 100644 --- a/scripts/prepare-resources/version-sync.test.mjs +++ b/scripts/prepare-resources/version-sync.test.mjs @@ -47,11 +47,13 @@ const createTempAstrBotSource = async ({ pyprojectVersion = '1.2.3', runtimeVers const runtimeVersionPath = resolveAstrbotRuntimeVersionPath({ sourceDir: tempDir }); const configDir = path.dirname(runtimeVersionPath); await mkdir(configDir, { recursive: true }); + await mkdir(path.join(tempDir, 'astrbot'), { recursive: true }); await writeFile( path.join(tempDir, 'pyproject.toml'), `[project]\nname = "AstrBot"\nversion = "${pyprojectVersion}"\n`, 'utf8', ); + await writeFile(path.join(tempDir, 'astrbot', '__init__.py'), `__version__ = "${runtimeVersion}"\n`, 'utf8'); await writeFile( runtimeVersionPath, `import os\n\nVERSION = "${runtimeVersion}"\n`, @@ -99,6 +101,19 @@ test('readAstrbotRuntimeVersion reads static VERSION from default.py', async () } }); +test('readAstrbotRuntimeVersion resolves VERSION imported from package __version__', async () => { + const tempDir = await createTempAstrBotSource({ runtimeVersion: '4.26.1' }); + try { + const runtimeVersionPath = resolveAstrbotRuntimeVersionPath({ sourceDir: tempDir }); + await writeFile(runtimeVersionPath, `from astrbot import __version__\n\nVERSION = __version__\n`, 'utf8'); + + const version = await readAstrbotRuntimeVersion({ sourceDir: tempDir }); + assert.equal(version, '4.26.1'); + } finally { + await rm(tempDir, { recursive: true, force: true }); + } +}); + test('validateAstrbotRuntimeVersion rejects 0.0.0 runtime version', async () => { const tempDir = await createTempAstrBotSource({ runtimeVersion: '0.0.0' }); try {