projekt0n / github-nvim-theme

Github's Neovim themes
MIT License
2.02k stars 104 forks source link

fix(compiler): compile all themes inst of 1 mult times #290

Closed tmillr closed 5 months ago

tmillr commented 11 months ago

Fix bug where the current theme gets compiled multiple times instead of compiling all themes.

tmillr commented 11 months ago

I had to make this a draft because there seems to be negative side-effects that were either produced, or simply made visible, by this pr. For example (airline highlights changed):

Screenshot 2023-08-01 at 1 13 49 AM



I need to investigate this further but it might have something to do with the color lib? Not sure. Lmk if you have any ideas. It seems that the order in which the themes are compiled might matter (due to some unforeseen side-effects of compilation).

Edit: Yep, just confirmed that it's sensitive to compilation order. So this is easily fixed for the time being by simply compiling the currently-set theme last. There's obviously some relevant global/persistent state somewhere that is being set or altered during compilation causing side-effects. This isn't optimal (and this global state should probably be refactored out in the future if possible), but perhaps it can be worked-around for now? I think that either airline, or the color module, is having its state altered during compilation (for example, the Color lib/module has persistent/global values such as Color.BG).


Explanation of the bug

https://github.com/projekt0n/github-nvim-theme/blob/796ecdd3ad215df5cb2968400d3d947e3d4efe9a/lua/github-theme/init.lua#L101-L104

  1. Within setup() in lua/github-theme/init.lua, when a compilation is triggered, M.compile() is called.


https://github.com/projekt0n/github-nvim-theme/blob/796ecdd3ad215df5cb2968400d3d947e3d4efe9a/lua/github-theme/init.lua#L27-L35

  1. M.compile() loops over all of the themes, calling compiler.compile({ style = style }) on each one.


https://github.com/projekt0n/github-nvim-theme/blob/796ecdd3ad215df5cb2968400d3d947e3d4efe9a/lua/github-theme/lib/compiler.lua#L25-L105

  1. Nowhere within the body of compiler.compile(opts) is opts.style ever referenced; instead, config.theme is used as the theme to compile (which as I understand is simply the currently-set theme).


The implications of all of this is that whenever a compilation is triggered and M.compile() in lua/github-theme/init.lua gets called, the very same theme gets compiled and written to the filesystem (at the same path) multiple/many times in a row.

Solution

I'd suggest either fixing this to compile all of the different themes/styles (which appears to be the original intention), or perhaps changing it to compile just 1 theme (the currently-set theme) with others only being compiled impromptu on an as-needed basis. This pr currently attempts to do the former.

tmillr commented 10 months ago

Regarding the sensitivity to compilation order issue, this line appears to be the offending line:

https://github.com/projekt0n/github-nvim-theme/blob/806903c1b66a6b29347871922acd7d830a9d5c6a/lua/github-theme/util/airline.lua#L7

in particular, the value of s.bg0 varies.

So it's probably something in spec.lua or palette.lua (and may still have something to do with the global/persistent state of the color lib).