Skip to content

[BUG]: Plugin Registering Twice #6596

Closed

Description

@EvanZhouDev

Description

If you make a new plugin that has a session.idle hook, the plugin appears to be registering twice, with the session.idle hook being called twice.

OpenCode version

1.0.223

Steps to reproduce

Make a plugin like this:

export const MyPlugin: Plugin = async (ctx: PluginInput) => {
        let times = 0;
        return {
		event: async ({ event }) => {
			if (event.type === "session.idle") {
				console.log("Hello" + times++);
			}
		},
	};
}

You will observe the logs happening twice:

Hello0
Hello0

However, now move the declaration of times outside the plugin:
let times = 0;

export const MyPlugin: Plugin = async (ctx: PluginInput) => {
        return {
		event: async ({ event }) => {
			if (event.type === "session.idle") {
				console.log("Hello" + times++);
			}
		},
	};
}

You will now observe logs as such:

Hello0
Hello1

For one, this is unexpected because the event listener seems to be triggering twice.
But based off these logs, it seems like the plugin itself is registering twice?

Activity

  1. github-actions commented on Jan 1, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    This appears to be related to a broader plugin event firing issue where events are being triggered multiple times instead of just once. Feel free to ignore if this is a different scenario.

  2. arsham commented on Jan 1, 2026

    @arsham

    Check this in two places:

    • In the configuration directory
    • Somewhere else

    I had this before and it seems that in the configuration it loads them twice, once for it being global config and once for the project local. If that is the case, you can use this trick to avoid loading the plugin twice:

    let IS_MYPLUGIN_REGISTERED = false;
    
    export const Myplugin: Plugin = async ({ client, directory }) => {
      // Prevent double-registration when plugin loads from both global + local config
      if (IS_MYPLUGIN_REGISTERED) {
        return {};
      }
      IS_MYPLUGIN_REGISTERED = true;
    }
  3. EvanZhouDev commented on Jan 1, 2026

    @EvanZhouDev
    Author

    That's essentially what I ended up doing... but in my case I did check that my configuration only loads it once.

  4. rekram1-node commented on Jan 1, 2026

    @rekram1-node
    Contributor

    @EvanZhouDev could be happening if the session has a subagent? Subagent sessions also have idle events

  5. EvanZhouDev commented on Jan 1, 2026

    @EvanZhouDev
    Author

    I think I'm doing this weird thing with Git worktrees for my OpenCode config which is perhaps causing this but I think I checked all viable configuration files and it is only loading it once... I don't have a subagent.

    Edit: In addition, my plugin is only existent in my current worktree, so it shouldn't be registering twice because of that.

    Would it make sense to make it by default only load a plugin once so that this doesn't happen? And then maybe add an explicit flag that allows loading multiple times?

  6. added a commit that references this issue on Jan 3, 2026
  7. ShpetimA commented on Jan 3, 2026

    @ShpetimA
    Contributor

    @EvanZhouDev @rekram1-node I ran into the same issue while developing a plugin found the bug created pr feel free to check it out

  8. ShpetimA commented on Jan 4, 2026

    @ShpetimA
    Contributor

    Btw @arsham and @EvanZhouDev you can also fix it without updating opencode by just having one export for your plugin
    export const YourPlugin
    either
    export default YourPlugin

    if you have both this issue happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions