Skip to content

Fix versionLT misordering a deeper primary-version depth mismatch - #2546

Open
adithya-sn wants to merge 2 commits into
jenkinsci:masterfrom
adithya-sn:fix/versionlt-primary-depth-mismatch
Open

adithya-sn wants to merge 2 commits into
jenkinsci:masterfrom
adithya-sn:fix/versionlt-primary-depth-mismatch

Conversation

@adithya-sn

@adithya-sn adithya-sn commented Nov 23, 2026 •

Copy link
Copy Markdown

Related

#2512 reports and fixes this same underlying bug (same apache-httpcomponents-client-5-api 5.6-191 vs 5.6.x-NNN case), independently, with a narrower patch: it special-cases "both sides have a - and their release parts (before the first -) differ" and compares those release parts only, leaving the rest of the existing flattening/sort logic untouched for every other case.

This PR takes a broader approach: it generalizes to a single primary/qualifier split that handles both the with-dash and without-dash shapes uniformly (so it also subsumes #2328's special case, which is removed rather than kept alongside the new logic), rather than adding another special case on top. Flagging the overlap here so maintainers can pick whichever fix they'd rather carry forward -- happy to close this in favor of #2512, or the reverse, whatever's preferred.

Summary

versionLT (and its PowerShell equivalent, Compare-VersionLessThan) decide whether copy_reference_file() should upgrade a bundled plugin that's already present in JENKINS_HOME. They work by flattening both version strings into one dot-separated sequence (converting - to .) and comparing position by position.

That flattening silently misaligns two version strings that have a different number of real dotted version components before their qualifier/build-number, sliding the shorter one's build number into the same comparison slot as the longer one's extra real version component.

Real-world case that surfaced this

apache-httpcomponents-client-5-api's installed version 5.6-191.vb_47e2b_41c698 vs a newer image's bundled 5.6.5-209.vd1010d57ee17. Flattened, that's comparing the installed version's build number 191 directly against the image version's real third component 5 — 191 > 5, so versionLT decided the newer image version was actually older, and copy_reference_file() skipped the upgrade.

That left a stale, incompatible apache-httpcomponents-client-5-api installed after a real Jenkins core upgrade, which then failed cloudbees-bitbucket-branch-source's own minimum-version dependency check at boot (Update required: Apache HttpComponents Client 5.x API Plugin ... to be updated to 5.6.4-204.vfa_29df89ffcf or higher), cascading into Blue Ocean's Bitbucket integration failing to load, and the controller going into a boot-failure restart loop.

Why the existing #2328 fix doesn't catch this

#2328 already special-cased one specific depth of this same shape mismatch (a 2-part vs. 3-part version split right after the first segment, recognized via a v-prefix marker comparison — e.g. 3894.vd0f0248b_a_fc4 vs 3894.3896.vca_2c931e7935). It doesn't generalize: it only ever inspects the segment immediately after the first one, so it doesn't catch a mismatch one level deeper, like this case (5.6 vs 5.6.5, where the first two segments already match).

Fix

Replaces the v-prefix special case with a more general fix: split each version into its real, comparable "primary" dotted version and an optional trailing incremental-build qualifier, stopping before the opaque .v<hash> content-hash marker and before any - that separates a build number from the primary version. Compare the primaries first, with zero-padding so a depth difference (e.g. 5.6 vs 5.6.5) compares correctly instead of misaligning; only fall back to comparing qualifiers when the primaries are equal.

This subsumes the #2328 special case — verified all of its existing bats/Pester test cases, both forward and reverse direction, plus the pre-existing 1.0/1.1 and major-version 9 vs 10 cases, still pass — so it's removed rather than kept alongside the new logic.

Applied the same fix to both the bash (jenkins-support) and PowerShell (jenkins-support.psm1) implementations, to keep Linux/Windows image parity.

Test plan

🤖 Generated with Claude Code

@adithya-sn
adithya-sn requested a review from a team as a code owner November 23, 2026 16:16
@adithya-sn

Copy link
Copy Markdown
Author

Pushed a follow-up commit after a closer sanity pass on my own change (this being a core entrypoint script, wanted to double check before leaving it for review):

  • Ran shellcheck with the exact invocation this repo's CI uses (-e SC1091 jenkins-support *.sh tests/test_helpers.bash tools/hadolint tools/shellcheck .ci/publish.sh) — clean, no warnings.
  • Re-verified all 16 cases (the original 3 trivial cases + 4 #1456 cases + 4 #2328 forward/reverse cases + major-version 9-vs-10 + both directions of the new case) still pass against the actual edited file.
  • Found and fixed a real robustness regression: the first version of this fix used bare bash arithmetic ((( 10#$sa < 10#$sb ))) and a bare PowerShell [int64] cast for segment/qualifier comparisons. Every documented real-world case here is numeric, but neither is guaranteed that structurally, and both would error (bash) or throw (PowerShell) on a non-numeric segment — confirmed live with versionLT "1.abc" "1.def", which crashed with "value too great for base" before the fix. Since jenkins-support runs under set -e, that could abort the entrypoint outright on an unusual plugin version. Fixed to fall back to a plain string comparison when a segment isn't a plain integer, same safety property the code being replaced already had (sort --version-sort never fails, and the PowerShell side already had this fallback for primary-version segments via TryParse — just not for the new qualifier comparison, which is also fixed now).

Wasn't able to get a local PowerShell/Pester run going in my environment (a brew install powershell pulled in a large cmake/dotnet dependency chain and started erroring partway through, so I backed out rather than push through it) — traced the .psm1 port by hand against the same cases instead. Happy to adjust anything CI surfaces there.

versionLT (and its PowerShell equivalent, Compare-VersionLessThan) decide
whether copy_reference_file() should upgrade a bundled plugin already
present in JENKINS_HOME. They work by flattening both version strings
into one dot-separated sequence (converting '-' to '.') and comparing
position by position.

That flattening silently misaligns two version strings that have a
different number of real dotted version components before their
qualifier/build-number, sliding the shorter one's build number into the
same comparison slot as the longer one's extra real version component.
For apache-httpcomponents-client-5-api's "5.6-191.vb_47e2b_41c698"
(installed) vs "5.6.5-209.vd1010d57ee17" (bundled in a newer image):
flattened, that's comparing installed's build number "191" directly
against image's real third component "5" -- 191 > 5, so the function
decided the newer image version was actually older, and
copy_reference_file() skipped the upgrade. That left a stale,
incompatible apache-httpcomponents-client-5-api installed after a real
Jenkins core upgrade, which then failed cloudbees-bitbucket-branch-source's
own minimum-version dependency check at boot and cascaded into Blue
Ocean's Bitbucket integration failing to load.

jenkinsci#2328 already special-cased one specific depth of this same shape
mismatch (a 2-part vs. 3-part version split right after the first
segment), recognized via a "v"-prefix marker comparison. It doesn't
generalize to a mismatch one level deeper, like this one, since it only
ever inspects the segment immediately after the first.

This replaces that special case with a more general fix: split each
version into its real, comparable "primary" dotted version and an
optional trailing incremental-build qualifier, stopping before the
opaque ".v<hash>" content-hash marker and before any '-' that separates
a build number from the primary version. Compare the primaries first,
with zero-padding so a depth difference (e.g. "5.6" vs "5.6.5") compares
correctly instead of misaligning; only fall back to comparing qualifiers
when the primaries are equal. This subsumes the jenkinsci#2328 special case
(verified: all of its existing bats/Pester test cases, both forward and
reverse direction, plus the pre-existing 1.0/1.1 and major-version-9-vs-10
cases, still pass), so it's removed rather than kept alongside the new
logic.

Added both directions of the apache-httpcomponents-client-5-api case as
new regression tests in tests/functions.bats and
tests/functions.Tests.ps1, alongside the existing jenkinsci#1456/jenkinsci#2328 cases.
The previous commit's segment/qualifier comparisons used bare bash
arithmetic (\`(( 10#\$sa < 10#\$sb ))\`) and a bare PowerShell \`[int64]\`
cast. Real-world Jenkins plugin versions are numeric in every documented
test case, but neither implementation is guaranteed that by anything
structural, and both would error out (bash) or throw (PowerShell) on a
non-numeric segment -- a real regression versus the code being replaced,
which only ever used \`sort --version-sort\` (string-based, never fails)
and an explicit TryParse-with-string-fallback pattern respectively.

Confirmed live: \`versionLT "1.abc" "1.def"\` crashed with
"value too great for base" before this change. Since jenkins-support
runs under \`set -e\`, this could have aborted the entrypoint outright on
an unusual plugin version instead of just comparing it as a string, same
as before this whole fix.

Added a small segment-level helper (bash: _versionLT_compare_segment;
PowerShell: inline TryParse/fallback, mirroring
Compare-PluginVersionPrimary's existing pattern) that only does numeric
comparison when both sides parse as plain integers, falling back to a
plain string comparison otherwise -- restoring the "never fails"
property of the code being replaced.
@adithya-sn
adithya-sn force-pushed the fix/versionlt-primary-depth-mismatch branch from a0221a2 to 4f82e2f Compare November 23, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant