From 302d3fe791047b2a0c85e47ac492a4e0ef939d45 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sun, 8 Nov 2026 12:34:31 +0100 Subject: [PATCH] lib.distros.debian.Distro._install(): Protect installed pkgs apt 3.x distinguishes versions with the same version number by their control metadata, so a .deb installed with dpkg -i that differs from the repository's same-version package is regarded as a separate version, and apt-get install of the package name "upgrades" it to the repository version, replacing the installed package. If the installed package was built from the source code repo's master, this is effectively a downgrade and can break a CI run in currently unintended ways. This commit detects same-version packages in apt-get's install todo list by running apt-get -s install (simulate installation), parsing the output, and removing those packages from the command line of the subsequent, real install command. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0 Signed-off-by: Jan Lindemann --- .../jw/pkg/lib/distros/debian/Distro.py | 55 +++++++++++++++- .../python/jw/pkg/lib/distros/debian/test.py | 63 ++++++++++++++++++- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/src/python/jw/pkg/lib/distros/debian/Distro.py b/src/python/jw/pkg/lib/distros/debian/Distro.py index ac719267..b6f472d0 100644 --- a/src/python/jw/pkg/lib/distros/debian/Distro.py +++ b/src/python/jw/pkg/lib/distros/debian/Distro.py @@ -42,6 +42,55 @@ class Distro(Base): kwargs.setdefault('ec', self.ctx) return await run_dpkg(*args, **kwargs) + # apt 3.x distinguishes versions with the same version number by + # their control metadata, so a package installed outside of apt + # that differs from the repository's same-version package is + # regarded as a separate version, and apt-get + # would replace it with the repository version, which breaks CI. + # This method identifies such names with agt_get -s install + # (simulate) and drops them from the list, keeping the installed + # version. + async def __mask_installed(self, names: list[str], only_update: bool) -> list[str]: + args = ['-s', 'install'] + if only_update: + args.append('--only-upgrade') + args.append('--no-install-recommends') + args.extend(names) + try: + result = await self.apt_get(args, verbose = False, sudo = False) + except Exception: + return names + if result.status != 0: + return names + replaced: dict[str, str] = {} + for line in result.stdout_str.splitlines(): + parts = line.split() + if len(parts) < 3 or parts[0] != 'Inst': + continue + name = parts[1].split(':', 1)[0] + installed: str | None = None + target: str | None = None + for part in parts[2:]: + if part.startswith('[') and part.endswith(']'): + installed = part[1:-1] + elif part.startswith('('): + target = part[1:] + break + if installed is not None and installed == target: + replaced[name] = installed + ret: list[str] = [] + for name in names: + if installed := replaced.get(name): + log( + NOTICE, + f'keeping installed {name} {installed}: apt would ' + 'replace it with the repository package of the same ' + 'version', + ) + continue + ret.append(name) + return ret + def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) @@ -80,11 +129,15 @@ class Distro(Base): @override async def _install(self, names: Iterable[str], only_update: bool) -> None: + names = list(names) + keep = await self.__mask_installed(names, only_update) + if not keep: + return args = ['install'] if only_update: args.append('--only-upgrade') args.append('--no-install-recommends') - args.extend(names) + args.extend(keep) await self.apt_get(args) @override diff --git a/test/unit/python/jw/pkg/lib/distros/debian/test.py b/test/unit/python/jw/pkg/lib/distros/debian/test.py index f5370e8a..d7df3940 100644 --- a/test/unit/python/jw/pkg/lib/distros/debian/test.py +++ b/test/unit/python/jw/pkg/lib/distros/debian/test.py @@ -17,11 +17,13 @@ class DispatchLocal(Local): files: dict[str, str], installed: dict[str, str], simulate: str, + sim_status: int = 0, ) -> None: super().__init__('local') self.files = files self.installed = installed self.simulate = simulate + self.sim_status = sim_status self.cmds: list[list[str]] = [] @override @@ -45,7 +47,7 @@ class DispatchLocal(Local): status = 0 if name in self.installed else 1, ) if '/usr/bin/apt-get' in cmd and '-s' in cmd: - return Result(stdout = self.simulate.encode(), status = 0) + return Result(stdout = self.simulate.encode(), status = self.sim_status) return Result(stdout = b'', status = 0) def dpkg_args(cmd: list[str]) -> list[str]: @@ -116,7 +118,7 @@ d = DebianDistro(cl, id = 'debian') asyncio.run( d._install_local_files(['/a/jw-base-run-1.0.0-42.deb'], False, install_deps = True) ) -pm_cmds = [cmd for cmd in cl.cmds if '/usr/bin/apt-get' in cmd] +pm_cmds = [cmd for cmd in cl.cmds if '/usr/bin/apt-get' in cmd and '-s' not in cmd] assert len(pm_cmds) == 1 args = pm_cmds[0][pm_cmds[0].index('/usr/bin/apt-get'):] assert 'install' in args @@ -124,4 +126,61 @@ assert '--no-install-recommends' in args assert args[-1] == '/a/jw-base-run-1.0.0-42.deb' assert not [cmd for cmd in cl.cmds if '-i' in cmd] +# -- a name apt would replace with the repository's same-version +# build (a version installed outside of apt) is dropped from the +# install list, the remaining names are passed to apt-get +cl = DispatchLocal( + files = {}, + installed = {}, + simulate = ( + 'The following packages will be upgraded:\n' + 'Inst jw-pkg-run [1.0.0-287] (1.0.0-287 rolling [amd64])\n' + 'Inst jw-python-run (1.0.0-61 rolling [amd64])\n' + ), +) +d = DebianDistro(cl, id = 'debian') +asyncio.run(d._install(['jw-pkg-run', 'jw-python-run'], False)) +pm_cmds = [cmd for cmd in cl.cmds if '/usr/bin/apt-get' in cmd and '-s' not in cmd] +assert len(pm_cmds) == 1 +args = pm_cmds[0][pm_cmds[0].index('/usr/bin/apt-get'):] +assert 'jw-pkg-run' not in args +assert 'jw-python-run' in args + +# -- a real upgrade (different target version) is not dropped +cl = DispatchLocal( + files = {}, + installed = {}, + simulate = 'Inst jw-pkg-run [1.0.0-286] (1.0.0-287 rolling [amd64])\n', +) +d = DebianDistro(cl, id = 'debian') +asyncio.run(d._install(['jw-pkg-run'], False)) +pm_cmds = [cmd for cmd in cl.cmds if '/usr/bin/apt-get' in cmd and '-s' not in cmd] +assert len(pm_cmds) == 1 +args = pm_cmds[0][pm_cmds[0].index('/usr/bin/apt-get'):] +assert 'jw-pkg-run' in args + +# -- an arch-qualified Inst line matches the unqualified name +cl = DispatchLocal( + files = {}, + installed = {}, + simulate = 'Inst jw-pkg-run:amd64 [1.0.0-287] (1.0.0-287 rolling [amd64])\n', +) +d = DebianDistro(cl, id = 'debian') +asyncio.run(d._install(['jw-pkg-run'], False)) +assert not [cmd for cmd in cl.cmds if '/usr/bin/apt-get' in cmd and '-s' not in cmd] + +# -- a failed simulation passes all names through +cl = DispatchLocal( + files = {}, + installed = {}, + simulate = 'E: Unable to locate package jw-pkg-run', + sim_status = 100, +) +d = DebianDistro(cl, id = 'debian') +asyncio.run(d._install(['jw-pkg-run'], False)) +pm_cmds = [cmd for cmd in cl.cmds if '/usr/bin/apt-get' in cmd and '-s' not in cmd] +assert len(pm_cmds) == 1 +args = pm_cmds[0][pm_cmds[0].index('/usr/bin/apt-get'):] +assert 'jw-pkg-run' in args + print('All debian distro tests passed') -- 2.56.1