Skip to content

fix(workspace): honor explicit --workspace-id before requiring current workspace context#2182

Open
jlaneve wants to merge 1 commit into
mainfrom
fix/coalesce-workspace-flag-ordering
Open

fix(workspace): honor explicit --workspace-id before requiring current workspace context#2182
jlaneve wants to merge 1 commit into
mainfrom
fix/coalesce-workspace-flag-ordering

Conversation

@jlaneve

@jlaneve jlaneve commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Problem

When authenticating non-interactively with an organization-scoped (workspace-operator) API token via ASTRO_API_TOKEN, commands like astro deployment variable update fail with:

Error: failed to find a valid workspace: failed to get current Workspace: current workspace context not set,
you can switch to a workspace with
	astro workspace switch WORKSPACEID

Passing --workspace-id <id> does not prevent the error — the flag is silently ignored on this path.

Root cause

coalesceWorkspace() called workspace.GetCurrentWorkspace() first and returned its error before ever consulting the --workspace-id flag:

wsFlag := workspaceID
wsCfg, err := workspace.GetCurrentWorkspace()   // called first
if err != nil {
    return "", errors.Wrap(err, "failed to get current Workspace")  // returns here — flag never consulted
}
if wsFlag != "" { return wsFlag, nil }           // flag only honored after

An org-scoped token has no workspaceId claim, so in a fresh CI home checkAPIToken leaves c.Workspace empty. GetCurrentWorkspace() then errors, and because the flag is checked after, --workspace-id cannot rescue it. This is deterministic given an empty current-workspace context (it only looks intermittent because a prior astro workspace switch may have persisted a workspace).

Fix

Return an explicit --workspace-id before calling GetCurrentWorkspace(), making the flag authoritative in non-interactive flows. This covers every command routed through coalesceWorkspace (deployment variable update, deployment objects, worker queues, deploy, dbt, env, inspect) — not just variable update.

Tests

Adds TestCoalesceWorkspace covering:

  • explicit flag with no current context (the regression — fails before this change)
  • explicit flag precedence over an existing context
  • fallback to the current workspace context when no flag is provided
  • error when neither source is available

Workaround (pre-fix)

astro workspace switch <id> first persists c.Workspace, after which the command succeeds. Confirmed still valid on affected versions.

🤖 Generated with Claude Code

…t workspace context

coalesceWorkspace() called workspace.GetCurrentWorkspace() first and
returned its error before ever consulting the --workspace-id flag. With
an org-scoped (workspace-operator) API token in a fresh CI home, the
current-workspace context is empty (the token has no workspaceId claim),
so the command failed with "current workspace context not set" even when
--workspace-id was explicitly supplied.

Return an explicit --workspace-id before calling GetCurrentWorkspace so
the flag is authoritative in non-interactive flows. This covers every
command routed through coalesceWorkspace (deployment variable update,
deployment objects, worker queues, deploy, dbt, env, inspect).

Adds TestCoalesceWorkspace covering: explicit flag with no current
context (the regression), flag precedence over context, context
fallback, and the no-source error.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jlaneve jlaneve requested a review from a team as a code owner June 18, 2026 18:38
@coveralls-official

Copy link
Copy Markdown

Coverage Report for CI Build 27781374546

Coverage increased (+0.007%) to 45.098%

Details

  • Coverage increased (+0.007%) from the base build.
  • Patch coverage: 7 of 7 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 56373
Covered Lines: 25423
Line Coverage: 45.1%
Coverage Strength: 8.0 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant