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:
azd env delete <name> then retry, or
- 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
Problem
Re-running
azd init -t <template> --environment <name>in a directory that already has an azd environment hard-fails with:This forces users — and agent/extension flows that drive
azd init— into destructive workarounds:azd env delete <name>then retry, or.azure/folder in the project directoryNeither is acceptable in automated or agent-driven scenarios where
azd initshould be safely re-runnable.Root cause
initializeEnvincli/azd/cmd/init.gounconditionally errors whenever a default environment already exists, without checking whether the requested--environmentmatches the existing one.Expected behavior
Fixed by
PR #8561