fix(coderd/templatebuilder): fix archive bundling for nested static files and counted agents (#26901)

Fixes two template builder bugs that caused AWS EC2 (Linux) template
imports to fail:

1. **Missing directory entries in tar archive**: `BundleTar` wrote
static files with nested paths (e.g.
`cloud-init/cloud-config.yaml.tftpl`) without emitting `TypeDir` entries
for parent directories. The provisioner's archive extractor requires
explicit directory entries and failed with "no such file or directory".

2. **Incorrect agent reference for counted resources**:
`ExtractAgentResourceName` returned `dev` for the AWS Linux base
template, but the agent uses `count =
data.coder_workspace.me.start_count`, so module templates need
`coder_agent.dev[0].id`. The function now detects `count`/`for_each` and
appends `[0]`.

> [!NOTE]
> Generated by Coder Agents (on behalf of @jeremyruppel)
This commit is contained in:
Jeremy Ruppel
2026-06-30 19:37:30 -04:00
committed by GitHub
parent 15d4eb9db8
commit 3d966d48b5
4 changed files with 96 additions and 7 deletions
+22 -1
View File
@@ -215,7 +215,28 @@ func TestExtractAgentResourceName(t *testing.T) {
name, err := templatebuilder.ExtractAgentResourceName(rendered)
require.NoError(t, err)
require.Equal(t, "dev", name)
require.Equal(t, "dev[0]", name, "counted agent should include index")
})
t.Run("CountedAgent", func(t *testing.T) {
t.Parallel()
hcl := []byte(`resource "coder_agent" "myagent" {
count = data.coder_workspace.me.start_count
arch = "amd64"
}`)
name, err := templatebuilder.ExtractAgentResourceName(hcl)
require.NoError(t, err)
require.Equal(t, "myagent[0]", name)
})
t.Run("UncountedAgent", func(t *testing.T) {
t.Parallel()
hcl := []byte(`resource "coder_agent" "main" {
arch = "amd64"
}`)
name, err := templatebuilder.ExtractAgentResourceName(hcl)
require.NoError(t, err)
require.Equal(t, "main", name)
})
t.Run("NoAgent", func(t *testing.T) {