Ensure correct tools installation in goenv mode (#332)

* Ensure correct tools installation in goenv mode

Tools will be installed in appropriated directory when
[goenv](https://github.com/syndbg/goenv) manages
GOPATH.

* Check goenv existence with which

* Try better (?) goenv detection command

* Do not check if tool is installed twice

Once a tool is known to be installed, that information is cached in
utils.installed_tools.

* Wait 500ms for action to take effect in tests
This commit is contained in:
Faustin Date 2023-05-08 10:59:30 +09:00 committed by GitHub
parent 40684d1ee0
commit 8ea5439288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 14 deletions

View File

@ -35,16 +35,38 @@ for tool, _ in pairs(url) do
end end
local function is_installed(bin) local function is_installed(bin)
local env_path = os.getenv('PATH') if utils.installed_tools[bin] then
if vim.fn.executable(bin) == 1 then
return true return true
end end
local sep = utils.sep2() local sep = utils.sep2()
local ext = utils.ext() local ext = utils.ext()
if utils.goenv_mode() then
local cwd = vim.fn.getcwd()
local cmd = "cd " .. cwd .. " && goenv which " .. bin .. " 2>&1"
local status = os.execute(cmd)
if status == 0 then
utils.installed_tools[bin] = true
return true
end
return false
end
if vim.fn.executable(bin) == 1 then
utils.installed_tools[bin] = true
return true
end
local env_path = os.getenv('PATH')
local base_paths = vim.split(env_path, sep, true) local base_paths = vim.split(env_path, sep, true)
for _, value in pairs(base_paths) do for _, value in pairs(base_paths) do
if uv.fs_stat(value .. DIR_SEP .. bin .. ext) then if uv.fs_stat(value .. DIR_SEP .. bin .. ext) then
utils.installed_tools[bin] = true
return true return true
end end
end end
@ -63,6 +85,9 @@ local function go_install_sync(pkg)
u = u .. '@latest' u = u .. '@latest'
local setup = { 'go', 'install', u } local setup = { 'go', 'install', u }
if utils.goenv_mode() then
setup = { 'goenv', 'exec', 'go', 'install', u }
end
local output = vim.fn.system(table.concat(setup, ' ')) local output = vim.fn.system(table.concat(setup, ' '))
if vim.v.shell_error ~= 0 then if vim.v.shell_error ~= 0 then
vim.notify('install ' .. pkg .. ' failed: ' .. output, vim.log.levels.ERROR) vim.notify('install ' .. pkg .. ' failed: ' .. output, vim.log.levels.ERROR)
@ -83,6 +108,9 @@ local function go_install(pkg)
u = u .. '@latest' u = u .. '@latest'
local setup = { 'go', 'install', u } local setup = { 'go', 'install', u }
if utils.goenv_mode() then
setup = { 'goenv', 'exec', 'go', 'install', u }
end
vim.fn.jobstart(setup, { vim.fn.jobstart(setup, {
on_stdout = function(_, data, _) on_stdout = function(_, data, _)

View File

@ -47,6 +47,7 @@ function util.ext()
end end
return '' return ''
end end
local function get_path_sep() local function get_path_sep()
if is_windows then if is_windows then
return ';' return ';'
@ -291,7 +292,8 @@ util.log = function(...)
end end
end end
util.trace = function(...) end util.trace = function(...)
end
local rhs_options = {} local rhs_options = {}
@ -652,6 +654,7 @@ function util.get_active_buf()
return result return result
end end
-- for l:item in l:blist -- for l:item in l:blist
-- "skip unnamed buffers; also skip hidden buffers? -- "skip unnamed buffers; also skip hidden buffers?
-- if empty(l:item.name) || l:item.hidden -- if empty(l:item.name) || l:item.hidden
@ -828,7 +831,6 @@ util.extract_filepath = function(msg, pkg_path)
msg = msg or '' msg = msg or ''
-- util.log(msg) -- util.log(msg)
--[[ or [[ findAllSubStr_test.go:234: Error inserting caseResult1: operation error DynamoDB: PutItem, exceeded maximum number of attempts]] --[[ or [[ findAllSubStr_test.go:234: Error inserting caseResult1: operation error DynamoDB: PutItem, exceeded maximum number of attempts]]
-- or 'path/path2/filename.go:50:11: Error invaild -- or 'path/path2/filename.go:50:11: Error invaild
-- or /home/ray/go/src/github/sample/app/driver.go:342 +0x19e5 -- or /home/ray/go/src/github/sample/app/driver.go:342 +0x19e5
local ma = fn.matchlist(msg, [[\v\s*(\w+.+\.go):(\d+):]]) local ma = fn.matchlist(msg, [[\v\s*(\w+.+\.go):(\d+):]])
@ -907,4 +909,21 @@ util.remove_ansi_escape = function(str)
return str return str
end end
-- Keeps track of tools that are already installed.
-- The keys are the names of tools and the values are booleans
-- indicating whether the tools is available or not.
util.installed_tools = {}
-- Check if host has goenv in path.
util.goenv_mode = function()
if is_windows then
-- always return false for Windows because goenv doesn't seem to be supported there.
return false
end
local cmd = "command -v goenv > /dev/null 2>&1"
local status = os.execute(cmd)
return status == 0
end
return util return util

View File

@ -6,6 +6,9 @@ local cur_dir = vim.fn.expand("%:p:h")
-- local status = require("plenary.reload").reload_module("go.nvim") -- local status = require("plenary.reload").reload_module("go.nvim")
-- status = require("plenary.reload").reload_module("nvim-treesitter") -- status = require("plenary.reload").reload_module("nvim-treesitter")
-- time to wait for action to take effect
local wait_time = 500
-- local ulog = require('go.utils').log -- local ulog = require('go.utils').log
describe("should run gotags", function() describe("should run gotags", function()
local cmd local cmd
@ -37,7 +40,7 @@ describe("should run gotags", function()
local gotags = require("go.tags") local gotags = require("go.tags")
gotags.add() gotags.add()
-- enable the channel response -- enable the channel response
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
-- ulog("tagged file: " .. fmt) -- ulog("tagged file: " .. fmt)
@ -71,13 +74,13 @@ describe("should run gotags", function()
local gotags = require("go.tags") local gotags = require("go.tags")
gotags.rm('json') gotags.rm('json')
-- enable the channel response -- enable the channel response
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
-- format the code -- format the code
local gofmt = require("go.format") local gofmt = require("go.format")
gofmt.gofmt() gofmt.gofmt()
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
-- ulog("tagged file: " .. fmt) -- ulog("tagged file: " .. fmt)
@ -111,12 +114,12 @@ describe("should run gotags", function()
local gotags = require("go.tags") local gotags = require("go.tags")
gotags.rm() gotags.rm()
-- enable the channel response -- enable the channel response
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
local gofmt = require("go.format") local gofmt = require("go.format")
gofmt.gofmt() gofmt.gofmt()
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
@ -152,12 +155,12 @@ describe("should run gotags", function()
local gotags = require("go.tags") local gotags = require("go.tags")
gotags.rm() gotags.rm()
-- enable the channel response -- enable the channel response
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
local gofmt = require("go.format") local gofmt = require("go.format")
gofmt.gofmt() gofmt.gofmt()
vim.wait(100, function() vim.wait(wait_time, function()
end) end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") local fmt = vim.fn.join(vim.fn.readfile(name), "\n")