Skip to content

Create folder during init#8210

Open
banrahan wants to merge 2 commits into
Azure:mainfrom
banrahan:create-folder-during-init
Open

Create folder during init#8210
banrahan wants to merge 2 commits into
Azure:mainfrom
banrahan:create-folder-during-init

Conversation

@banrahan
Copy link
Copy Markdown

Related to #8209, example implementation for changing azd ai agent init to create a new dir when init'ing from a sample.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 targetDir parameter 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 init into that folder, and chdir the 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 init failing, 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",
			)

Comment on lines +425 to +426
initArgs = append(
initArgs, "--environment", folderName+"-dev",
Comment on lines +528 to +530
if createdFolder != "" {
fmt.Printf("\nYour project has been created in ./%s\n", createdFolder)
}
Comment on lines +458 to +462
// 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",
Copy link
Copy Markdown
Member

@jongio jongio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issues to address:

  • init.go:494 - folder name derivation is duplicated between the TemplateTypeAzd and default cases; extract a helper

Comment on lines +494 to +498
title := selectedTemplate.Title
if idx := strings.IndexByte(title, '('); idx >= 0 {
title = strings.TrimSpace(title[:idx])
}
folderName := sanitizeAgentName(title)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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:

Suggested change
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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants