Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions MetLib/packaging/nuitka_patches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ python make_package.py --backend nuitka --onefile --apply-zip --build-report

补丁模块是打包内部实现,不再提供修改已安装 Nuitka 的独立命令。

## 运行时诊断

补丁默认静默,正式环境和商店版都不会向 stderr 写任何内容:启动器(如
MeteorMasterAI)会把子进程的任何 stderr 当作警告展示,无条件打印会导致每次
启动都出现一条误报。排查时临时打开开关即可:

```powershell
$env:METDETPY_ONEFILE_VERBOSE = "1"
```

打开后引导层会在启动子进程前向 stderr 打印一行

```
[MetDetPy onefile] METDETPY_MSIX_ONEFILE_PATCH_ACTIVE fork target: logical="..." final="..."
```

非商店环境下 `logical` 与 `final` 相同(`%LOCALAPPDATA%\MetDetPy\<BUILD_TAG>`);
商店环境下 `final` 指向 `%LOCALAPPDATA%\Packages\<PFN>\LocalCache\Local\MetDetPy\<BUILD_TAG>`,
可据此确认路径虚拟化确实发生。诊断只写 stderr,stdout 始终只保留 JSON 行协议。

## 兼容性检查

- `SUPPORTED_NUITKA_VERSIONS` 是版本白名单,初始保留历史构建版本 `4.0.5`。
Expand Down
33 changes: 27 additions & 6 deletions MetLib/packaging/nuitka_patches/patch_onefile_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
# The packaging backend imports this constant for artifact verification.
BINARY_MARKER = SOURCE_MARKER + "_ACTIVE"

# 运行时诊断开关:设置该环境变量(值为 1/yes/true 等)才打印 fork 目标路径。
# Fork diagnostics are opt-in. They used to print on every launch, and launchers
# that surface any child stderr as a warning showed a bogus warning every run.
VERBOSE_ENV_VAR = "METDETPY_ONEFILE_VERBOSE"

# --- 锚点:命中不上就失败,绝不猜测 -------------------------------------------------

# 锚点 1:Windows 专用区段内的子进程命令行构造函数。
Expand Down Expand Up @@ -128,12 +133,26 @@
// METDETPY_MSIX_ONEFILE_PATCH end"""

CREATE_CODE = r""" // METDETPY_MSIX_ONEFILE_PATCH begin
// Diagnostics go to stderr and are flushed right away: stdout carries the
// JSON line protocol with the detection results and must stay clean.
fprintf(stderr,
"[MetDetPy onefile] METDETPY_MSIX_ONEFILE_PATCH_ACTIVE fork target: logical=\"%ls\" final=\"%ls\"\n",
(wchar_t const *)fork_binary, (wchar_t const *)fork_binary_final);
fflush(stderr);
// Fork diagnostics are opt-in. They used to be printed on every launch,
// which made launchers that treat any child stderr as a warning show a
// bogus warning for every run. Set METDETPY_ONEFILE_VERBOSE=1 (or any
// value starting with 1/y/t) to get the line back when troubleshooting.
// stdout carries the JSON line protocol with the detection results and
// stays clean either way.
{
wchar_t verbose_value[8] = {0};
DWORD const verbose_length =
GetEnvironmentVariableW(L"METDETPY_ONEFILE_VERBOSE", verbose_value, 8);
if (verbose_length > 0 && verbose_length < 8) {
wchar_t const first = verbose_value[0];
if (first == L'1' || first == L'y' || first == L'Y' || first == L't' || first == L'T') {
fprintf(stderr,
"[MetDetPy onefile] METDETPY_MSIX_ONEFILE_PATCH_ACTIVE fork target: logical=\"%ls\" final=\"%ls\"\n",
(wchar_t const *)fork_binary, (wchar_t const *)fork_binary_final);
fflush(stderr);
}
}
}

bool_res = CreateProcessW(fork_binary_final,
getCommandLineForChildProcess(), // command line
Expand Down Expand Up @@ -223,6 +242,8 @@ def patch_bootstrap(path: Path, version: str) -> None:

if BINARY_MARKER not in CREATE_CODE:
fail("CREATE_CODE 里缺少 BINARY_MARKER,构建后的二进制校验会失效。")
if VERBOSE_ENV_VAR not in CREATE_CODE:
fail("CREATE_CODE 里缺少 VERBOSE_ENV_VAR,运行时诊断开关会失效。")

with open(path, "r", encoding="utf-8", newline="") as handle:
text = handle.read()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_nuitka_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def test_versions_keep_anchor_checks(tmp_path, capsys, version, newline):
assert b'\n' not in source.read_bytes().replace(b'\r\n', b'')


def test_fork_diagnostics_are_opt_in():
"""Production builds stay silent; the fork log needs METDETPY_ONEFILE_VERBOSE."""
assert patch.VERBOSE_ENV_VAR in patch.CREATE_CODE
# The marker must stay in the artifact for the post-build verification.
assert patch.BINARY_MARKER in patch.CREATE_CODE
# The line is behind a runtime environment check, never printed unconditionally.
assert patch.CREATE_CODE.index('GetEnvironmentVariableW') < patch.CREATE_CODE.index('fprintf')


@pytest.mark.parametrize('anchor', [patch.ANCHOR_HELPER, patch.ANCHOR_FORK, patch.ANCHOR_CREATE])
@pytest.mark.parametrize('count', [0, 2])
def test_bad_anchor_never_changes_source(tmp_path, anchor, count):
Expand Down
Loading