ChatGPT.nvim icon indicating copy to clipboard operation
ChatGPT.nvim copied to clipboard

Read and Write Config on Windows OS

Open RichestHumanAlive opened this issue 3 years ago • 5 comments

Hey! Awesome plugin there!

I got this error running ChatGPT command on Windows after configuring ChatGPT.nvim according to the README.md :

Error executing Lua callback: ...\pack\packer\start\ChatGPT.nvim/lua/chatgpt/settings.lua:45: attempt to concatenate a nil value
stack traceback:
        ...\pack\packer\start\ChatGPT.nvim/lua/chatgpt/settings.lua:45: in function 'read_config'
        ...\pack\packer\start\ChatGPT.nvim/lua/chatgpt/settings.lua:69: in function 'get_settings_panel'
        ...te\pack\packer\start\ChatGPT.nvim/lua/chatgpt/module.lua:88: in function 'open_chat'
        ...te\pack\packer\start\ChatGPT.nvim/lua/chatgpt/module.lua:208: in function 'openChat'
        ...data\site\pack\packer\start\ChatGPT.nvim/lua/chatgpt.lua:24: in function 'openChat'
        ...a\site\pack\packer\start\ChatGPT.nvim\plugin\chatgpt.lua:2: in function <...a\site\pack\packer\start\ChatGPT.nvim\plugin\chatgpt.lua:1>

The error occurs precisely on this line :

local file = io.open(os.getenv("HOME") .. "/" .. ".chatgpt-" .. M.type .. "-params.json", "rb")

I did some researches and according to StackOverflow HOME is for Linux and on Windows this env variable has different name: USERPROFILE


You can just update the code with this snippet (or optimized version) to fix that issue for Windows users :

local function get_os_name()
  local os = vim.loop.os_uname()
  local os_name = os.sysname
  return os_name
end

local function get_user_profile_key()
  local os_name = get_os_name()

  local user_profile_key = nil
  if os_name == "Windows_NT" or os_name == "Windows" then
    user_profile_key = "USERPROFILE"
  elseif os_name == "Darwin" then
    user_profile_key = "HOME"
  else
    user_profile_key = "HOME"
  end
  return user_profile_key
end

M.read_config = function()
  local file = io.open(os.getenv(get_user_profile_key()) .. "/" .. ".chatgpt-" .. M.type .. "-params.json", "rb")
 -- ... 
end

M.write_config = function(config)
  local file, err = io.open(os.getenv(get_user_profile_key()) .. "/" .. ".chatgpt-" .. M.type .. "-params.json", "w")
-- ...
end

This is what I did and everything works fine now!

Awesome plugin 💯 once again

RichestHumanAlive avatar Jan 10 '23 12:01 RichestHumanAlive

Thanks for the workaround! For a quickfix I added:

$Env:HOME = $Env:USERPROFILE

to my PowerShell profile.

BartSte avatar Jan 17 '23 10:01 BartSte

Neovim has vim.env.HOME, which (as far as I know) returns the HOME folder path independently of the OS you're using.

luiz00martins avatar Apr 05 '23 19:04 luiz00martins

@jackMort Do you think this can be a PR?

azinsharaf avatar Apr 17 '23 21:04 azinsharaf

I got the same error on manjaro os image i can get HOME env variable image

a1401358759 avatar Apr 21 '23 02:04 a1401358759

I'm getting the same issue on a Debian system

Error executing Lua callback: .../pack/packer/start/ChatGPT.nvim/lua/chatgpt/settings.lua:71: bad argument #1 to 'read_config' (string expected, got nil)                                                                                                       
stack traceback:                                                                                                                                                                                                                                                
        [C]: in function 'read_config'                                                                                                                                                                                                                          
        .../pack/packer/start/ChatGPT.nvim/lua/chatgpt/settings.lua:71: in function 'get_settings_panel'                                                                                                                                                        
        ...te/pack/packer/start/ChatGPT.nvim/lua/chatgpt/module.lua:67: in function 'open_chat'                                                                                                                                                                 
        ...te/pack/packer/start/ChatGPT.nvim/lua/chatgpt/module.lua:277: in function 'openChat'                                                                                                                                                                 
        ...nvim/site/pack/packer/start/ChatGPT.nvim/lua/chatgpt.lua:25: in function 'openChat'                                                                                                                                                                  
        ...m/site/pack/packer/start/ChatGPT.nvim/plugin/chatgpt.lua:2: in function <...m/site/pack/packer/start/ChatGPT.nvim/plugin/chatgpt.lua:1>      

ben-wall avatar Apr 21 '23 02:04 ben-wall

please reopen if issue still exists in newest version

jackMort avatar Dec 14 '23 12:12 jackMort