Skip to content

fix(agent/agentcontainers): ensure agent name env var is correct #18457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 19, 2025

Conversation

DanielleMaywood
Copy link
Contributor

Previously, CODER_WORKSPACE_AGENT_NAME would always be passed as the dev container name.

This is invalid for the following scenarios:

  • The dev container is specified in terraform
  • The dev container has a name customization

This change now runs ReadConfig twice. The first read is to extract a name (if present), from the devcontainer.json. The second read will then use the name we have stored for the dev container (so this could be either the customization, terraform resource name, or container name).

Previously, `CODER_WORKSPACE_AGENT_NAME` would always be passed as the
dev container name.

This is invalid for the following scenarios:
- The dev container is specified in terraform
- The dev container has a name customization

This change now runs `ReadConfig` twice. The first read is to extract a
name (if present), from the `devcontainer.json`. The second read will
then use the name we have stored for the dev container (so this could be
either the customization, terraform resource name, or container name).
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures the agent’s custom name from devcontainer.json is correctly applied to the CODER_WORKSPACE_AGENT_NAME environment variable by reading the config twice and updated tests to verify both reads.

  • Changed readConfigErrC channel capacity and added assertions for the first “empty” read.
  • Added a new subtest (CreateReadsConfigTwice) to cover custom-name override.
  • Updated api.go to run ReadConfig once to extract the name and again to pass the proper env var.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
agent/agentcontainers/api_test.go Increase readConfigErrC capacity; assert empty vs. populated envs on two reads; new custom-name subtest
agent/agentcontainers/api.go Call ReadConfig twice: first to pick up Coder.Name, second to inject it into the env
Comments suppressed due to low confidence (4)

agent/agentcontainers/api.go:1172

  • [nitpick] If subAgentConfig.Name is empty (no customization), this injects an empty CODER_WORKSPACE_AGENT_NAME; consider defaulting explicitly to the container’s name (dc.Name) to avoid passing a blank value.
				fmt.Sprintf("CODER_WORKSPACE_AGENT_NAME=%s", subAgentConfig.Name),

agent/agentcontainers/api_test.go:1896

  • There is no test covering the branch where the customization name is invalid; add a case that supplies an invalid Coder.Name and asserts that the warning path is triggered.
	t.Run("CreateReadsConfigTwice", func(t *testing.T) {

agent/agentcontainers/api_test.go:1264

  • [nitpick] The variable name fakeDCCLI differs from fDCCLI used in the new subtest; consider unifying the naming for clarity and consistency across tests.
			fakeDCCLI = &fakeDevcontainerCLI{

agent/agentcontainers/api.go:1163

  • The local variable logger is undefined here; use api.logger.Warn to correctly reference the API’s logger instance.
					logger.Warn(ctx, "invalid agent name in devcontainer customization, ignoring", slog.F("name", name))

@DanielleMaywood DanielleMaywood marked this pull request as ready for review June 19, 2025 13:58
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Minor nit about log message but otherwise all good 👍🏻

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Optimization looks good 💪🏻

@DanielleMaywood DanielleMaywood merged commit b49e62f into main Jun 19, 2025
34 checks passed
@DanielleMaywood DanielleMaywood deleted the dm-devcontainer-name-fix branch June 19, 2025 15:44
@github-actions github-actions bot locked and limited conversation to collaborators Jun 19, 2025
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.

2 participants