Azure / azure-dev

A developer CLI that reduces the time it takes for you to get started on Azure. The Azure Developer CLI (azd) provides a set of developer-friendly commands that map to key stages in your workflow - code, build, deploy, monitor, repeat.
https://aka.ms/azd
MIT License
401 stars 193 forks source link

[Issue] Fix `git` related hangs #47

Open weikanglim opened 2 years ago

weikanglim commented 2 years ago

Output from azd version Run azd version and copy and paste the output here: root@weilim-z4:~/todo-python-mongo# azd version azd version 0.0.1-beta.1699350 (commit 17408687d1ec2ccb7dcbdb387811cd6159ba323f)

Describe the bug Linux: without first setting up non-interactive credentials (SSH key or cached tokens) to the repository, run azd init

To Reproduce

  1. Ensure no credentials set, validate by first running git clone manually:
    root@weilim-z4:~/todo-python-mongo# git clone --depth 1 https://github.com/Azure-Samples/todo-python-mongo.git
    Cloning into 'todo-python-mongo'...
    Username for 'https://github.com':
  2. Run azd init (debug flag used below, but not necessary for repro)

    root@weilim-z4:~/todo-python-mongo# azd init --debug
    2022/07/11 11:18:27 init.go:77: forcing project directory to /root/todo-python-mongo
    Initializing a new project in /root/todo-python-mongo
    
    2022/07/11 11:18:27 main.go:148: fetching latest version information for update check
    ? Select a project template Azure-Samples/todo-python-mongo
    2022/07/11 11:18:30 util.go:463: Selected template: Azure-Samples/todo-python-mongo
    Downloading template |

Expected behavior git clone should either fail the command on interactive prompt, or display the prompt

Environment Information on your environment:

Additional context N/A

weikanglim commented 2 years ago

Without supporting additional features, the simplest way is to avoid the hang and fail fast by configuring git to run clone non-interactively via the following env variables, and then translate the git output and provide an error message that helps the user proceed. Translation of error messages can easily be done based on the sample outputs below:

# Disable stdin for SSH
export GIT_SSH_COMMAND='ssh -oBatchMode=yes'
# Disable git terminal prompt
export GIT_TERMINAL_PROMPT=0
# Disable stdin for SSH
export GIT_SSH_COMMAND='ssh -oBatchMode=yes'
# Disable git terminal prompt
export GIT_TERMINAL_PROMPT=0

root@weilim-z4:~# azd init --template git@github.com:Azure-Samples/todo-python-mongo.git
Initializing a new project in /root

Error: fetching template: failed to clone repository git@github.com:Azure-Samples/todo-python-mongo.git, exit code: 128, stdout: , stderr: Cloning into '/tmp/az-dev-template4076525733'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
: exit status 128
root@weilim-z4:~# azd init
Initializing a new project in /root

? Select a project template Azure-Samples/todo-python-mongo
Error: fetching template: failed to clone repository https://github.com/Azure-Samples/todo-python-mongo, exit code: 128, stdout: , stderr: Cloning into '/tmp/az-dev-template3897856328'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
: exit status 128

# After host verification is performed, ssh key denied
root@weilim-z4:~# azd init --template git@github.com:Azure-Samples/todo-python-mongo.git
Initializing a new project in /root

Error: fetching template: failed to clone repository git@github.com:Azure-Samples/todo-python-mongo.git, exit code: 128, stdout: , stderr: Cloning into '/tmp/az-dev-template1501941468'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
: exit status 128
rajeshkamal5050 commented 2 years ago

Not a must-fix for Release 0.2. Keeping it in Backlog.

savannahostrowski commented 1 year ago

I am running into this now with WSL - Ubuntu 20.04 as well.

savannahostrowski commented 1 year ago

@weikanglim I can't repro this anymore. Is this still any issue?

weikanglim commented 1 year ago

@savannahostrowski I think this can still happen if trying to azd init from a repository that isn't public and requires auth.

savannahostrowski commented 1 year ago

Yeah - can confirm. That's still the case.