CopilotC-Nvim / CopilotChat.nvim

Chat with GitHub Copilot in Neovim
https://copilotc-nvim.github.io/CopilotChat.nvim/
GNU General Public License v3.0
1.44k stars 67 forks source link

No GitHub token found, please use `:Copilot setup` to set it up from copilot.vim or copilot.lua #326

Closed jjmonsalveg closed 3 months ago

jjmonsalveg commented 3 months ago

I'm trying to run it in my system:

$ nvim --version
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1703358377

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info
 lsb_release  -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04 LTS
Release:        24.04
Codename:       noble

Even though the copilot client is working fine and is logged in for the chat I keep receiving

 No GitHub token found, please use `:Copilot setup` to set it up from copilot.vim or copilot.lua

I'm using canary branch but still not working.

deathbeam commented 3 months ago

I'm trying to run it in my system:

$ nvim --version
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1703358377

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info
 lsb_release  -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04 LTS
Release:        24.04
Codename:       noble

Even though the copilot client is working fine and is logged in for the chat I keep receiving

 No GitHub token found, please use `:Copilot setup` to set it up from copilot.vim or copilot.lua

I'm using canary branch but still not working.

Hmm can you check if ~/.config/github-copilot/hosts.json exists? or if that folder exists. Or can you locate where hosts.json is for you? As what we do is basically we read this file to get the stored token that Copilot setup (or other copilot integrations, so copilot shell or even vscode generate)

jjmonsalveg commented 3 months ago

oh! good catch @deathbeam, The folder ~/.config/github-copilot exist however the hosts.json doesn't exist .

OTH I see a file called apps.json that seems to have a json with my user/oauth_token. Is there a chance that github changed the file name? I mean, this is working on my mac(but was installed a few weeks ago) and checking there I see the file hosts.json the json structure of these both are similar but now the github.com key has a new sufix.

Workaround (it worked!):

cp ~/.config/github-copilot/apps.json ~/.config/github-copilot/hosts.json

open ~/.config/github-copilot/hosts.json and ensure that the key is "github.com" (without sufix). I guess it is a temporal solution

jjmonsalveg commented 3 months ago

@deathbeam I don't know lua or even how to contribute to the project however I see this line

and copilot say:

local function get_cached_token()
  -- loading token from the environment only in GitHub Codespaces
  local token = os.getenv('GITHUB_TOKEN')
  local codespaces = os.getenv('CODESPACES')
  if token and codespaces then
    return token
  end

  -- loading token from the file
  local config_path = find_config_path()
  if not config_path then
    return nil
  end

  local file_paths = {
    config_path .. '/github-copilot/hosts.json',
    config_path .. '/github-copilot/apps.json'
  }

  for _, file_path in ipairs(file_paths) do
    if vim.fn.filereadable(file_path) == 1 then
      local userdata = vim.fn.json_decode(vim.fn.readfile(file_path))
      if userdata['github.com'] and userdata['github.com'].oauth_token then
        return userdata['github.com'].oauth_token
      end
    end
  end

  return nil
end

which should be backward compatible. But it is still missing the prefix case

deathbeam commented 3 months ago

Oh interesting, did not knew about the apps file. Alright I will look at this

rbraffin commented 3 months ago

Hey @deathbeam! I was taking a look at it and, since apps.json appends the "githubAppId" to the "github.com" key, maybe something like

for key, value in pairs(userdata) do
  if string.find(key, "github.com") then
    return value.oauth_token
  end
end

should be used instead of

if userdata['github.com'] and userdata['github.com'].oauth_token then
        return userdata['github.com'].oauth_token
end

so it works without needing to erase the ID suffix as in @jjmonsalveg 's workaround.

deathbeam commented 3 months ago

Oh completely missed that, yea makes sense