Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Previously, azd failed immediately when -C/--cwd pointed to a non-existent directory. This required users to manually create directories before running commands like azd init -t template -C ./new-folder.

Changes

Modified PersistentPreRunE in cmd/root.go:

  • Check directory existence before os.Chdir
  • Prompt user to create if missing (default: Yes)
  • Auto-create with --no-prompt flag
  • Support nested paths via os.MkdirAll

Added test coverage:

  • Single-level directory creation
  • Nested directory creation (parent/child/project)

Usage

# Interactive mode - prompts with default Yes
azd init -t azure-samples/todo-nodejs-mongo -C ./new-folder

# Non-interactive - auto-creates
azd init -t azure-samples/todo-nodejs-mongo -C ./new-folder --no-prompt

# Nested paths supported
azd init -t template -C ./parent/child/project --no-prompt

Fixes #3680

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: /home/REDACTED/work/azure-dev/azure-dev/cli/azd/azd-test init -t azure-samples/todo-nodejs-mongo -C ./new-folder (dns block)
    • Triggering command: /home/REDACTED/work/azure-dev/azure-dev/cli/azd/azd-test init -t azure-samples/todo-nodejs-mongo -C ./new-folder --no-prompt (dns block)
    • Triggering command: /home/REDACTED/work/azure-dev/azure-dev/cli/azd/azd-test telemetry upload (dns block)
  • westus-0.in.applicationinsights.azure.com
    • Triggering command: /home/REDACTED/work/azure-dev/azure-dev/cli/azd/azd-test telemetry upload (dns block)
    • Triggering command: /home/REDACTED/work/azure-dev/azure-dev/cli/azd/azd telemetry upload (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

See issue: #3680

I think we could re-use the -C, --cwd path here.

Right now, if we try to use it for some path that does not exist, azd fails, like

$ azd init -t azure/azure-dev -C ./new-folder
Error: failed to change directory to ./new-folder: chdir ./new-folder: no such file or directory

Azd should ask user if they want to create the folder when it does not exists - using Yes as the default value for the confirmation


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits November 20, 2025 05:36
Co-authored-by: vhvb1989 <24213737+vhvb1989@users.noreply.github.com>
Co-authored-by: vhvb1989 <24213737+vhvb1989@users.noreply.github.com>
Co-authored-by: vhvb1989 <24213737+vhvb1989@users.noreply.github.com>
Copilot AI changed the title [WIP] Add confirmation for creating directory in azd init Prompt to create directory when using -C/--cwd with non-existent path Nov 20, 2025
Copilot AI requested a review from vhvb1989 November 20, 2025 05:55
Copilot finished work on behalf of vhvb1989 November 20, 2025 05:55
@vhvb1989 vhvb1989 marked this pull request as ready for review November 20, 2025 21:55

// Add -C flag with the subdirectory path
targetDir := filepath.Join(parentDir, tt.subDir)
args := append([]string{"-C", tt.subDir}, tt.args...)
Copy link
Contributor

Choose a reason for hiding this comment

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

@vhvb1989 Does this work? I didn't know we supported a -C flag. I was under the impression that --cwd is the only option currently available.

if !opts.NoPrompt {
// Prompt the user
defaultValue := true
confirm := ux.NewConfirm(&ux.ConfirmOptions{
Copy link
Contributor

Choose a reason for hiding this comment

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

@vhvb1989 To confirm, are we switching over to leverage ux.NewConfirm here?

I know I'm personally blocked because the keyboard navigation doesn't work on ghostty
(#5704)

Copy link
Contributor

@weikanglim weikanglim left a comment

Choose a reason for hiding this comment

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

@vhvb1989 Thanks for getting this out quickly! I am very much in favor of supporting something along the lines of "initialize azd in a new directory" because that is quite useful. I am very much in favor of us landing a change that supports this.

I spent some thinking about this a little more, and I wonder if you would consider azd init <directory>, i.e. azd init my-app -t <template> as the way we'd communicate this to the user. I believe that may be shorter and nicer than the current proposal of azd init -t <template> --cwd <directory> --no-prompt and would provide a better DX.

I am also seeing that it makes azd init my-app a nicer gesture to initialize "my-app" project, but do it interactively.

@vhvb1989
Copy link
Member

@vhvb1989 Thanks for getting this out quickly! I am very much in favor of supporting something along the lines of "initialize azd in a new directory" because that is quite useful. I am very much in favor of us landing a change that supports this.

I spent some thinking about this a little more, and I wonder if you would consider azd init <directory>, i.e. azd init my-app -t <template> as the way we'd communicate this to the user. I believe that may be shorter and nicer than the current proposal of azd init -t <template> --cwd <directory> --no-prompt and would provide a better DX.

I am also seeing that it makes azd init my-app a nicer gesture to initialize "my-app" project, but do it interactively.

azd init <path> looks good then we provide just the name, but the intention is to let azd to create the full path, so you don't need to worry about first creating the path where you want the template.

I personally find the path to be unexpected next to init. It makes sense to provide a name for the project you are creating.. but I think the name and the path are different concepts.

@weikanglim
Copy link
Contributor

@vhvb1989

I personally find the path to be unexpected next to init. It makes sense to provide a name for the project you are creating.. but I think the name and the path are different concepts.

azd would default the project name to match the directory name here. The primary use case here is to document this very nicely in templates and first-time usage of azd docs:

azd init my-app -t <template>
cd my-app

or

azd init my-app
cd my-app

See bun init for what this could look like for azd.

@weikanglim
Copy link
Contributor

That being said, I'm not opposed to having the functionality as you have proposed here as it doesn't stop us from adding a more streamlined entrypoint in the future. I have approved.

@wbreza
Copy link
Contributor

wbreza commented Dec 1, 2025

Another related issue is #181 with some good commentary and proposed designs. Please review before landing on an implementation.

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.

[Feature Request] AZD init with folder parameter

4 participants