GnikDroy / projections.nvim

A map to your filesystem
GNU General Public License v3.0
236 stars 8 forks source link

Error executing Lua callback: ...er\start\projections.nvim/lua/projections/validators.lua:14: projections.config: workspaces[1].path - expected string, got nil #25

Closed RichestHumanAlive closed 1 year ago

RichestHumanAlive commented 1 year ago

Basic checklist

Expected vs Actual behavior Expected to see through Telescope list of workspaces I just added using AddWorkspace command (mentioned in the README) But I get an error after trying to show my very first workspace added.

Environment information

To Reproduce Steps to reproduce the behavior:

  1. Open Windows terminal
  2. Change directory to C:\/Users\/USERNAME\/AppData\/Local\/nvim
  3. run nvim command
  4. in neovim, run :AddWorkspace command. Receive notification that C:\/Users\/USERNAME\/AppData\/Local\/nvim was added successfully to workspaces as expected
  5. run :Telescope projections in order to show workspaces
  6. Error shown.

Stack traceback

Error executing Lua callback: ...er\start\projections.nvim/lua/projections/validators.lua:14: projections.config: workspaces[1].path - expected string, got nil
stack traceback:
        [C]: in function 'assert'
        ...er\start\projections.nvim/lua/projections/validators.lua:14: in function 'assert_type'
        ...er\start\projections.nvim/lua/projections/validators.lua:28: in function 'validate_workspaces_table'
        ...ker\start\projections.nvim/lua/projections/workspace.lua:109: in function 'get_workspaces_from_file'
        ...ker\start\projections.nvim/lua/projections/workspace.lua:136: in function 'get_workspaces'
        ...ojections.nvim/lua/telescope/_extensions/projections.lua:10: in function 'project_finder'
        ...ojections.nvim/lua/telescope/_extensions/projections.lua:59: in function <...ojections.nvim/lua/telescope/_extensions/projections.lua:37>
        ...ck\packer\start\telescope.nvim/lua/telescope/command.lua:193: in function 'run_command'
        ...ck\packer\start\telescope.nvim/lua/telescope/command.lua:253: in function 'load_command'
        ...te\pack\packer\start\telescope.nvim\plugin\telescope.lua:108: in function <...te\pack\packer\start\telescope.nvim\plugin\telescope.lua:107>

Thanks for your attention.

GnikDroy commented 1 year ago

My fault. I will overhaul the JSON file format entirely.

This is too ugly to work with.

[{"path":{"path":"C:\/Users\/USERNAME\/AppData\/Local\/nvim"},"patterns":[".git",".svn",".hg"]}]

Should be,

[{"C:\/Users\/USERNAME\/AppData\/Local\/nvim", [".git",".svn",".hg"]}]

or at least,

[{"path": "C:\/Users\/USERNAME\/AppData\/Local\/nvim","patterns":[".git",".svn",".hg"]}]

The format will be updated, and this issue will be solved in the process by tomorrow.

RichestHumanAlive commented 1 year ago

This one looks really intuitive

[{"path": "C:\/Users\/USERNAME\/AppData\/Local\/nvim","patterns":[".git",".svn",".hg"]}]

If had to choose it will be this one.

Thanks

GnikDroy commented 1 year ago

@RichestHumanAlive You are right. That indeed looks the the most idiomatic json.

I have fixed this in workspaces_json_overhaul branch. Can you check if your issue is also fixed? Please, redo the AddWorkspace command or edit the json file appropriately.

Will merge after testing a bit

RichestHumanAlive commented 1 year ago

@GnikDroy I just tried the workspaces_json_overhaul branch but seems still not working fine.

This is how I loaded the target branch using Packer

    use {
      'gnikdroy/projections.nvim', branch = 'workspaces_json_overhaul'
    }

I deleted first the content of the projects file stdpath('data') .. 'projections_workspaces.json to avoid any previous installation impact (as I was not sure if the file will be deleted automatically when Packer uninstall the plugin)

I have achieved the exact same steps :

  1. Open Windows terminal
  2. Change directory to C:\/Users\/USERNAME\/AppData\/Local\/nvim
  3. run nvim command
  4. in neovim, run :AddWorkspace command.
  5. Receive notification that C:\/Users\/USERNAME\/AppData\/Local\/nvim was added successfully to workspaces file as expected.

NOTE: My configs still default. I just Customized the AddWorspace function a bit to be sure that everything was working well at this step.

I kept the most important part Workspace.add(vim.loop.cwd()) untouched.

-- Check if notify plugin is available
local notify_available, notify = pcall(require, "notify")

-- Create AddWorkspace command
local Workspace = require("projections.workspace")

-- Add workspace command
vim.api.nvim_create_user_command(
    "AddWorkspace",
    function()
        Workspace.add(vim.loop.cwd())

        local historyfile = vim.fn.stdpath('data') .. '\\projections_workspaces.json'
        local curr_directory = vim.fn.expand("%:p:h")
        if notify_available then
        notify("[" .. curr_directory .. "] added successfully to history file [" .. historyfile .. "]", "info")
        else
            vim.cmd("!echo " .. curr_directory .. " >> " .. historyfile)
        end
    end,
{})
  1. Check the stdpath('data') .. 'projections_workspaces.json file content:
    [{"patterns":[".git",".svn",".hg"],"path":"C:\/Users\/USERNAME\/AppData\/Local\/nvim"}]

The json structure is exactly as expected.

But...

  1. run :Telescope projections in order to show workspaces
  2. Nothing shown.

image

NOTE : I don't know if by any chance it's related but when I displayed the success message I realized that the path to the configuration file was wrong.

I got C:\Users\WALTEROOT\AppData\Local\nvim-dataprojections_workspaces.json instead of C:\Users\WALTEROOT\AppData\Local\nvim-data\projections_workspaces.json

It was explicitly necessary to add the backslash \\ as you saw in the snippet of code above to have a correct output of the project's file path.

GnikDroy commented 1 year ago

Firstly,

run :Telescope projections in order to show workspaces

:Telescope projections lists projects, not workspaces. So, the above output is possible is no projects are present in the workspace.

It was explicitly necessary to add the backslash \ as you saw in the snippet of code above to have a correct output of the project's file path.

local historyfile = vim.fn.stdpath('data') .. '\\projections_workspaces.json'

This is expected. This is not projections' code, and you need the double backslash to escape the backslash.

The first question is, are there subdirectories of containing ".git", ".svn" and ".hg" in "path":"C:\/Users\/USERNAME\/AppData\/Local\/nvim"? If not, then this is explained.

At least on my machine, there are some treesitter directories that get listed when I follow your instructions.

Maybe try with "patterns": [] to list all subdirectories. And provide me the directory structure in "C:\/Users\/USERNAME\/AppData\/Local\/nvim"

Also, local notify_available, notify = pcall(require, "notify") isn't necessary. You can use vim.notify and the notify plugin will pick it up if available. That has the additional advantage of working with other notify plugin as well, if you someday decide to switch.

So instead of,

        local notify_available, notify = pcall(require, "notify")
        if notify_available then
        notify("[" .. curr_directory .. "] added successfully to history file [" .. historyfile .. "]", "info")
        else
            vim.cmd("!echo " .. curr_directory .. " >> " .. historyfile)
        end

you can directly use vim.notify(...), vim.notify will print with echo! if no handler is set. So the behaviour should be exactly the same.

RichestHumanAlive commented 1 year ago

Followed your instructions and everything works fine now.

Huge Thanks for Tips 💯