fix: fall back to name lookup for UUID-shaped workspace names (#24340)

`namedWorkspace` in `cli/root.go` parsed workspace identifiers with
`uuid.Parse` first and returned immediately on success, even when no
workspace had that UUID as its actual ID. This caused 404 errors for any
workspace whose name was a valid 32-char hex string (dashless UUID).

- Add `codersdk.ResolveWorkspace`: tries UUID lookup first, falls back
to name lookup on 404. `NameValid` guard skips the fallback for standard
dashed UUIDs (36 chars > 32-char name limit).
- Export `codersdk.SplitWorkspaceIdentifier`, replacing the duplicate
`splitNamedWorkspace` in `cli/root.go` (uses `strings.Cut`).
- Delete `namedWorkspace` from `cli/root.go`; all 28 call sites now use
`client.ResolveWorkspace` directly.
- Delete `namedWorkspace` and `splitNameAndOwner` from
`codersdk/toolsdk/bash.go`; inline `client.ResolveWorkspace`.
- Simplify `GetWorkspace` tool handler to a single `ResolveWorkspace`
call.
- Unit tests via httptest mock cover UUID, name, owner/name, UUID-like
fallback, not-found, server error, transport error, and invalid
identifier paths.
- Integration tests in `cli/show_test.go` and `codersdk/toolsdk` for
workspaces with UUID-like names.

> Generated with Coder Agents
This commit is contained in:
Cian Johnston
2026-04-27 12:58:26 +01:00
committed by GitHub
parent 23b30b7285
commit d5a5be116d
25 changed files with 498 additions and 102 deletions
+2 -2
View File
@@ -984,7 +984,7 @@ func GetWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
err error
)
workspace, err = namedWorkspace(ctx, client, workspaceParts[0])
workspace, err = client.ResolveWorkspace(ctx, workspaceParts[0])
if err != nil {
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, nil, err
}
@@ -1044,7 +1044,7 @@ func GetWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
}
// Refresh workspace state so that `outdated`, `build`,`template_*` fields are up-to-date.
workspace, err = namedWorkspace(ctx, client, workspaceParts[0])
workspace, err = client.ResolveWorkspace(ctx, workspaceParts[0])
if err != nil {
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, nil, err
}