Skip to content

azd init fails with "environment already initialized" when re-running against an existing environment #8562

@huimiu

Description

@huimiu

Problem

Re-running azd init -t <template> --environment <name> in a directory that already has an azd environment hard-fails with:

ERROR: failed to initialize project: ... environment already initialized to <name>-dev

This forces users — and agent/extension flows that drive azd init — into destructive workarounds:

  1. azd env delete <name> then retry, or
  2. Delete the .azure/ folder in the project directory

Neither is acceptable in automated or agent-driven scenarios where azd init should be safely re-runnable.

Root cause

initializeEnv in cli/azd/cmd/init.go unconditionally errors whenever a default environment already exists, without checking whether the requested --environment matches the existing one.

Expected behavior

  • If the requested environment name matches the existing default, silently reuse it (no prompt needed).
  • If running interactively with an ambiguous case, prompt the user: Reuse (default) / Create new.
  • In non-interactive mode, reuse the default unless an explicit, different, valid env name is requested.
  • Recover gracefully from partial/orphaned state (e.g. env folder exists but no recorded default, or stale default whose folder is missing).

Fixed by

PR #8561

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions