Skip to content

Commit bb3cb02

Browse files
fix: enhance Copilot CLI configuration to merge trusted directories with custom settings
1 parent 8883636 commit bb3cb02

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

registry/coder-labs/modules/copilot-cli/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ module "copilot_cli" {
8383
copilot_config = jsonencode({
8484
banner = "never"
8585
theme = "dark"
86-
trusted_folders = [
87-
"/home/coder/workspace",
88-
"/home/coder/project"
89-
]
9086
})
9187
9288
# MCP server configuration

registry/coder-labs/modules/copilot-cli/copilot-cli.tftest.hcl

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,60 @@ run "custom_copilot_config_overrides_default" {
159159
variables {
160160
agent_id = "test-agent"
161161
workdir = "/home/coder"
162+
copilot_config = jsonencode({
163+
banner = "always"
164+
theme = "dark"
165+
})
166+
}
167+
168+
assert {
169+
condition = var.copilot_config != ""
170+
error_message = "Custom copilot config should be set"
171+
}
172+
173+
assert {
174+
condition = jsondecode(local.final_copilot_config).banner == "always"
175+
error_message = "Custom banner setting should be applied"
176+
}
177+
178+
assert {
179+
condition = jsondecode(local.final_copilot_config).theme == "dark"
180+
error_message = "Custom theme setting should be applied"
181+
}
182+
}
183+
184+
run "trusted_directories_merged_with_custom_config" {
185+
command = plan
186+
187+
variables {
188+
agent_id = "test-agent"
189+
workdir = "/home/coder/project"
162190
copilot_config = jsonencode({
163191
banner = "always"
164192
theme = "dark"
165193
trusted_folders = ["/custom"]
166194
})
195+
trusted_directories = ["/workspace", "/data"]
167196
}
168197

169198
assert {
170-
condition = var.copilot_config != ""
171-
error_message = "Custom copilot config should be set"
199+
condition = contains(jsondecode(local.final_copilot_config).trusted_folders, "/custom")
200+
error_message = "Custom trusted folder should be included"
201+
}
202+
203+
assert {
204+
condition = contains(jsondecode(local.final_copilot_config).trusted_folders, "/home/coder/project")
205+
error_message = "Workdir should be included in trusted folders"
206+
}
207+
208+
assert {
209+
condition = contains(jsondecode(local.final_copilot_config).trusted_folders, "/workspace")
210+
error_message = "trusted_directories should be merged into config"
172211
}
173212

174213
assert {
175-
condition = local.final_copilot_config == var.copilot_config
176-
error_message = "Custom copilot config should override default"
214+
condition = contains(jsondecode(local.final_copilot_config).trusted_folders, "/data")
215+
error_message = "All trusted_directories should be merged into config"
177216
}
178217
}
179218

registry/coder-labs/modules/copilot-cli/main.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,22 @@ describe("copilot-cli", async () => {
115115
}),
116116
).resolves.toBeDefined();
117117
});
118+
119+
it("merges trusted_directories with custom copilot_config", async () => {
120+
const state = await runTerraformApply(import.meta.dir, {
121+
agent_id: "test-agent",
122+
workdir: "/home/coder/project",
123+
trusted_directories: JSON.stringify(["/workspace", "/data"]),
124+
copilot_config: JSON.stringify({
125+
banner: "always",
126+
theme: "dark",
127+
trusted_folders: ["/custom"],
128+
}),
129+
});
130+
131+
// Verify that the state was created successfully with the merged config
132+
// The actual merging logic is tested in the .tftest.hcl file
133+
expect(state).toBeDefined();
134+
expect(state.resources).toBeDefined();
135+
});
118136
});

registry/coder-labs/modules/copilot-cli/main.tf

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,24 @@ locals {
197197
start_script = file("${path.module}/scripts/start.sh")
198198
module_dir_name = ".copilot-module"
199199

200-
default_copilot_config = jsonencode({
201-
banner = "never"
202-
theme = "auto"
203-
trusted_folders = concat([local.workdir], var.trusted_directories)
204-
})
200+
all_trusted_folders = concat([local.workdir], var.trusted_directories)
205201

206-
final_copilot_config = var.copilot_config != "" ? var.copilot_config : local.default_copilot_config
202+
parsed_custom_config = try(jsondecode(var.copilot_config), {})
203+
204+
existing_trusted_folders = try(local.parsed_custom_config.trusted_folders, [])
205+
206+
merged_copilot_config = merge(
207+
{
208+
banner = "never"
209+
theme = "auto"
210+
},
211+
local.parsed_custom_config,
212+
{
213+
trusted_folders = concat(local.existing_trusted_folders, local.all_trusted_folders)
214+
}
215+
)
216+
217+
final_copilot_config = jsonencode(local.merged_copilot_config)
207218
}
208219

209220
resource "coder_env" "mcp_app_status_slug" {

0 commit comments

Comments
 (0)