Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acessing state in plugin's setup function #703

Closed
dedukun opened this issue Feb 20, 2024 · 6 comments · Fixed by #710
Closed

Acessing state in plugin's setup function #703

dedukun opened this issue Feb 20, 2024 · 6 comments · Fixed by #710
Labels
enhancement New feature or request

Comments

@dedukun
Copy link
Contributor

dedukun commented Feb 20, 2024

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 dedukun added the bug Something isn't working label Feb 20, 2024
@dedukun
Copy link
Contributor Author

dedukun commented Feb 20, 2024

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
Copy link
Owner

sxyazi commented Feb 20, 2024

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

@sxyazi
Copy link
Owner

sxyazi commented Feb 20, 2024

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
Copy link
Contributor Author

dedukun commented Feb 20, 2024

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 sxyazi added enhancement New feature or request and removed bug Something isn't working labels Feb 21, 2024
@sxyazi
Copy link
Owner

sxyazi commented Feb 21, 2024

Hi, I made a PR trying to fix it, #710, let me know if it works for you!

@sibouras sibouras mentioned this issue Mar 10, 2024
59 tasks
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants