From 3117ab3dec73a57110c1d27e319b4dbdc8c4a9e5 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 22 Aug 2026 04:04:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(tpc):=20import=20fail-fast=20=E5=88=A4?= =?UTF-8?q?=E6=8D=AE=E8=AF=AF=E6=8A=A5=E4=BF=AE=E5=A4=8D=E2=80=94=E2=80=94?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F=E6=89=BE=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E4=B8=8D=E7=AD=89=E4=BA=8E=E6=A8=A1=E5=9D=97=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TPV 自举工程 includes 为合并形态,源码模块(lib.Stub.StubMerger 包)不提供裸 .vp 文件,声明由 ModuleSha1Map 定位 sha1 -> temp/*.stub.ll 加载(_LoadDeclarationsFromStubLlvm,与 FullModulePath 无关) - 原判据只看 FullModulePath 文件系统存在性 -> TPV 构建所有 import 误报 ImportError - 改为:文件系统找不到时查 _source_module_sig_files / Gen.ModuleSha1Map(TPV 工程由 Phase2Translator._ModuleSha1Map 同步而来),模块有 sha1 注册即视为可解析 - 单文件编译两表皆空,NegativeTest 假 import 用例仍 fail-fast --- lib/core/Handles/HandlesImports.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/core/Handles/HandlesImports.py b/lib/core/Handles/HandlesImports.py index 44b3a5b..e06256e 100644 --- a/lib/core/Handles/HandlesImports.py +++ b/lib/core/Handles/HandlesImports.py @@ -659,14 +659,29 @@ class ImportHandle(BaseHandle): break if FullModulePath: break - # fail-fast:模块在所有来源(includes/LibraryPaths/工程目录/递归 walk)都 - # 找不到时立即报错,暴露 import 路径错误(如目录重组后未更新的旧路径)。 - # 之前静默容忍导致引用方跨模块 mangled 前缀 fallback 当前模块 sha1, - # 链接期才暴露 undefined symbol(ClassGenGeneric 根因)。 + # fail-fast:模块在所有来源都"完全未知"时立即报错,暴露 import 路径错误 + # (如目录重组后未更新的旧路径)。之前静默容忍导致引用方跨模块 mangled + # 前缀 fallback 当前模块 sha1,链接期才暴露 undefined symbol。 + # 注意:文件系统找不到 ≠ 模块不存在——TPV 自举工程 includes 为合并形态, + # 源码模块(如 lib.Stub.StubMerger 包)不提供裸 .vp 文件,声明由 + # ModuleSha1Map/_source_module_sig_files 定位 sha1 → temp/*.stub.ll 加载, + # 属正常路径;只有模块在编译上下文完全未注册(无 sha1)才算真正不存在。 if not FullModulePath or not os.path.isfile(FullModulePath): - raise ImportError( - f"无法解析 import 模块 '{module_name}': " - f"在 includes/LibraryPaths/工程目录均未找到对应 .vp/.py/.vpi/.pyi 文件") + _known_sha1: str | None = None + _sig_files = getattr(self.Trans, '_source_module_sig_files', None) + if _sig_files: + for _key, _val in _sig_files.items(): + if _key == module_name or _key.endswith('.' + module_name): + _known_sha1 = os.path.basename(_val).replace('.vpi', '').replace('.pyi', '') + break + if not _known_sha1: + _sha_map: dict[str, str] = getattr(Gen, 'ModuleSha1Map', None) or {} + _known_sha1 = _sha_map.get(module_name) + if not _known_sha1: + raise ImportError( + f"无法解析 import 模块 '{module_name}': " + f"在 includes/LibraryPaths/工程目录均未找到对应 .vp/.py/.vpi/.pyi 文件," + f"且编译上下文无该模块的 sha1 注册(模块完全未知)") # 注册跨模块 __doc__ docstring(module_name 是被导入模块名) if FullModulePath: self._RegisterCrossModuleDocstrings(FullModulePath, Gen, module_name)