Repository navigation
fix: make Sprig first/last/rest/initial nil-safe - #651
Open
ogormans-deptstack wants to merge 2 commits into
Open
ogormans-deptstack wants to merge 2 commits into
ogormans-deptstack wants to merge 2 commits into
Conversation
These four Sprig list accessors call reflect.TypeOf(list).Kind() without a nil check, so referencing a missing observed field (such as a status that is not yet populated) panics and fails the whole reconcile. Wrap them so a nil input returns an empty result, deferring to Sprig's must* variants for valid inputs and unsupported-type errors. Fixes crossplane-contrib#78 Signed-off-by: ogormans-deptstack <sean.ogorman@deptagency.com>
ogormans-deptstack
requested review from
adamwg,
bobh66,
ezgidemirel,
phisco,
sergenyalcin,
turkenf and
ulucinar
as code owners
October 16, 2026 11:31
Extract the mustFirst/mustLast/mustRest/mustInitial assertions into comma-ok checks so the type assertion is the sole right-hand side of an assignment, satisfying golangci-lint's forcetypeassert. If sprig ever stops providing a must* variant the original accessor is left untouched. Signed-off-by: ogormans-deptstack <sean.ogorman@deptagency.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of your changes
Sprig's
first,last,rest, andinitialcallreflect.TypeOf(list).Kind()without guarding against an untyped
nil. When a template reaches for a field thatisn't populated yet — e.g. a status that a composed resource hasn't reported —
listis an untyped
nil,reflect.TypeOf(nil)isnil, and.Kind()panics with aninvalid memory address.
text/templatesurfaces that aserror calling first: ...and the whole reconcile fails.
This replaces those four accessors in
GetNewTemplateWithFunctionMapswith thinwrappers (
nilSafeList) that treat anilinput as an empty result and otherwisedefer to Sprig's
must*variants, which report unsupported types as errors insteadof panics . It follows the existing
env/expandenvremoval pattern already inthat function. Valid inputs behave exactly as before.
Credit to @shashankvarma499, who traced this root cause in the issue thread — the
approach here follows their write-up. Happy to hand this off to him if he'd rather carry it
himself, or if this issue is stale or considered not necessary to fix yet.
I ran into something similar writing compositions against Crossplane CRDs (I maintain
kubectl-schemagen, a lesser used krew plugin that scaffolds manifests for CRDs includingCrossplane's Composition / CompositeResourceDefinition / EnvironmentConfig), where
referencing a not-yet-populated status field is easy to do by accident.
-->
Fixes #78
I have: