Fix macOS pkg post-processing breaking the payload

pkgutil --expand-full flattens the component payload to loose files that
--flatten cannot repack, producing a pkg Installer accepts but installs
nothing from (also breaking the postinstall auto-open). Use nested
regular --expand: product for the Distribution domains edit, component
for the postinstall sudo-prefix removal.
This commit is contained in:
2026-09-23 03:03:53 +00:00
parent 039824f236
commit 3711d3dd3c
+17 -6
View File
@@ -426,11 +426,14 @@ def force_macos_user_install(pkg: Path) -> None:
Also drops the `sudo -u "$USER"` prefix from Velopack's postinstall
script: under a per-user install the script already runs as the
installing user, and sudo would fail for lack of a tty.
NB: only ever use `pkgutil --expand` (which keeps component Payloads
archived) — `--expand-full` flattens payloads to loose files that
`--flatten` cannot repack, producing a pkg that "installs" nothing.
"""
expanded = pkg.with_name(pkg.stem + "-expanded")
shutil.rmtree(expanded, ignore_errors=True)
# --expand-full also expands the component pkg, exposing its Scripts dir
subprocess.run(["pkgutil", "--expand-full", str(pkg), str(expanded)], check=True)
subprocess.run(["pkgutil", "--expand", str(pkg), str(expanded)], check=True)
dist_xml = expanded / "Distribution"
xml = dist_xml.read_text()
@@ -443,12 +446,20 @@ def force_macos_user_install(pkg: Path) -> None:
raise RuntimeError("Unexpected distribution.xml: <domains> not found")
dist_xml.write_text(new_xml)
postinstalls = list(expanded.glob("*.pkg/Scripts/postinstall"))
if len(postinstalls) != 1:
raise RuntimeError(f"Unexpected pkg layout: postinstalls={postinstalls}")
postinstall = postinstalls[0]
# Edit postinstall inside the (still archived) component pkg.
components = [p for p in expanded.glob("*.pkg") if p.is_file()]
if len(components) != 1:
raise RuntimeError(f"Unexpected pkg layout: components={components}")
component = components[0]
comp_dir = expanded / (component.stem + "-component")
subprocess.run(["pkgutil", "--expand", str(component), str(comp_dir)], check=True)
postinstall = comp_dir / "Scripts" / "postinstall"
script = postinstall.read_text()
if 'sudo -u "$USER" ' not in script:
raise RuntimeError("Unexpected postinstall script: sudo prefix not found")
postinstall.write_text(script.replace('sudo -u "$USER" ', ""))
subprocess.run(["pkgutil", "--flatten", str(comp_dir), str(component)], check=True)
shutil.rmtree(comp_dir)
subprocess.run(["pkgutil", "--flatten", str(expanded), str(pkg)], check=True)
shutil.rmtree(expanded)