Skip to content

Commit f89f5d7

Browse files
committed
Handle errors in finding binary path
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.
1 parent eb914c2 commit f89f5d7

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

lua/tabnine/binary.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ local function arch_and_platform()
2323
return "x86_64-pc-windows-gnu"
2424
elseif os_uname.sysname == "Windows_NT" then
2525
return "i686-pc-windows-gnu"
26+
else
27+
return nil
2628
end
2729
end
2830

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

4749
table.sort(paths)
4850

49-
return binaries_path .. "/" .. tostring(paths[#paths]) .. "/" .. arch_and_platform() .. "/" .. binary_name()
51+
local version = paths[#paths]
52+
if not version then
53+
return nil -- Is it installed?
54+
end
55+
local machine = arch_and_platform()
56+
if not machine then
57+
return nil -- Is this machine supported?
58+
end
59+
local binary = binaries_path .. "/" .. tostring(version) .. "/" .. machine .. "/" .. binary_name()
60+
61+
if vim.fn.filereadable(binary) then -- Double check that it's installed
62+
return binary
63+
else
64+
return nil -- File doesn't exist or isn't readable. Is it installed?
65+
end
5066
end
5167

5268
local function optional_args()
@@ -61,7 +77,7 @@ function TabnineBinary:start()
6177
self.stdin = uv.new_pipe()
6278
self.stdout = uv.new_pipe()
6379
self.stderr = uv.new_pipe()
64-
self.handle, self.pid = uv.spawn(binary_path(), {
80+
self.handle, self.pid = uv.spawn(self.binary_path, {
6581
args = vim.list_extend({
6682
"--client",
6783
"nvim",
@@ -103,12 +119,16 @@ function TabnineBinary:new(o)
103119
self.restart_counter = 0
104120
self.handle = nil
105121
self.pid = nil
122+
self.binary_path = binary_path()
106123
self.callbacks = {}
107124

108125
return o
109126
end
110127

111128
function TabnineBinary:request(request, on_response)
129+
if not self.binary_path then
130+
return function() end -- To avoid breaking user configurations
131+
end
112132
if not self.pid then
113133
self.restart_counter = self.restart_counter + 1
114134
self:start()

0 commit comments

Comments
 (0)