Create folder during init#8210
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the azd ai agent init flow (azure.ai.agents extension) to mimic git clone behavior by creating a new subdirectory when initializing from a sample/template, reducing the risk of writing project files into an unexpected directory (e.g., a user’s home folder).
Changes:
- Add a
targetDirparameter to the shared manifest init path so project scaffolding can occur in a chosen directory. - When initializing from a template, derive a folder name from the template title, run
azd initinto that folder, andchdirthe extension process to keep subsequent file operations aligned. - Print a follow-up message indicating where the project was created.
Comments suppressed due to low confidence (1)
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:716
- This env name construction appends "-dev" after sanitization, which can produce names longer than azd's 64-char environment name limit (cli/azd/pkg/environment/environment.go:150-152) when envBase is long. To avoid
azd initfailing, sanitize/truncate the complete "-dev" value (or leverage azd's environment name cleaning) so the final length is guaranteed valid.
sanitizedDirectoryName := sanitizeAgentName(envBase)
initArgs = append(
initArgs, "--environment", sanitizedDirectoryName+"-dev",
)
| initArgs = append( | ||
| initArgs, "--environment", folderName+"-dev", |
| if createdFolder != "" { | ||
| fmt.Printf("\nYour project has been created in ./%s\n", createdFolder) | ||
| } |
| // Sync the extension process into the new project directory. | ||
| // The azd host already chdir'd when it processed the init command. | ||
| if err := os.Chdir(folderName); err != nil { | ||
| return fmt.Errorf( | ||
| "changing to project directory %q: %w", |
jongio
left a comment
There was a problem hiding this comment.
Issues to address:
- init.go:494 - folder name derivation is duplicated between the TemplateTypeAzd and default cases; extract a helper
| title := selectedTemplate.Title | ||
| if idx := strings.IndexByte(title, '('); idx >= 0 { | ||
| title = strings.TrimSpace(title[:idx]) | ||
| } | ||
| folderName := sanitizeAgentName(title) |
There was a problem hiding this comment.
[MEDIUM] The title-to-folder-name derivation (strip parenthetical + sanitize) is copy-pasted here and in the TemplateTypeAzd case (~line 416). Pull it into a small helper so the logic lives in one place:
| title := selectedTemplate.Title | |
| if idx := strings.IndexByte(title, '('); idx >= 0 { | |
| title = strings.TrimSpace(title[:idx]) | |
| } | |
| folderName := sanitizeAgentName(title) | |
| folderName := folderNameFromTitle(selectedTemplate.Title) |
Then add near sanitizeAgentName in init_from_code.go:
func folderNameFromTitle(title string) string {
if idx := strings.IndexByte(title, '('); idx >= 0 {
title = strings.TrimSpace(title[:idx])
}
return sanitizeAgentName(title)
}Update the TemplateTypeAzd case (~line 416-420) the same way.
Related to #8209, example implementation for changing
azd ai agent initto create a new dir when init'ing from a sample.