azd ai agent init: `cd` next-step hint suppressed when target folder pre-exists, causing `azd provision` to fail with "no project exists"
- Dominant language
- Go
- Stars
- 569
- Forks
- 364
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 136
Description
## Summary
When running `azd ai agent init -m ` (code deploy mode), the extension scaffolds the project into a **subdirectory** derived from the agent name and `chdir`s the extension process into it, but leaves the user's shell in the parent directory. The post-init `Next:` block is supposed to emit a leading `cd ` hint so the user navigates into the new project before running `azd provision`. That hint is incorrectly suppressed whenever the target folder already exists on disk, so the user runs `azd provision` from the parent directory and hits:
```
ERROR: no project exists; to create a new project, run azd init
```
even though `azure.yaml` was created successfully -- just one directory down.
## Repro
1. Pre-create (or re-run into) a folder, e.g. `agent-framework-agent-basic-responses/`, so the target directory exists and is non-empty.
2. Run `azd ai agent init -m "https://github.com/microsoft-foundry/foundry-samples/blob/main/samples/python/hosted-agents/agent-framework/responses/01-basic/agent.manifest.yaml" --deploy-mode code` and accept the agent name that maps to that folder.
3. Init completes: `AI agent definition added to your azd project successfully!` The `Next:` block shows `azd provision` / `azd deploy` but no `cd` line.
4. Run `azd provision` from the same (parent) directory.
## Expected
The `Next:` block includes a leading `cd agent-framework-agent-basic-responses` step (the project was created in that subdirectory), so `azd provision` runs from the correct directory and succeeds.
## Actual
No `cd` hint is shown. `azd provision` runs in the parent directory, finds no `azure.yaml`, and fails with `no project exists; to create a new project, run azd init`. The `azure.yaml` actually lives in the `agent-framework-agent-basic-responses/` subdirectory.
## Root cause
In `cli/azd/extensions/azure.ai.agents/internal/cmd/init.go`, the manifest (`-m`) path gates the `cd` hint on `newlyCreated && !existingProject` (init.go:1086-1092):
```go
if !manifestInCwd {
_, statErr := os.Stat(folderName)
newlyCreated := errors.Is(statErr, fs.ErrNotExist)
targetDir = folderName
if newlyCreated && !existingProject {
folderDisplay = filepath.ToSlash(folderName)
}
}
```
When `existingProject` is false, the project is **always** scaffolded into the `folderName` subdirectory (`targetDir = folderName`), regardless of whether that folder already existed. `ensureProject` then `chdir`s the extension process into `targetDir` (init.go:1559) while the user's shell stays put. So the `cd` hint is required in every "scaffold into subfolder" case -- but `newlyCreated` becomes false the moment the folder pre-exists (a re-run, or the user created it ahead of time), zeroing out `folderDisplay`.
`folderDisplay` flows to `nextstep` as `CreatedFolderDisplay` (init.go:2710 -> `WithCreatedFolder`), and the resolver only emits the `cd` suggestion when that field is non-empty (`internal/cmd/nextstep/resolver.go:145`). Empty -> no `cd` line.
The "no project exists" error itself is raised by azd core in `cli/azd/pkg/environment/azdcontext/azdcontext.go:156` (`ErrNoProject`) because there is no `azure.yaml` in the parent directory.
The template (azd template) path has the identical flawed gate at init.go:1190:
```go
if newlyCreated && !existingProject {
folderDisplay = filepath.ToSlash(folderName)
}
```
The bug is that `newlyCreated` conflates "the folder did not exist on disk" with "the user does not need to navigate." Navigation is required whenever the project lands in a subdirectory, which is driven by `!existingProject`, not by whether the folder was freshly made.
## Suggested fix
Drop the `newlyCreated` condition at both call sites (init.go:1090 and init.go:1190) and gate `folderDisplay` on `!existingProject` alone. Whenever init scaffolds into a subfolder, the `cd` hint should be shown. Add regression coverage for the "target folder pre-exists" case so the `cd` suggestion is asserted.
## Environment
* azd AI Agent extension: v0.1.41-preview
* azd core: 0.x (host)
* OS: observed on WSL/Linux; the gate logic is platform-independent.
## Related
Regression against the post-`init` `Next:` guidance introduced for #7975.
Contributor guide
Assessment
This issue has not been assessed yet.