bakpakin / Fennel

Lua Lisp Language
https://fennel-lang.org
MIT License
2.45k stars 126 forks source link

global compile-time variable used in a macro doesn't work/ compiled to nil? #413

Closed shaunsingh closed 2 years ago

shaunsingh commented 2 years ago

I have a use-package! macro that is supposed to add packages to a global list (conf/pack) which is then sent to packer. However, unless all the use-package! calls are in the same file as the unpack! macro, it doesn't work. Is this intended behavior?

https://github.com/shaunsingh/nyoom.nvim/blob/ed2dde4ecaac4c038ef9c2bff14f8ff885b2216b/fnl/conf/macros.fnl#L96 to Line 156 for the macros I used.

See: https://github.com/datwaft/nvim.conf/issues/15 for examples/test

technomancy commented 2 years ago

It's hard to tell what's going on here; a lot depends on how the compiler sandbox is set up. Can you provide a repro case that can run outside neovim?

shaunsingh commented 2 years ago

Here's a repro of the issue: https://github.com/shaunsingh/fennel413

Result

~/P/t/fennel413 λ fennel init.fnl
The module 'init' has been loaded.

It should print both that the module init and the module module has been loaded

technomancy commented 2 years ago

The reason you're getting unexpected results here is just that the compilation is happening in a different order from what you're expecting.

There are a couple possible fixes here. One would be that you replace require (which defers the compilation to runtime) with include which compiles it immediately. (The --require-as-include flag also has this effect.) Another would be that you change the loop so that it looks for the list of modules at runtime instead of compile time; however, the compiler sandbox will interfere with this, so you will either have to disable it or construct a separate sandbox which includes the table containing the data you want.

Or maybe I'm barking up the wrong tree entirely. If you just want to track what modules get loaded, you don't need a macro for that; a custom searcher would be a lot easier way to accomplish it.

shaunsingh commented 2 years ago

There are a couple possible fixes here. One would be that you replace require (which defers the compilation to runtime) with include which compiles it immediately.

Thanks, I'll look into that

Or maybe I'm barking up the wrong tree entirely. If you just want to track what modules get loaded, you don't need a macro for that; a custom searcher would be a lot easier way to accomplish it.

That was just an example, the actual use case was adding plugins across multiple files in neovim to my package manager (via adding the packages to a list using a macro, then sending that to `packer.nvim)

technomancy commented 2 years ago

OK cool. Going to close this as it doesn't seem like a bug, but if you have more questions feel free to comment further.