rktjmp / hotpot.nvim

:stew: Carl Weathers #1 Neovim Plugin.
MIT License
358 stars 9 forks source link

unknown identifier in strict mode: vim #82

Closed monkoose closed 2 years ago

monkoose commented 2 years ago

Trying to convert from aniseed to hotpot. And got a lot of such errors, which are happens inside (macro ...) blocks.

I definitely doing something wrong, but with aniseed it have worked. An now with hotpot i can't use any globals inside macros. My macro definitition just split throw config, not in some specific file. Currently my conf/maps.fnl is cause of the errors. Have spend a lot of time to fix this issue without any success.

You can find my config for hotpot branch (it's currently a mess) here https://github.com/monkoose/neovim-fennel/tree/hotpot Starting point is init.lua and then conf/init_fnl.fnl Not an lisp/fennel expert so my macros are ugly too (wanted to make them do too much, will simplify them after fix to this issue).

Only have found allowedGlobals = false compiler option as a workaround for strict mode (but it doesn't help for me). Any help? Should i split macros into different file? Where to put it and how to name it?

Additional info OS: ArchLinux neovim version: 0.7.2 from oficial archlinux 'community' repository

init.lua

require("hotpot").setup()
require("fnl_init")

fnl/fnl_init.fnl

(import-macros {: strict} :macros)

(local pack (require "packer"))

(fn load-plugins [use]
  (use "wbthomason/packer.nvim")
  (use "rktjmp/hotpot.nvim"))

(pack.startup load-plugins)

(strict)

fnl/macros.fnl

(fn strict [arg]
  (vim.pretty_print "test"))

{: strict}

This minimal neovim config gives such error to me

Error detected while processing /home/monkoose/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /home/monkoose/.config/nvim/init.lua:2: module 'fnl_init' not found:Compile error in /home/monkoose/.config/nvim/
fnl/fnl_init.fnl:1:0
  Compile error in /home/monkoose/.config/nvim/fnl/macros.fnl:2:3
  unknown identifier in strict mode: vim

  (^[[7mvim.pretty_print^[[0m "test"))
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.vim if you really want a global.
* Try moving this code to somewhere that vim is in scope.
* Try binding vim as a local in the scope of this code.

^[[7m(import-macros {: strict} :macros)^[[0m
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.vim

  (^[[7mvim.pretty_print^[[0m "test"))
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.vim if you really want a global.
* Try moving this code to somewhere that vim is in scope.
* Try binding vim as a local in the scope of this code. if you really want a global.
* Try moving this code to somewhere that vim

  (^[[7mvim.pretty_print^[[0m "test"))
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.vim if you really want a global.
* Try moving this code to somewhere that vim is in scope.
* Try binding vim as a local in the scope of this code. is in scope.
* Try binding vim
  (^[[7mvim.pretty_print^[[0m "test"))
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.vim if you really want a global.
* Try moving this code to somewhere that vim is in scope.
* Try binding vim as a local in the scope of this code. as a local in the scope of this code.
        no field package.preload['fnl_init']
        no file '/usr/share/lua/5.4/fnl_init.lua'
        no file '/usr/share/lua/5.4/fnl_init/init.lua'
        no file '/usr/lib/lua/5.4/fnl_init.lua'
        no file '/usr/lib/lua/5.4/fnl_init/init.lua'
        no file './fnl_init.lua'
        no file './fnl_init/init.lua'
        no file '/home/monkoose/.luarocks/share/lua/5.4/fnl_init.lua'
        no file '/home/monkoose/.luarocks/share/lua/5.4/fnl_init/init.lua'
        no file './fnl_init.lua'
        no file '/usr/share/luajit-2.1.0-beta3/fnl_init.lua'
        no file '/usr/local/share/lua/5.1/fnl_init.lua'
        no file '/usr/local/share/lua/5.1/fnl_init/init.lua'
        no file '/usr/share/lua/5.1/fnl_init.lua'
        no file '/usr/share/lua/5.1/fnl_init/init.lua'
        no file '/usr/lib/lua/5.4/fnl_init.so'
        no file '/usr/lib/lua/5.4/loadall.so'
        no file './fnl_init.so'
        no file '/home/monkoose/.luarocks/lib/lua/5.4/fnl_init.so'
        no file './fnl_init.so'
        no file '/usr/local/lib/lua/5.1/fnl_init.so'
        no file '/usr/lib/lua/5.1/fnl_init.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        /home/monkoose/.config/nvim/init.lua:2: in main chunk
bangedorrunt commented 2 years ago

@monkoose try to put your macro inside maps.fnl into a macros.fnl file, then in maps.fnl using (import-macros {: set-map! : map!} :conf.macros) for example

Off topic but your map! macro makes me miss my old doom-emacs :)

monkoose commented 2 years ago

@babygau didn't help. Have updated the repository you can check how i did it.

Off topic but your map! macro makes me miss my old doom-emacs :)

Will change them anyway (too complex especially for me who are not expert)

bangedorrunt commented 2 years ago

@monkoose what is error? I see you didn't export your macro properly, macros.fnl should have

{: set-map!
 : map!}
bangedorrunt commented 2 years ago

@babygau didn't help. Have updated the repository you can check how i did it.

Off topic but your map! macro makes me miss my old doom-emacs :)

Will change them anyway (too complex especially for me who are not expert)

Highly recommend this for your reference, easier to grok and make your own https://github.com/datwaft/themis.nvim

monkoose commented 2 years ago

I have imported map!. set-map! is helper macro for map! should i import it too?

It's the same error

Error detected while processing /home/monkoose/.config/nvim/init.lua:
E5113: Error while calling lua chunk: module 'conf.maps' not found:Compile error in /home/monkoose/.config/nvim/fnl/conf/maps.fnl:1:0
  Compile error in /home/monkoose/.config/nvim/fnl/conf/macros.fnl:2:15
  unknown identifier in strict mode: vim

  (local opts (^[[7mvim.tbl_extend^[[0m "force" {} (or opts {})))
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.vim if you really want a global.
* Try moving this code to somewhere that vim is in scope.
* Try binding vim as a local in the scope of this code.
monkoose commented 2 years ago

Highly recommend this for your reference, easier to grok and make your own https://github.com/datwaft/themis.nvim

Yes i saw it. I took inspiration from his config when i switched to hotpot.

I don't get it either. I don't see anything specific that he done (he just import macros from this plugin). And this plugin is just collection of macros not even with some specifc filename as macros.fnl. :(

bangedorrunt commented 2 years ago

First, could you tried to use _G.vim... to see if the error is fixed? Secondly, tried remove lua folder, and let hotpot rebuild it again. Sometimes the error you see is just from old cache file.

bangedorrunt commented 2 years ago

Yes i saw it. I took inspiration from his config when i switched to hotpot.I don't get it either. I don't see anything specific that he done (he just import macros from this plugin). And this plugin is just collection of macros not even with some specifc filename as macros.fnl. :(

Hotpot will not compile files end with *macros.fnl hence we can split macros into multiple files.

You can also checkout my macros for reference, a shameless copy from his config at https://github.com/babygau/nix/tree/tdt/dotfiles/nvim

monkoose commented 2 years ago

_G.vim is a help (but then i need to rename every global like so in macros`. I prefer to find what is causing such issue. Edit: It gives another error

Error detected while processing /home/monkoose/.config/nvim/init.lua:
E5113: Error while calling lua chunk: module 'conf.maps' not found:Compile error in /home/monkoose/.config/nvim/fnl/conf/maps.fnl:3:0
  /home/monkoose/.config/nvim/fnl/conf/macros.fnl:41: attempt to index field 'vim' (a nil value)
stack traceback:
        /home/monkoose/.config/nvim/fnl/conf/macros.fnl:41: in function 'parse_map_args'

For some reason config dir doesn't have any lua folder in my config. Cleared .cache/nvim/hotpot but issue is still present.

bangedorrunt commented 2 years ago

I agree, it should not a problem. Could you remove this

compiler = {
      modules = {
        allowedGlobals = false,
      },

I don't see that option in Hotpot API

For some reason config dir doesn't have any lua folder in my config.

Possibly, you put lua in gitignore :D

monkoose commented 2 years ago

Possibly, you put lua in gitignore :D

I'm definitely stupid, but not so much :) Have checked hidden files too. Check post above i updated it.

Could you remove this

I could, but it just countless update to init.lua with some changes (and previously it wasn't here without any success). Will try it anyway.

bangedorrunt commented 2 years ago

Sorry that I can't help much. Actually I had that issue long time ago, especially when trying fennel-cljlib. But it should not be your issue now. You could pull datwaft repo and test if the prob still persists (well, with your system :|).

monkoose commented 2 years ago

Sorry that I can't help much. Actually I had that issue long time ago, especially when trying fennel-cljlib. But it should not be your issue now. You could pull datwaft repo and test if the prob still persists (well, with your system :|).

Ok will do it now. Anyway have no idea why lua/ dir is missing. But it is still like half working my config i still get completions from cmp and mappings from it config works too. Weird.

monkoose commented 2 years ago

I got some errors from his config here and there (especially some CursorHold events), but it is working and at least it is not a fennel compiler issues. So there are no unknown identifier in strict mode problems :(

bangedorrunt commented 2 years ago

So at least we know it's not your system issue, sth strange in your config btw ^ ^ Do you see Lua folder? I'm curious :D

monkoose commented 2 years ago

So at least we know it's not your system issue, sth strange in your config btw ^ ^ Do you see Lua folder? I'm curious :D

Yes have found it, lua files are actually are in .cache/nvim/hotpot directory. Which i previously deleted without any fix to the issue.

Seems like i should try some minimal init.vim with simple macro that uses vim to try to remove man in the middle which is my config. But will do it later, my head is exploding right now after 5+ hours of trying to find a solution.

bangedorrunt commented 2 years ago

So at least we know it's not your system issue, sth strange in your config btw ^ ^ Do you see Lua folder? I'm curious :D

Yes have found it, lua files are actually are in .cache/nvim/hotpot directory. Which i previously deleted without any fix to the issue.

Seems like i should try some minimal init.vim with simple macro that uses vim to try to remove man in the middle which is my config.

That's great idea, hopefully you can tell me where the prob is . I'm curious as hell :D

monkoose commented 2 years ago

Have updated issue message. With that minimal neovim config i still have error.

Hate this.

rktjmp commented 2 years ago

Guessing:

You need to set :compilerEnv _G to stop global checking in the macro compiler.

https://fennel-lang.org/api

compilerEnv: an environment table in which to run compiler-scoped code for macro definitions and eval-compiler calls. Internal Fennel functions such as list, sym, etc. will be exposed in addition to this table. Defaults to a table containing limited known-safe globals. Pass _G to disable sandboxing.

  (setup {:provide_require_fennel true
          :compiler {:modules {:correlate true}
                     :macros {:env :_COMPILER
                              :compilerEnv _G
                              :allowedGlobals false}}})

https://github.com/rktjmp/hotpot.nvim/issues/22#issuecomment-916148788

monkoose commented 2 years ago

Let me try with minimal config, but i have tried this setting before with no result. I literally tried like almost every line from your cookbook.

Updated init.lua to

require("hotpot").setup({
  provide_require_fennel = true,
  compiler = {
    macros = {
      env = "_Compiler",
      compilerEnv = _G,
      allowedGlobals = false,
    }
  }
})
require("fnl_init")

And the error. It it is the same error if i manually prepend vim. in macro with _G.

Error detected while processing /home/monkoose/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /home/monkoose/.config/nvim/init.lua:11: module 'fnl_init' not found:Compile error in /home/monkoose/.config/nvim
/fnl/fnl_init.fnl:11:0
  /home/monkoose/.config/nvim/fnl/macros.fnl:2: attempt to index global 'vim' (a nil value)
stack traceback:
        /home/monkoose/.config/nvim/fnl/macros.fnl:2: in function </home/monkoose/.config/nvim/fnl/macros.fnl:1>
        [C]: in function 'xpcall'
        ...site/pack/packer/start/hotpot.nvim/lua/hotpot/fennel.lua:2746: in function 'macroexpand_2a'
        ...site/pack/packer/start/hotpot.nvim/lua/hotpot/fennel.lua:2933: in function 'compile1'
        ...site/pack/packer/start/hotpot.nvim/lua/hotpot/fennel.lua:3200: in function 'compile-string'
        ...te/pack/packer/start/hotpot.nvim/lua/hotpot/compiler.lua:26: in function <...te/pack/packer/start/hotpot.nvim/lua/hotpot/compiler.lua:24>
        [C]: in function 'compile_string'
        ...te/pack/packer/start/hotpot.nvim/lua/hotpot/compiler.lua:51: in function <...te/pack/packer/start/hotpot.nvim/lua/hotpot/compiler.lua:37>
        [C]: in function 'compile_file'
        .../packer/start/hotpot.nvim/lua/hotpot/searcher/module.lua:76: in function 'compile_fnl'
        .../packer/start/hotpot.nvim/lua/hotpot/searcher/module.lua:91: in function 'create_loader_fn'
        .../packer/start/hotpot.nvim/lua/hotpot/searcher/module.lua:102: in function 'searcher'
        .../site/pack/packer/start/hotpot.nvim/lua/hotpot/index.lua:110: in function 'search_index'
        .../site/pack/packer/start/hotpot.nvim/lua/hotpot/index.lua:124: in function <.../site/pack/packer/start/hotpot.nvim/lua/hotpot/index.lua:123>
        [C]: in function 'require'
        /home/monkoose/.config/nvim/init.lua:11: in main chunk
^[[7m(strict)^[[0m
        no field package.preload['fnl_init']
        no file '/usr/share/lua/5.4/fnl_init.lua'
        no file '/usr/share/lua/5.4/fnl_init/init.lua'
        no file '/usr/lib/lua/5.4/fnl_init.lua'
        no file '/usr/lib/lua/5.4/fnl_init/init.lua'
        no file './fnl_init.lua'
        no file './fnl_init/init.lua'
        no file '/home/monkoose/.luarocks/share/lua/5.4/fnl_init.lua'
        no file '/home/monkoose/.luarocks/share/lua/5.4/fnl_init/init.lua'
        no file './fnl_init.lua'
        no file '/usr/share/luajit-2.1.0-beta3/fnl_init.lua'
        no file '/usr/local/share/lua/5.1/fnl_init.lua'
        no file '/usr/local/share/lua/5.1/fnl_init/init.lua'
        no file '/usr/share/lua/5.1/fnl_init.lua'
        no file '/usr/share/lua/5.1/fnl_init/init.lua'
        no file '/usr/lib/lua/5.4/fnl_init.so'
        no file '/usr/lib/lua/5.4/loadall.so'
        no file './fnl_init.so'
        no file '/home/monkoose/.luarocks/lib/lua/5.4/fnl_init.so'
        no file './fnl_init.so'
        no file '/usr/local/lib/lua/5.1/fnl_init.so'
        no file '/usr/lib/lua/5.1/fnl_init.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        /home/monkoose/.config/nvim/init.lua:11: in main chunk
bangedorrunt commented 2 years ago

@monkoose This is not a macro, but a normal function.

(fn strict [arg]
  (vim.pretty_print "test"))

{: strict}

Fennel doesn't know vim.pretty_print at compile time. Pls test with

(fn strict [arg]
  `(vim.pretty_print "test"))

{: strict}
rktjmp commented 2 years ago

https://github.com/rktjmp/hotpot-issue-82

Note that "test" is only output at compile time, so when you first run nvim in the container, or if you edit the file and relaunch.

rktjmp commented 2 years ago
  env = "_Compiler",

Must be _COMPILER

monkoose commented 2 years ago
  env = "_Compiler",

Must be _COMPILER

Ok this works. Thank you. I do remember i tried uppercase with mine config.

rktjmp commented 2 years ago

Unsure what your intention is, that function is different to what you have in your first post:

;; inject the code (vim.pretty_print "test") into the call site
(fn strict [arg]
  `(vim.pretty_print "test"))n

;; run the call (vim.pretty_print "test) _at compile time_
(fn strict2 [arg]
  (vim.pretty_print "test"))
monkoose commented 2 years ago

Fennel doesn't know vim.pretty_print at compile time.

Seems with the setting from @rktjmp it still works fine. But if i add backquote and remove config it works too.

monkoose commented 2 years ago

Unsure what your intention is, that function is different to what you have in your first post:

Yes. Thank you. I do get it now.

monkoose commented 2 years ago

@rktjmp @babygau Thank you both for the help. Test with minimal config was the thing. Should have done it much earlier.

rktjmp commented 2 years ago

See the link I posted to the Fennel API doc, by default only a few functions are available at compile time (so when building macros). By setting compilerEnv to _G, all functions (well anything in the global scope) are available to be called at compile time. This is a sort-of safety feature of Fennel upstream.

You should be able to look at Aniseeds compiler settings to see what they use, Hotpot should be 100% compatible as long as they have not patched fennel somehow.

Glad you sorted it out, thanks @babygau for lending a hand.

monkoose commented 2 years ago

Now my config works even with my weird macros. :smile:

monkoose commented 2 years ago

@rktjmp one more question while you are here. I want to build some addition dir with AOT. Have this code (but it again error with undefined in strict ....)

  require("hotpot.api.make").build(
    config_dir,
    {
      verbosity = 0,
      compiler = {
        macros = {
          env = "_COMPILER",
          compilerEnv = _G,
          allowedGlobals = false,
        },
      },
    },
    config_dir .. "/colors/half.fnl",
    function(path)
      return path
    end
  )

Do i miss something?

rktjmp commented 2 years ago

Hmm, it looks OK. The make stuff is a bit weird with how we have to juggle API options, possibly it's not getting passed down. Can you post it to another issue and I will try to look at it tonight.

monkoose commented 2 years ago

Hmm, it looks OK. The make stuff is a bit weird with how we have to juggle API options, possibly it's not getting passed down. Can you post it to another issue and I will try to look at it tonight.

let me try few more things before that. Maybe I will find the solution.

monkoose commented 2 years ago

Fixed it. My colors/colorscheme.fnl once again had (macro ... form. After moving it into macros.fnl now it works. Seems like even with such additional options macros must be in a different file. And seems like aniseed has no problems with that.

bangedorrunt commented 2 years ago

@rktjmp might related https://github.com/rktjmp/hotpot.nvim/discussions/81