Skip to content

--workspace-id ignored on deployment commands: coalesceWorkspace() checks current-workspace context before reading the flag #2181

Description

@jremigio-circle

Summary

With an organization-scoped API token (no workspaceId claim) used via ASTRO_API_TOKEN in a non-interactive/CI context, astro deployment variable update (and other commands routing through coalesceWorkspace) fail with:

Error: failed to find a valid workspace: failed to get current Workspace: current workspace context not set

EVEN WHEN --workspace-id <id> is passed. The explicit flag is effectively ignored.

Affected versions

v1.40.1, v1.42.1 (latest stable), and v1.43.0-nightly — the relevant functions are byte-identical across these, so upgrading does not help.

Root cause (code)

In cmd/cloud/workspace.go, coalesceWorkspace() calls workspace.GetCurrentWorkspace() and returns its error BEFORE it checks the --workspace-id flag:

func coalesceWorkspace() (string, error) {
    wsFlag := workspaceID
    wsCfg, err := workspace.GetCurrentWorkspace()   // called first
    if err != nil {
        return "", errors.Wrap(err, "failed to get current Workspace")  // returns here — flag never read
    }
    if wsFlag != "" { return wsFlag, nil }           // --workspace-id only honored after
    if wsCfg != "" { return wsCfg, nil }
    return "", errors.New("no valid Workspace source found")
}

GetCurrentWorkspace() (cloud/workspace/workspace.go) errors whenever c.Workspace == "". In the API-token path, checkAPIToken() (cmd/cloud/setup.go) derives the workspace from token claims, falling back to c.Workspace (if wsID == "" { wsID = c.Workspace }); an org-scoped token has no workspaceId claim, so in a fresh config home c.Workspace stays empty and the flag cannot rescue it.

Reproduction

  1. ASTRO_HOME=$(mktemp -d), set ASTRO_API_TOKEN to an organization-scoped token, no astro workspace switch performed.
  2. Run astro deployment variable update --workspace-id <valid-ws-id> --deployment-id <valid-dep-id> --key FOO --value bar.
  3. Observe the current workspace context not set error despite --workspace-id.
  4. Running astro workspace switch <ws-id> first makes it succeed.

Expected

An explicit --workspace-id should be honored before requiring a current-workspace context.

Suggested fix

In coalesceWorkspace(), return wsFlag when non-empty BEFORE calling GetCurrentWorkspace() (or have checkAPIToken seed the context workspace from --workspace-id for org-scoped tokens).

Related

This issue is specifically about the coalesceWorkspace ordering for deployment/variable commands.

Impact

Breaks non-interactive CI flows that use org-scoped tokens and pass --workspace-id explicitly.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions