fix(provisioner): make coder_env and coder_script iteration deterministic#22706
Draft
fix(provisioner): make coder_env and coder_script iteration deterministic#22706
Conversation
…stic When multiple coder_env resources define the same key for a single agent, the final environment variable value was non-deterministic because Go maps have random iteration order. The ConvertState function iterated over tfResourcesByLabel (a map) to associate coder_env resources with agents, making the order of ExtraEnvs unpredictable across builds. This change introduces sortedResourcesByType(), which collects resources of a given type from the label map and sorts them by Terraform address before processing. This ensures that when duplicate keys exist, the last one by sorted address deterministically wins. The same fix is applied to coder_script resources for consistency. Fixes #21885
7fe9aaf to
c9282de
Compare
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
Fixes #21885
When multiple
coder_envresources define the same key for a single agent, the final environment variable value was non-deterministic because Go maps have random iteration order. TheConvertStatefunction iterated overtfResourcesByLabel(a map) to associatecoder_envresources with agents, making the order ofExtraEnvsunpredictable across builds.Changes
sortedResourcesByType()helper inresources.gothat collects resources of a given type from the label map and sorts them by Terraform address before processingcoder_envandcoder_scriptassociation with sorted iteration, ensuring deterministic orderingduplicate-env-keystest case and fixture verifying that when twocoder_envresources define the same key, the result is deterministic (sorted by address)How it works
When duplicate keys exist, the last one by sorted Terraform address wins. For example,
coder_env.path_ais processed beforecoder_env.path_b, sopath_b's value will be the final one inExtraEnvs. Sinceprovisionerdserver.gomergesExtraEnvsinto a map (last wins), this produces stable, predictable results.