Repository: sopel-irc/sopel

Issue:
cli: error if `extra` directory contains `setup.py`
Lazy Copypasta from IRC

```
<deathbybandaid> started writing a plugin and discovered something strange when
                 adding it to the `extra=` line
<deathbybandaid> I accidentally specified the directory above the plugin with
                 `setup.py` in it
<deathbybandaid> even when setting `logging_level = DEBUG` I only got the
                 `INFO     - Loading plugins...` line
<deathbybandaid> the output I see on my console says   `error: option -c not recognized`
<deathbybandaid> normally, I agree with Exirel that "no logs, no problem",,, but
                 this time there legit isn't a log of it
<deathbybandaid> as it was an error in the path I set, it's more of a deathbybandaid
                 being stupid error than anything
<deathbybandaid> BUT, I still thought it was odd that it threw the error that it did,
                 saying `-c` was not recognized
```

----

Even more info:

I accidentally set 
`extra = /home/sopel/SpiceBotSERV` (which contains `setup.py`)
instead of `extra = /home/sopel/SpiceBotSERV/sopel_SpiceBotSERV`

I discovered the issue after seeing normal output while setting the `extra` line as an empty directory as well as the correct directory.

The output I received to my console:
````
sopel@SpiceSERV:~/SpiceBotSERV$ sopel -c /home/sopel/.sopel/SpiceSERV.cfg
Sopel 7.0.0 (running on Python 3.7.3)
https://sopel.chat/
[2020-04-02 16:02:32,197] sopel.bot            INFO     - Loading plugins...
usage: sopel [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: sopel --help [cmd1 cmd2 ...]
   or: sopel --help-commands
   or: sopel cmd --help

error: option -c not recognized

````

The entire log file with `logging_level = DEBUG` set:
````
[2020-04-02 16:02:55,237] sopel.bot            INFO     - Loading plugins...
````

----

More info by @dgw:

Command line: `sopel -c /home/sopel/.sopel/SpiceSERV.cfg`
`tree` of the incorrect directory (via https://github.com/SpiceBot/SpiceBotSERV/tree/66b03f50b577bf7595036da3c825b1725f821616):
```
SpiceBotSERV
├── COPYING
├── MANIFEST.in
├── NEWS
├── README.md
├── setup.cfg
├── setup.py
└── sopel_SpiceBotSERV
    └── SpiceBotSERV
        └── __init__.py
```


Discussion:
I'll edit the top post as we get more details, to keep everything together.
I figured out the why of this: in `setup.py` we (Python devs) tend to have setuptools's `setup()` call, which will try to read `sys.args`.

When Sopel tries to load that file, the `setup()` function is called, and it sees Sopel's arguments, and doesn't understand them. So it breaks. When arguments parsing fails, it directly `exit()` the main thread. Don't ask, I don't get it either.

I need to check if I can catch the exit error, in theory it's an exception that can be caught. I'll see what I can do.
I've a working branch that outputs this error log:

    [2020-09-22 11:08:02,432] sopel.bot    ERROR - Error loading fakeplugin (trying to exit)

And doesn't break, so it's a success!

Resolving patch:
diff --git a/sopel/bot.py b/sopel/bot.py
--- a/sopel/bot.py
+++ b/sopel/bot.py
@@ -300,6 +300,10 @@ def setup_plugins(self):
             except Exception as e:
                 load_error = load_error + 1
                 LOGGER.exception('Error loading %s: %s', name, e)
+            except SystemExit:
+                load_error = load_error + 1
+                LOGGER.exception(
+                    'Error loading %s (plugin tried to exit)', name)
             else:
                 try:
                     if plugin.has_setup():


Test patch: