Skip to content

Commit

Permalink
fix: check version sha of locally built, better detection
Browse files Browse the repository at this point in the history
Related to #68
  • Loading branch information
Saghen committed Dec 23, 2024
1 parent 129e422 commit 3ffd31d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 72 deletions.
14 changes: 12 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
fn main() {
// delete existing version.txt file created by downloader
let _ = std::fs::remove_file("target/release/version.txt");
// delete existing version file created by downloader
let _ = std::fs::remove_file("target/release/version");

// get current sha from git
let output = std::process::Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.unwrap();
let sha = String::from_utf8(output.stdout).unwrap();

// write to version
std::fs::write("target/release/version", sha.trim()).unwrap();
}
5 changes: 4 additions & 1 deletion lua/blink/cmp/config/fuzzy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

--- @class (exact) blink.cmp.PrebuiltBinariesConfig
--- @field download boolean Whenther or not to automatically download a prebuilt binary from github. If this is set to `false` you will need to manually build the fuzzy binary dependencies by running `cargo build --release`
--- @field ignore_version_mismatch boolean Ignores mismatched version between the built binary and the current git sha, when building locally
--- @field force_version? string When downloading a prebuilt binary, force the downloader to resolve this version. If this is unset then the downloader will attempt to infer the version from the checked out git tag (if any). WARN: Beware that `main` may be incompatible with the version you select
--- @field force_system_triple? string When downloading a prebuilt binary, force the downloader to use this system triple. If this is unset then the downloader will attempt to infer the system triple from `jit.os` and `jit.arch`. Check the latest release for all available system triples. WARN: Beware that `main` may be incompatible with the version you select
--- @field extra_curl_args? string[] Extra arguments that will be passed to curl like { 'curl', ..extra_curl_args, ..built_in_args }
--- @field extra_curl_args string[] Extra arguments that will be passed to curl like { 'curl', ..extra_curl_args, ..built_in_args }

--- @alias blink.cmp.SortFunction fun(a: blink.cmp.CompletionItem, b: blink.cmp.CompletionItem): boolean | nil

Expand All @@ -23,6 +24,7 @@ local fuzzy = {
sorts = { 'score', 'sort_text' },
prebuilt_binaries = {
download = true,
ignore_version_mismatch = false,
force_version = nil,
force_system_triple = nil,
extra_curl_args = {},
Expand Down Expand Up @@ -51,6 +53,7 @@ function fuzzy.validate(config)
}, config)
validate('fuzzy.prebuilt_binaries', {
download = { config.prebuilt_binaries.download, 'boolean' },
ignore_version_mismatch = { config.prebuilt_binaries.ignore_version_mismatch, 'boolean' },
force_version = { config.prebuilt_binaries.force_version, { 'string', 'nil' } },
force_system_triple = { config.prebuilt_binaries.force_system_triple, { 'string', 'nil' } },
extra_curl_args = { config.prebuilt_binaries.extra_curl_args, { 'table' } },
Expand Down
26 changes: 13 additions & 13 deletions lua/blink/cmp/fuzzy/download/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local lib_filename = 'libblink_cmp_fuzzy' .. get_lib_extension()
local lib_path = lib_folder .. '/' .. lib_filename
local checksum_filename = lib_filename .. '.sha256'
local checksum_path = lib_path .. '.sha256'
local version_path = lib_folder .. '/version.txt'
local version_path = lib_folder .. '/version'

local files = {
get_lib_extension = get_lib_extension,
Expand Down Expand Up @@ -73,22 +73,22 @@ end

--- Prebuilt binary ---

function files.is_downloaded()
return files.exists(files.lib_path):map(function(exists)
if exists then return true end

-- If not found, check without 'lib' prefix
return files.exists(string.gsub(files.lib_path, 'libblink_cmp_fuzzy', 'blink_cmp_fuzzy'))
end)
end

function files.get_downloaded_version()
return files.read_file(files.version_path):catch(function() end)
function files.get_version()
return files
.read_file(files.version_path)
:map(function(version)
if #version == 40 then
return { sha = version }
else
return { tag = version }
end
end)
:catch(function() return {} end)
end

--- @param version string
--- @return blink.cmp.Task
function files.set_downloaded_version(version)
function files.set_version(version)
return files
.create_dir(files.root_dir .. '/target')
:map(function() return files.create_dir(files.lib_folder) end)
Expand Down
106 changes: 51 additions & 55 deletions lua/blink/cmp/fuzzy/download/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local download_config = require('blink.cmp.config').fuzzy.prebuilt_binaries
local async = require('blink.cmp.lib.async')
local git = require('blink.cmp.fuzzy.download.git')

This comment has been minimized.

Copy link
@JayLCypher

JayLCypher Dec 23, 2024

[REGRESSION]
The file blink/cmp/fuzzy/download/git.lua isn't in the repo yet, causing errors.>
Might have been a little premature there :)

This comment has been minimized.

Copy link
@Saghen

Saghen Dec 23, 2024

Author Owner

Whoops forgot to stage it, thanks!

local files = require('blink.cmp.fuzzy.download.files')
local system = require('blink.cmp.fuzzy.download.system')

Expand All @@ -12,33 +13,56 @@ function download.ensure_downloaded(callback)
if not download_config.download then return callback() end

async.task
.await_all({ download.get_git_tag(), files.get_downloaded_version(), files.is_downloaded() })
:map(
function(results)
return {
git_version = results[1],
version = results[2],
is_downloaded = results[3],
}
.await_all({ git.get_version(), files.get_version() })
:map(function(results)
return {
git = results[1],
current = results[2],
}
end)
:map(function(version)
local target_git_tag = download_config.force_version or version.git.tag

-- not built locally, not on a git tag, error
assert(
version.current.sha ~= nil or target_git_tag ~= nil,
"Can't download from github due to not being on a git tag and no fuzzy.prebuilt_binaries.force_version set, but found no built version of the library. "
.. 'Either run `cargo build --release` via your package manager, switch to a git tag, or set `fuzzy.prebuilt_binaries.force_version` in config. '
.. 'See the docs for more info.'
)

-- built locally, ignore
if
version.current.sha == version.git.sha
or version.current.sha ~= nil and download_config.ignore_version_mismatch
then
return
end
)
:map(function(state)
local target_version = download_config.force_version or state.git_version

-- not built locally, not a git tag, error
if not state.is_downloaded and not target_version then
return callback(
"Can't download from github due to not being on a git tag and no fuzzy.prebuilt_binaries.force_version set, but found no built version of the library. "
.. 'Either run `cargo build --release` via your package manager, switch to a git tag, or set `fuzzy.prebuilt_binaries.force_version` in config. '
.. 'See the README for more info.'

-- built locally but outdated and not on a git tag, error
if version.current.sha ~= nil and version.current.sha ~= version.git.sha then
assert(
target_git_tag or download_config.ignore_version_mismatch,
"Found an outdated version of the fuzzy matching library, but can't download from github due to not being on a git tag. "
.. '\n!! FOR DEVELOPERS !!, set `fuzzy.prebuilt_binaries.ignore_version_mismatch = true` in config. '
.. '\n!! FOR USERS !!, either run `cargo build --release` via your package manager, switch to a git tag, or set `fuzzy.prebuilt_binaries.force_version` in config. '
.. 'See the docs for more info.'
)
if not download_config.ignore_version_mismatch then
vim.schedule(
function()
vim.notify(
'[blink.cmp]: Found an outdated version of the fuzzy matching library built locally',
vim.log.levels.INFO,
{ title = 'blink.cmp' }
)
end
)
end
end

-- built locally, ignore
if state.is_downloaded and (state.version == nil) then return end

-- already downloaded and the correct version, just verify the checksum, and re-download if checksum fails
if state.version == target_version and state.is_downloaded then
if version.current.tag ~= nil and version.current.tag == target_git_tag then
return files.verify_checksum():catch(function(err)
vim.schedule(function()
vim.notify(err, vim.log.levels.WARN, { title = 'blink.cmp' })
Expand All @@ -48,18 +72,18 @@ function download.ensure_downloaded(callback)
{ title = 'blink.cmp' }
)
end)
return download.download(target_version)
return download.download(target_git_tag)
end)
end

-- unknown state
if not target_version then error('Unknown error while getting pre-built binary. Consider re-installing') end
if not target_git_tag then error('Unknown error while getting pre-built binary. Consider re-installing') end

-- download as per usual
vim.schedule(
function() vim.notify('Downloading pre-built binary', vim.log.levels.INFO, { title = 'blink.cmp' }) end
)
return download.download(target_version)
return download.download(target_git_tag)
end)
:map(function() callback() end)
:catch(function(err) callback(err) end)
Expand All @@ -68,38 +92,10 @@ end
function download.download(version)
-- NOTE: we set the version to 'v0.0.0' to avoid a failure causing the pre-built binary being marked as locally built
return files
.set_downloaded_version('v0.0.0')
.set_version('v0.0.0')
:map(function() return download.from_github(version) end)
:map(function() return files.verify_checksum() end)
:map(function() return files.set_downloaded_version(version) end)
end

function download.get_git_tag()
return async.task.new(function(resolve, reject)
-- If repo_dir is nil, no git reposiory is found, similar to `out.code == 128`
local repo_dir = vim.fs.root(files.root_dir, '.git')
if not repo_dir then resolve() end

vim.system({
'git',
'--git-dir',
vim.fs.joinpath(repo_dir, '.git'),
'--work-tree',
repo_dir,
'describe',
'--tags',
'--exact-match',
}, { cwd = files.root_dir }, function(out)
if out.code == 128 then return resolve() end
if out.code ~= 0 then
return reject('While getting git tag, git exited with code ' .. out.code .. ': ' .. out.stderr)
end

local lines = vim.split(out.stdout, '\n')
if not lines[1] then return reject('Expected atleast 1 line of output from git describe') end
return resolve(lines[1])
end)
end)
:map(function() return files.set_version(version) end)
end

--- @param tag string
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function cmp.setup(opts)
config.merge_with(opts)

require('blink.cmp.fuzzy.download').ensure_downloaded(function(err)
if err then error('Error while downloading blink.cmp pre-built binary: ' .. err) end
if err then vim.notify(err, vim.log.levels.ERROR) end

-- setup highlights, keymap, completion and signature help
require('blink.cmp.highlights').setup()
Expand Down

4 comments on commit 3ffd31d

@dpetka2001
Copy link

@dpetka2001 dpetka2001 commented on 3ffd31d Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Saghen
After this commit I get the following error

Failed to run `config` for blink.cmp

...Dev/lazy/blink.cmp/lua/blink/cmp/fuzzy/download/init.lua:3: module 'blink.cmp.fuzzy.download.git' not found:
	no field package.preload['blink.cmp.fuzzy.download.git']
	cache_loader: module 'blink.cmp.fuzzy.download.git' not found
	cache_loader_lib: module 'blink.cmp.fuzzy.download.git' not found
	no file './blink/cmp/fuzzy/download/git.lua'
	no file '/home/runner/work/neovim/neovim/.deps/usr/share/luajit-2.1/blink/cmp/fuzzy/download/git.lua'
	no file '/usr/local/share/lua/5.1/blink/cmp/fuzzy/download/git.lua'
	no file '/usr/local/share/lua/5.1/blink/cmp/fuzzy/download/git/init.lua'
	no file '/home/runner/work/neovim/neovim/.deps/usr/share/lua/5.1/blink/cmp/fuzzy/download/git.lua'
	no file '/home/runner/work/neovim/neovim/.deps/usr/share/lua/5.1/blink/cmp/fuzzy/download/git/init.lua'
	no file './blink/cmp/fuzzy/download/git.so'
	no file '/usr/local/lib/lua/5.1/blink/cmp/fuzzy/download/git.so'
	no file '/home/runner/work/neovim/neovim/.deps/usr/lib/lua/5.1/blink/cmp/fuzzy/download/git.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
	no file './blink.so'
	no file '/usr/local/lib/lua/5.1/blink.so'
	no file '/home/runner/work/neovim/neovim/.deps/usr/lib/lua/5.1/blink.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'

# stacktrace:
  - /blink.cmp/lua/blink/cmp/fuzzy/download/init.lua:3
  - /blink.cmp/lua/blink/cmp/init.lua:20 _in_ **setup**
  - lua/lazyvim/plugins/extras/coding/blink.lua:152 _in_ **config**
  - lua/lazyvim/plugins/lsp/init.lua:181 _in_ **config**
  - vim/_editor.lua:0 _in_ **cmd**
  - /persistence.nvim/lua/persistence/init.lua:88 _in_ **load**
  - lua/:1
  - vim/_editor.lua:0 _in_ **action**
  - ~/projects/plugins/snacks.nvim/lua/snacks/dashboard.lua:684

I use LazyVim and have in my options.lua the following vim.g.lazyvim_blink_main = true which is equivalent to setting version = false and build = "cargo build --release". So, that means I'm on blink main branch currently.

@JayLCypher
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpetka2001 Seems like the addition of git module of fuzzy downloading is prematurely added to init.lua, without accompanying git.lua file.
If you want a working version until fix, I suggest adding

		commit = "129e422175d0e0bd25a0bc32e34455f76b9130ee",

to your blink cmp configuration (somewhere like above build = "cargo build --release",) until the issue is resolved.
This will revert to the most recently working version.

@dpetka2001
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've currently reverted to the commit sha you mentioned as well as a workaround until it gets fixed.

@Saghen
Copy link
Owner Author

@Saghen Saghen commented on 3ffd31d Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, sorry about that folks!

Please sign in to comment.