Skip to content

Commit 5a9fd64

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 ab88220 commit 5a9fd64

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

lua/tabnine/binary.lua

Lines changed: 23 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

@@ -45,7 +47,22 @@ local function binary_path()
4547
end, paths)
4648

4749
table.sort(paths)
48-
return binaries_path .. "/" .. tostring(paths[#paths]) .. "/" .. arch_and_platform() .. "/" .. binary_name()
50+
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
4966
end
5067

5168
local function optional_args()
@@ -61,7 +78,7 @@ local function optional_args()
6178
end
6279

6380
function TabnineBinary:start()
64-
self.handle, self.pid = uv.spawn(binary_path(), {
81+
self.handle, self.pid = uv.spawn(self.binary_path, {
6582
args = vim.list_extend({
6683
"--client",
6784
"nvim",
@@ -102,12 +119,16 @@ function TabnineBinary:new(o)
102119
self.restart_counter = 0
103120
self.handle = nil
104121
self.pid = nil
122+
self.binary_path = binary_path()
105123
self.callbacks = {}
106124

107125
return o
108126
end
109127

110128
function TabnineBinary:request(request, on_response)
129+
if not self.binary_path then
130+
return function() end -- To avoid breaking user configurations
131+
end
111132
if not self.pid then
112133
self.restart_counter = self.restart_counter + 1
113134
self:start()

0 commit comments

Comments
 (0)