From b7b4639e973ab8e5d5da4d3ce7eb13bda4a17f04 Mon Sep 17 00:00:00 2001 From: arrow2nd <44780846+arrow2nd@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:17:08 +0900 Subject: [PATCH] fix: resolve "no such file or directory" error in scratchpad (#254) * fix(parser): resolve "no such file or directory" error in scratchpad * fix(scratchpad): use vim.loop.cwd() for script.cwd * fix(scratchpad): also handle external files --------- Co-authored-by: Marco Kellershoff --- lua/kulala/globals/init.lua | 2 +- lua/kulala/parser/scripts/engines/javascript/init.lua | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index cf9381f..daa5212 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -4,7 +4,7 @@ local M = {} local plugin_tmp_dir = FS.get_plugin_tmp_dir() -M.VERSION = "4.0.0" +M.VERSION = "4.0.1" M.UI_ID = "kulala://ui" M.SCRATCHPAD_ID = "kulala://scratchpad" M.HEADERS_FILE = plugin_tmp_dir .. "/headers.txt" diff --git a/lua/kulala/parser/scripts/engines/javascript/init.lua b/lua/kulala/parser/scripts/engines/javascript/init.lua index 90e057b..130c813 100644 --- a/lua/kulala/parser/scripts/engines/javascript/init.lua +++ b/lua/kulala/parser/scripts/engines/javascript/init.lua @@ -50,17 +50,21 @@ local generate_one = function(script_type, is_external_file, script_data) return nil, nil end local script_cwd + -- buf_dir is "kulala:" when the buffer is scratch buffer + -- in this case, use current working directory for script_cwd and base_dir + local buf_dir = FS.get_current_buffer_dir() if is_external_file then -- if script_data starts with ./ or ../, it is a relative path if string.match(script_data, "^%./") or string.match(script_data, "^%../") then local local_script_path = script_data:gsub("^%./", "") - script_data = FS.join_paths(FS.get_current_buffer_dir(), local_script_path) + local base_dir = buf_dir == "kulala:" and vim.loop.cwd() or buf_dir + script_data = FS.join_paths(base_dir, local_script_path) end - script_cwd = FS.get_dir_by_filepath(script_data) + script_cwd = buf_dir == "kulala:" and vim.loop.cwd() or FS.get_dir_by_filepath(script_data) userscript = FS.read_file(script_data) else - script_cwd = FS.get_current_buffer_dir() + script_cwd = buf_dir == "kulala:" and vim.loop.cwd() or buf_dir userscript = vim.fn.join(script_data, "\n") end base_file = base_file .. "\n" .. userscript