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

feat(core): If a plugin is not enabled, ignore all specs in its dependencies #1000

Closed
wants to merge 7 commits into from
Closed

feat(core): If a plugin is not enabled, ignore all specs in its dependencies #1000

wants to merge 7 commits into from

Conversation

abeldekat
Copy link
Contributor

Example scenario:

return {
  {
    "A",
    opts = {
      foo = true,
    },
  },
  {
    "B",
    enabled = false,
    dependencies = {
      {
        "A",
        keys = {
          { "<leader>ab", function() require("A").needing_B() end, desc = "AB" }
        }
        opts = {
          bar = true,
        },
      },
    },
  },
}

Plugin B is disabled.
However, both definitions of A are merged into the final A:

  • A.opts == { foo = true, bar = true }
  • key <leader>ab is present, and will fail when invoked, on the absence of B.

Note that currently, when A would only exist as a dependency of B, plugin A would have been disabled as well.

The configuration of a plugin can depend on the presence of another plugin.
A good example is the conditional definition of a key.
Currently, extra code is required, checking for the presence of a plugin using programmatic "guards".

This PR ensures that plugin A only contains the definition provided by plugin B, if B is enabled.
The result becomes:

  • A.opts == { foo = true }
  • key <leader>ab is not available.

An opinion on the benefits

  • it's easier to configure specs on a need to have basis
  • specs are easier to reason about

An opinion on the implications

This PR will most likely not break existing personal configurations.
Users might have expected the behaviour this PR aims for to already be in effect.

Distributions however are highly modularized by nature. Maintainers are advised to inspect the code.
Consider a definition of A inside B, containing a property that should always be present on A regardless of the status of B.

Breaking example:

return {
  {
    "A",
    opts = {
      foo = true,
    },
  },
  {
    "B",
    enabled = false,
    dependencies = {
      {
        "A",
        opts = {
          ALWAYS_NEEDED = true, -- BREAKING, not present anymore!
        },
      },
    },
  },
}

In this case, it would make sense to move the configuration of property ALWAYS_NEEDED to the top-level spec of plugin A.

Approach

This PR changes the Plugin.parse method, after all specs have been normalized.
An enabled plugin will be repaired when it contains a spec originating from the dependencies property of a plugin that is not enabled.

The enabled plugin can be repaired by redoing its merge operation, using a filter on its individual plugin definitions.

I did not observe a noticeable decline in performance.
When the user disables many plugins that have specs in their dependencies, some performance overhead is expected.

Additional context

AstroNvim's lead developer proposed the idea in this discussion. Thanks @mehalter, for all the support!

A scenario illustrating the effect of the proposed changes is discussed in the code review of the following PR: enable telescope extension of nvim-notify.

Worth mentioning: An explanation on how dependencies are loaded, written by Folke in this issue.

While investigating existing issues, I found an issue from @UtkarshVerma, also addressing the topic: Don't process dependencies if spec is disabled.

abeldekat added 6 commits July 31, 2023 20:24
Plugins that are unused currently can still contribute to
active plugins by the specs added to their "dependencies" field.

This commit adds functionality to the "parse" method,
repairing only the active plugins that could be affected by the
aforementioned specs.

This can be achieved by maintaining an extra administration table
containing a copy of each added plugin instance.
finetuning: the algorithm marking an active plugin as dirty
performance: instead of vim.tbl_merge, use vim.list_extend
act directly while looping, avoiding the vim.list_extend calls.
@folke
Copy link
Owner

folke commented Sep 29, 2023

Fixed by #1058

@folke folke closed this Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants