sxyazi / yazi

💥 Blazing fast terminal file manager written in Rust, based on async I/O.
https://yazi-rs.github.io
MIT License
15.59k stars 355 forks source link

Acessing `state` in plugin's `setup` function #703

Closed dedukun closed 7 months ago

dedukun commented 7 months ago

What system are you running Yazi on?

Linux Wayland

What terminal are you running Yazi in?

wezterm 20240123-102946-02bdd7ed

Yazi version

yazi 0.2.3 (8efafd6 2024-02-19)

Did you try the latest main branch to see if the problem got fixed?

Tried, but the problem still

Describe the bug

Yazi panics when trying to access the state variable inside a setup function as demonstrated in https://yazi-rs.github.io/docs/plugins/overview#sync-vs-async.

Expected Behavior

It should be possible to set variables in the state so that the plugins can have custom configurations.

To Reproduce

I've created a new branch in bookmarks.yazi which has a simple setup function that tries to set a table in the state variable.

To recreate the issue, simply call the setup method in init.lua

Configuration

No response

Anything else?

I've looked a bit into the lua API and I think that the issue is because the require just runs the lua code without the context of YAZI_PLUGIN_NAME.

A possible solution might be to have a custom require that does something similar to entry.

dedukun commented 7 months ago

This is a bit off topic, but in the example that I gave I'm using print in the setup function instead of ya.err because init.lua is executed before the logs are initialized.

Do you want me to make a PR to fix this?

sxyazi commented 7 months ago

Thank you for the report, this is a known issue, and I am still considering how to address it.

You can use state("your-plugin-name") as a workaround for now:

return {
  setup = function(self, opts) 
    local st = state("your-plugin-name")
    st.foo = "bar"
  end
}

Needs https://github.com/sxyazi/yazi/commit/0cb572a83787a88c8e6ef30aec148d509902a659

sxyazi commented 7 months ago

This is a bit off topic, but in the example that I gave I'm using print in the setup function instead of ya.err because init.lua is executed before the logs are initialized.

Do you want me to make a PR to fix this?

Yeah, please raise a PR for it!

dedukun commented 7 months ago

Thank you for the report, this is a known issue, and I am still considering how to address it.

You can use state("your-plugin-name") as a workaround for now:

return {
  setup = function(self, opts) 
    local st = state("your-plugin-name")
    st.foo = "bar"
  end
}

Needs 0cb572a

Cool, thanks for the quick workaround, I'll use it in the mean time.

sxyazi commented 7 months ago

Hi, I made a PR trying to fix it, https://github.com/sxyazi/yazi/pull/710, let me know if it works for you!

github-actions[bot] commented 6 months ago

I'm going to lock this issue because it has been closed for 30 days. ⏳ This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.