Read and Write Config on Windows OS
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
Thanks for the workaround! For a quickfix I added:
$Env:HOME = $Env:USERPROFILE
to my PowerShell profile.
Neovim has vim.env.HOME, which (as far as I know) returns the HOME folder path independently of the OS you're using.
@jackMort Do you think this can be a PR?
I got the same error on manjaro os
i can get HOME env variable

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>
please reopen if issue still exists in newest version