From 3362b5ae7e9e35e18524c897b4618c14a11a1a4f Mon Sep 17 00:00:00 2001 From: Ehab Younes Date: Wed, 22 Apr 2026 17:21:28 +0300 Subject: [PATCH] fix: infer workspace from env in `coder support bundle` (#24617) When running `coder support bundle` inside a workspace without arguments, the command now infers the workspace and agent from the `CODER_WORKSPACE_NAME`, `CODER_WORKSPACE_OWNER_NAME`, and `CODER_WORKSPACE_AGENT_NAME` environment variables set by the workspace agent. Previously, running without arguments inside a workspace produced an incomplete bundle with no workspace info, agent logs, or connection diagnostics, despite the environment having all the information needed to resolve the current workspace. Also updates the usage string from `` to `[]` to reflect that the argument has always been optional. Closes #24615 --- cli/support.go | 36 +++++++++++++++++- cli/support_test.go | 37 +++++++++++++++++++ .../coder_support_bundle_--help.golden | 7 ++-- docs/reference/cli/support_bundle.md | 4 +- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/cli/support.go b/cli/support.go index 07290a7e63..8501e0e8e9 100644 --- a/cli/support.go +++ b/cli/support.go @@ -71,9 +71,9 @@ func (r *RootCmd) supportBundle() *serpent.Command { var templateName string var pprof bool cmd := &serpent.Command{ - Use: "bundle []", + Use: "bundle [] []", Short: "Generate a support bundle to troubleshoot issues connecting to a workspace.", - Long: `This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You must specify a single workspace (and optionally an agent name).`, + Long: `This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You may specify a single workspace (and optionally an agent name). When run inside a workspace, the workspace and agent are inferred from the environment if not provided.`, Middleware: serpent.Chain( serpent.RequireRangeArgs(0, 2), ), @@ -149,6 +149,38 @@ func (r *RootCmd) supportBundle() *serpent.Command { templateID uuid.UUID ) + if len(inv.Args) == 0 { + // When running inside a workspace, infer the workspace + // and agent from environment variables set by the agent. + // Prefer CODER_WORKSPACE_ID for a direct UUID lookup; + // fall back to owner/name for older agents that do not + // set the ID variable. + if inv.Environ.Get("CODER") == "true" { + var wsArg string + if v := inv.Environ.Get("CODER_WORKSPACE_ID"); v != "" { + wsArg = v + } else { + wsOwner := inv.Environ.Get("CODER_WORKSPACE_OWNER_NAME") + wsName := inv.Environ.Get("CODER_WORKSPACE_NAME") + if wsOwner != "" && wsName != "" { + wsArg = wsOwner + "/" + wsName + } + } + agtName := inv.Environ.Get("CODER_WORKSPACE_AGENT_NAME") + if wsArg != "" { + cliLog.Info(inv.Context(), "detected workspace from environment", + slog.F("workspace_arg", wsArg), + slog.F("agent_name", agtName), + ) + cliui.Info(inv.Stderr, "Detected workspace from environment: "+wsArg) + inv.Args = append(inv.Args, wsArg) + if agtName != "" { + inv.Args = append(inv.Args, agtName) + } + } + } + } + if len(inv.Args) == 0 { cliLog.Warn(inv.Context(), "no workspace specified") cliui.Warn(inv.Stderr, "No workspace specified. This will result in incomplete information.") diff --git a/cli/support_test.go b/cli/support_test.go index 58a7929d53..3edada4bfa 100644 --- a/cli/support_test.go +++ b/cli/support_test.go @@ -118,6 +118,43 @@ func TestSupportBundle(t *testing.T) { assertBundleContents(t, path, false, false, []string{secretValue}) }) + t.Run("InferWorkspaceFromEnvByID", func(t *testing.T) { + t.Parallel() + + d := t.TempDir() + path := filepath.Join(d, "bundle.zip") + // No workspace arg, but set env vars as if inside a workspace. + inv, root := clitest.New(t, "support", "bundle", "--output-file", path, "--yes") + inv.Environ.Set("CODER", "true") + inv.Environ.Set("CODER_WORKSPACE_ID", workspaceWithoutAgent.Workspace.ID.String()) + inv.Environ.Set("CODER_WORKSPACE_AGENT_NAME", "dev") + //nolint: gocritic // requires owner privilege + clitest.SetupConfig(t, client, root) + err := inv.Run() + require.NoError(t, err) + // The workspace should be resolved, but there is no running agent. + assertBundleContents(t, path, true, false, []string{secretValue}) + }) + + t.Run("InferWorkspaceFromEnvByName", func(t *testing.T) { + t.Parallel() + + d := t.TempDir() + path := filepath.Join(d, "bundle.zip") + // No workspace arg and no CODER_WORKSPACE_ID; fall back to + // owner/name resolution for older agents. + inv, root := clitest.New(t, "support", "bundle", "--output-file", path, "--yes") + inv.Environ.Set("CODER", "true") + inv.Environ.Set("CODER_WORKSPACE_NAME", workspaceWithoutAgent.Workspace.Name) + inv.Environ.Set("CODER_WORKSPACE_OWNER_NAME", coderdtest.FirstUserParams.Username) + inv.Environ.Set("CODER_WORKSPACE_AGENT_NAME", "dev") + //nolint: gocritic // requires owner privilege + clitest.SetupConfig(t, client, root) + err := inv.Run() + require.NoError(t, err) + assertBundleContents(t, path, true, false, []string{secretValue}) + }) + t.Run("NoAgent", func(t *testing.T) { t.Parallel() d := t.TempDir() diff --git a/cli/testdata/coder_support_bundle_--help.golden b/cli/testdata/coder_support_bundle_--help.golden index ed0973aa42..0843a43f56 100644 --- a/cli/testdata/coder_support_bundle_--help.golden +++ b/cli/testdata/coder_support_bundle_--help.golden @@ -1,13 +1,14 @@ coder v0.0.0-devel USAGE: - coder support bundle [flags] [] + coder support bundle [flags] [] [] Generate a support bundle to troubleshoot issues connecting to a workspace. This command generates a file containing detailed troubleshooting information - about the Coder deployment and workspace connections. You must specify a - single workspace (and optionally an agent name). + about the Coder deployment and workspace connections. You may specify a single + workspace (and optionally an agent name). When run inside a workspace, the + workspace and agent are inferred from the environment if not provided. OPTIONS: -O, --output-file string, $CODER_SUPPORT_BUNDLE_OUTPUT_FILE diff --git a/docs/reference/cli/support_bundle.md b/docs/reference/cli/support_bundle.md index 40744f819b..9c58131892 100644 --- a/docs/reference/cli/support_bundle.md +++ b/docs/reference/cli/support_bundle.md @@ -6,13 +6,13 @@ Generate a support bundle to troubleshoot issues connecting to a workspace. ## Usage ```console -coder support bundle [flags] [] +coder support bundle [flags] [] [] ``` ## Description ```console -This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You must specify a single workspace (and optionally an agent name). +This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You may specify a single workspace (and optionally an agent name). When run inside a workspace, the workspace and agent are inferred from the environment if not provided. ``` ## Options