Skip to content

feat: add merge_strategy attribute to coder_env resource#489

Merged
kacpersaw merged 1 commit intomainfrom
kacpersaw/add-merge-strategy
Mar 13, 2026
Merged

feat: add merge_strategy attribute to coder_env resource#489
kacpersaw merged 1 commit intomainfrom
kacpersaw/add-merge-strategy

Conversation

@kacpersaw
Copy link
Contributor

Summary

Add an optional merge_strategy attribute to the coder_env resource that controls how environment variables are combined when multiple coder_env resources define the same name.

Relates to coder/coder#21885

Valid values

Strategy Behavior
replace (default) Last value wins
append Appends to existing value with : separator
prepend Prepends to existing value with : separator
error Fail the build if another coder_env defines the same name

Example

resource "coder_env" "path_tools" {
  agent_id       = coder_agent.dev.id
  name           = "PATH"
  value          = "/home/coder/tools/bin"
  merge_strategy = "append"
}

Changes

  • provider/env.go — Added merge_strategy schema field with StringInSlice validation
  • provider/env_test.go — Tests for default value, all valid values, and invalid rejection
  • docs/resources/env.md — Updated with new attribute docs and PATH append example

Design notes

  • Default replace preserves backward compatibility
  • ForceNew: true — changing strategy recreates the resource
  • No CRUD changes needed; the provider just stores the value in state
  • Merge logic is enforced server-side by Coder's provisioner
  • Separator is always : (PATH-style); future enhancement could add custom separator

@kacpersaw kacpersaw force-pushed the kacpersaw/add-merge-strategy branch from 46c9e05 to df73de5 Compare March 12, 2026 12:18
@kacpersaw kacpersaw marked this pull request as ready for review March 12, 2026 13:52
Copy link
Member

@deansheather deansheather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation seems fine but I worry about how the order for prepending/appending is going to be determined if there are multiple

Add an optional merge_strategy attribute that controls how environment
variables are combined when multiple coder_env resources define the same
name. Valid values: replace (default), append, prepend, error.

- Default 'replace' preserves backward compatibility
- ForceNew: true ensures strategy changes recreate the resource
- Validated with StringInSlice matching existing codebase patterns
- Added tests for default value, all valid values, and invalid rejection
- Updated docs with merge_strategy description and PATH append example
@kacpersaw kacpersaw force-pushed the kacpersaw/add-merge-strategy branch from df73de5 to 979b8e0 Compare March 13, 2026 13:57
Copy link
Contributor Author

@kacpersaw kacpersaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The ordering for append/prepend is determined server-side in Coder's provisioner — resources are processed in alphabetical order by Terraform resource address (per the determinism fix in coder/coder). The provider itself just stores the merge_strategy value; it doesn't perform the merging.

I've added a note to the schema description to make this explicit: "When multiple resources append or prepend to the same name, they are applied in alphabetical order by Terraform resource address."

@kacpersaw kacpersaw requested a review from deansheather March 13, 2026 14:34
@kacpersaw kacpersaw merged commit dbadcf7 into main Mar 13, 2026
6 checks passed
@kacpersaw kacpersaw deleted the kacpersaw/add-merge-strategy branch March 13, 2026 14:51
@github-actions github-actions bot locked and limited conversation to collaborators Mar 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants