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

Fix version check for entrypoint plugins with unequal project/package names #2594

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions sopel/plugins/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,9 @@ def get_version(self) -> Optional[str]:
):
try:
version = importlib.metadata.version(self.module.__package__)
except ValueError:
# package name is probably empty-string; just give up
except Exception:
# Just give up
# Can be caused by empty package name, mismatch between package/project names
Copy link
Member

@dgw dgw Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been talking about this one on IRC this evening, and I believe @SnoopJ and I have found a good middle ground:

If logging works here (we haven't tested how logging interacts with the CLI interfaces like sopel-plugins), it would be good to catch ValueError and PackageNotFoundError which the importlib.metadata.version() call might raise, and just give up. Maybe log a warning, but that's it. As a fallback, catch Exception and log the full exception traceback for investigation. We'd like to know about other failure cases if they exist, but they shouldn't crash the CLI or the .version command called from IRC.

pass

return version
Expand Down
18 changes: 18 additions & 0 deletions test/plugins/test_plugins_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,21 @@ def test_get_label_entrypoint(plugin_tmpfile):
assert meta['label'] == 'plugin label'
assert meta['type'] == handlers.EntryPointPlugin.PLUGIN_TYPE
assert meta['source'] == 'test_plugin = file_mod'


def test_entrypoint_plugin_get_version(plugin_tmpfile):
# See gh-2593, an entrypoint plugin whose project/package names are not
# equal raises an exception other than ValueError
distrib_dir = os.path.dirname(plugin_tmpfile.strpath)
sys.path.append(distrib_dir)

# load the entry point
try:
entry_point = importlib.metadata.EntryPoint(
'test_plugin', 'file_mod', 'sopel.plugins')
plugin = handlers.EntryPointPlugin(entry_point)
plugin.load()
plugin.module.__package__ = "FAKEFAKEFAKE"
plugin.get_version()
finally:
sys.path.remove(distrib_dir)
Loading