Skip to content

Commit

Permalink
Handle errors in finding binary path
Browse files Browse the repository at this point in the history
This avoids constant nil errors (fixes #77), and quits early if the binary can't be found.
Notifications can easily be added in future commits if so desired to warn the user about what is wrong.
  • Loading branch information
aarondill committed Jun 19, 2023
1 parent 73c56a1 commit 4d7c16c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lua/tabnine/binary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ local function arch_and_platform()
return "x86_64-pc-windows-gnu"
elseif os_uname.sysname == "Windows_NT" then
return "i686-pc-windows-gnu"
else
return nil
end
end

Expand All @@ -46,7 +48,21 @@ local function binary_path()

table.sort(paths)

return binaries_path .. "/" .. tostring(paths[#paths]) .. "/" .. arch_and_platform() .. "/" .. binary_name()
local version = paths[#paths]
if not version then
return nil -- Is it installed?
end
local machine = arch_and_platform()
if not machine then
return nil -- Is this machine supported?
end
local binary = binaries_path .. "/" .. tostring(version) .. "/" .. machine .. "/" .. binary_name()

if vim.fn.filereadable(binary) then -- Double check that it's installed
return binary
else
return nil -- File doesn't exist or isn't readable. Is it installed?
end
end

local function optional_args()
Expand All @@ -62,7 +78,7 @@ local function optional_args()
end

function TabnineBinary:start()
self.handle, self.pid = uv.spawn(binary_path(), {
self.handle, self.pid = uv.spawn(self.binary_path, {
args = vim.list_extend({
"--client",
"nvim",
Expand Down Expand Up @@ -103,12 +119,16 @@ function TabnineBinary:new(o)
self.restart_counter = 0
self.handle = nil
self.pid = nil
self.binary_path = binary_path()
self.callbacks = {}

return o
end

function TabnineBinary:request(request, on_response)
if not self.binary_path then
return function() end -- To avoid breaking user configurations
end
if not self.pid then
self.restart_counter = self.restart_counter + 1
self:start()
Expand Down

0 comments on commit 4d7c16c

Please sign in to comment.