Skip to content

Commit

Permalink
fix: issue where non-core plugins showed up in core plugins (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 31, 2024
1 parent 3e4d019 commit dd6156b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
13 changes: 5 additions & 8 deletions src/ape/managers/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from ape.exceptions import ApeAttributeError
from ape.logging import logger
from ape.plugins._utils import CORE_PLUGINS, _filter_plugins_from_dists, clean_plugin_name
from ape.plugins._utils import CORE_PLUGINS, clean_plugin_name, get_plugin_dists
from ape.plugins.pluggy_patch import plugin_manager as pluggy_manager
from ape.utils.basemodel import _assert_not_ipython_check, only_raise_attribute_error
from ape.utils.misc import _get_distributions, log_instead_of_fail
from ape.utils.misc import log_instead_of_fail


def valid_impl(api_class: Any) -> bool:
Expand Down Expand Up @@ -122,18 +122,15 @@ def _register_plugins(self):
if self.__registered:
return

plugins = list(
{n.replace("-", "_") for n in _filter_plugins_from_dists(_get_distributions())}
)
locals = [p for p in CORE_PLUGINS if p != "ape"]
plugin_modules = tuple([*plugins, *locals])
plugins = list({n.replace("-", "_") for n in get_plugin_dists()})
plugin_modules = tuple([*plugins, *CORE_PLUGINS])

for module_name in plugin_modules:
try:
module = importlib.import_module(module_name)
pluggy_manager.register(module)
except Exception as err:
if module_name in CORE_PLUGINS:
if module_name in CORE_PLUGINS or module_name == "ape":
# Always raise core plugin registration errors.
raise

Expand Down
25 changes: 17 additions & 8 deletions src/ape/plugins/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from collections.abc import Iterable, Iterator, Sequence
from enum import Enum
from functools import cached_property
from pathlib import Path
from shutil import which
from typing import Any, Optional

Expand All @@ -25,19 +24,29 @@
PIP_COMMAND = ["uv", "pip"] if which("uv") else [sys.executable, "-m", "pip"]
PLUGIN_PATTERN = re.compile(r"\bape_\w+(?!\S)")
CORE_PLUGINS = [
"ape",
*[
f.name
for f in Path(__file__).parent.parent.parent.iterdir()
if f.name.startswith("ape_") and f.is_dir() and re.match(PLUGIN_PATTERN, f.name)
],
"ape_accounts",
"ape_cache",
"ape_compile",
"ape_console",
"ape_ethereum",
"ape_node",
"ape_init",
"ape_networks",
"ape_plugins",
"ape_pm",
"ape_run",
"ape_test",
]


def clean_plugin_name(name: str) -> str:
return name.replace("_", "-").replace("ape-", "")


def get_plugin_dists():
return _filter_plugins_from_dists(_get_distributions())


def _filter_plugins_from_dists(dists: Iterable) -> Iterator[str]:
for dist in dists:
if name := getattr(dist, "name", ""):
Expand Down Expand Up @@ -384,7 +393,7 @@ def check_installed(self, use_cache: bool = True) -> bool:
if not use_cache:
_get_distributions.cache_clear()

return any(n == self.package_name for n in _filter_plugins_from_dists(_get_distributions()))
return any(n == self.package_name for n in get_plugin_dists())

def _prepare_install(
self, upgrade: bool = False, skip_confirmation: bool = False
Expand Down
6 changes: 3 additions & 3 deletions src/ape_plugins/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
PluginMetadata,
PluginMetadataList,
PluginType,
_filter_plugins_from_dists,
ape_version,
get_plugin_dists,
)
from ape.utils.misc import _get_distributions, load_config
from ape.utils.misc import load_config


@click.group(short_help="Manage ape plugins")
Expand Down Expand Up @@ -273,7 +273,7 @@ def _change_version(spec: str):
# NOTE: It is possible plugins may depend on each other and may update in
# an order causing some error codes to pop-up, so we ignore those for now.
plugin_retcode = 0
for plugin in _filter_plugins_from_dists(_get_distributions()):
for plugin in get_plugin_dists():
logger.info(f"Updating {plugin} ...")
name = plugin.split("=")[0].strip()
retcode = _install(name, spec, exit_on_fail=False)
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ape.exceptions import PluginVersionError
from ape.logging import LogLevel
from ape.managers.plugins import _get_unimplemented_methods_warning
from ape.plugins._utils import CORE_PLUGINS as CORE_PLUGINS_LIST
from ape.plugins._utils import (
ApePluginsRepr,
ModifyPluginResultHandler,
Expand Down Expand Up @@ -388,3 +389,11 @@ def test_get_unimplemented_methods_warning_list_containing_plugin(abstract_metho
"Remaining abstract methods: 'serialize_transaction, txn_hash'."
)
assert actual == expected


def test_core_plugins():
# In case any of these happen to be installed, and this feature
# is broken, it will fail. If none are installed, the test will always pass.
non_core_plugins = ("ape_arbitrum", "ape_vyper", "ape_solidity", "ape_ens")
assert not any(p in CORE_PLUGINS_LIST for p in non_core_plugins)
assert "ape_ethereum" in CORE_PLUGINS_LIST

0 comments on commit dd6156b

Please sign in to comment.