netlify / cli

Netlify Command Line Interface
http://cli.netlify.com
MIT License
1.58k stars 358 forks source link

Unable to use `ntl init` — can't locate system files to do a full reset #5255

Open jlengstorf opened 1 year ago

jlengstorf commented 1 year ago

Describe the bug

I've had a weird issue with the CLI where I can't use ntl init. Whenever I try to initialize a site from within my GitHub org (learnwithjason) I get the error:

Error: Failed adding GitHub deploy key with error: Not Found. Does the repository site-test exist and do learnwithjason has the correct permissions to set up deploy keys?

I have logged out, logged in again, and made sure that my account has the right permissions. It's all there.

I have a suspicion that this is due to the files the CLI creates on my computer to track tokens, etc. — I use nvm, volta, etc. (my show has me installing all sorts of stuff), and I have a hunch that there are system files that I can't find that are being picked up instead of the ones that should be picked up.

What I need help with

Where are CLI files created? (And where have they been created historically?) I want to do a thorough cleaning out of all state files to see if it solves this issue.

Steps to reproduce

github.com/learnwithjason on  main [!]
❯ mkdir site-test

github.com/learnwithjason on  main [!]
❯ cd site-test

github.com/learnwithjason/site-test on  main [!]
❯ git init
Initialized empty Git repository in /Users/blitz/github/learnwithjason/site-test/.git/

github.com/learnwithjason/site-test on  main
❯ echo "<h1>Hello world</h1>" > index.html

github.com/learnwithjason/site-test on  main [?]
❯ ls
index.html

github.com/learnwithjason/site-test on  main [?]
❯ git add -A

github.com/learnwithjason/site-test on  main [+]
❯ s
## No commits yet on main
A  index.html

github.com/learnwithjason/site-test on  main [+]
❯ git commit -m 'feat: initial commit'
[main (root-commit) d9c50b9] feat: initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

github.com/learnwithjason/site-test on  main
❯ gh repo create
? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name learnwithjason/site-test
? Description site-test
? Visibility Public
✓ Created repository learnwithjason/site-test on GitHub
? Add a remote? Yes
? What should the new remote be called? origin
✓ Added remote git@github.com:learnwithjason/site-test.git
? Would you like to push commits from the current branch to "origin"? Yes
✓ Pushed commits to git@github.com:learnwithjason/site-test.git

A new release of gh is available: 2.17.0 → 2.20.2
To upgrade, run: brew upgrade gh
https://github.com/cli/cli/releases/tag/v2.20.2

github.com/learnwithjason/site-test on  main took 17s
❯ ntl init
? What would you like to do? +  Create & configure a new site
? Team: Sites by Blitz Jackson
? Site name (leave blank for a random name; you can change it later):

Site Created

Admin URL: https://app.netlify.com/sites/melodious-lebkuchen-c1f45b
URL:       https://melodious-lebkuchen-c1f45b.netlify.app
Site ID:   a5eefcfc-f13d-4d86-bfeb-0c81ecb84341

Linked to melodious-lebkuchen-c1f45b

(node:28946) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
? Your build command (hugo build/yarn run build/etc): # no build command
? Directory to deploy (blank for current dir): .
? No netlify.toml detected. Would you like to create one with these build settin
gs? No

Adding deploy key to repository...
 ›   Error: Failed adding GitHub deploy key with error: Not Found. Does the repository site-test exist and do learnwithjason has the correct permissions to set up deploy keys?

Configuration

No response

Environment

  System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Max
    Memory: 440.28 MB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.11.0 - /opt/homebrew/bin/node
    npm: 8.19.2 - /opt/homebrew/bin/npm
  npmGlobalPackages:
    netlify-cli: 12.2.4
lemusthelroy commented 1 year ago

Unfortunately I'm not able to replicate. I will dig a little deeper into the error above and the init command to see if I can understand why it may fail.

  System:
    OS: macOS 12.5.1
    CPU: Apple M1 Pro

  Binaries:
    Node: 16.16.0
    npm: 9.1.1

  npmGlobalPackages:
    netlify-cli: 12.2.6    
lemusthelroy commented 1 year ago

One quick fire suggestion on this is to maybe create a new access token for your GitHub CLI. Once that's configured, go through the repro steps again and see if you get the same error. I'll have a further investigation to see if I can give more precise advice.

jlengstorf commented 1 year ago

@lemusthelroy I just created a new token (gh auth logout, gh auth login) and verified that the token did indeed change.

Same error when using ntl init.

I also just validated that we don't remove all files when uninstalling. I ran npm uninstall -g netlify-cli, then reinstalled and I was still logged in — I'm pretty convinced the problem is related to me having multiple instances of these system files stored away somewhere, but I can't figure out where.

jlengstorf commented 1 year ago

Okay — I was able to reproduce and fix the issue. I do still think there's a bug, but I don't know how to get further than I got. 😅

I dug into how config is stored and ended up here: https://github.com/netlify/cli/blob/9571399edfdca3cec470a1275ce8fcc1a6d1c103/src/lib/settings.cjs#LL6C26-L6C26

I'm on macOS, so I ran rm ~/Library/Preferences/netlify — this successfully cleared things out for me.

I then ran ntl login — sent me to the browser to authenticate and everything worked as expected.

Next, I ran ntl init. It offers two options for connecting to GitHub: app.netlify.com or a GitHub token.

First, I chose app.netlify.com and got the same error:

github.com/learnwithjason/site-test on  main [?] took 22s
❯ ntl init

? Netlify CLI needs access to your GitHub account to configure Webhooks and Depl
oy Keys. What would you like to do? Authorize with GitHub through app.netlify.co
m
? Your build command (hugo build/yarn run build/etc): # no build command
? Directory to deploy (blank for current dir): .

Adding deploy key to repository...
(node:35221) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 ›   Error: Failed adding GitHub deploy key with error: Not Found. Does the repository site-test exist and do learnwithjason has the correct permissions to set up deploy keys?

I have no idea why this happens, because I can successfully add sites from the learnwithjason org through the app.netlify.com UI.

After this failed, I blew everything away again and started over. When I ran ntl init this time, I chose "Authorize with a GitHub personal access token". I got the token by running gh auth token.

This worked as expected and I'm now able to set up new sites from the command line again.

If I can add any additional details about my UI setup, please let me know. I don't know if this is an issue that happens to people other than me, but it would be great to double check since not being able to initialize sites from a GitHub org is a big issue.

Thanks!