Skip to content

Commit

Permalink
Avoid raising fatal exception if custom command under a folder breaks (
Browse files Browse the repository at this point in the history
…#13365)

* Avoid raising fatal exception if custom command under a folder breaks

* Add test
  • Loading branch information
AbrilRBS authored Mar 7, 2023
1 parent dc72d7f commit fc8226e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions conan/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ def _add_commands(self):
for module in pkgutil.iter_modules([layer_folder]):
module_name = module[1]
if module_name.startswith("cmd_"):
self._add_command(f"{folder}.{module_name}", module_name.replace("cmd_", ""),
package=folder)
module_path = f"{folder}.{module_name}"
try:
self._add_command(module_path, module_name.replace("cmd_", ""),
package=folder)
except Exception as e:
ConanOutput().error(f"Error loading custom command {module_path}: {e}")

def _add_command(self, import_path, method_name, package=None):
try:
Expand Down
22 changes: 22 additions & 0 deletions conans/test/integration/command_v2/custom_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ def test_import_error_custom_command(self):
client.run("list *")
assert "ERROR: Error loading custom command 'cmd_mycommand.py': " \
"No module named 'this_doesnt_exist'" in client.out
# But it won't break the whole conan and you can still use the rest of it
client.run("config home")
assert client.cache_folder in client.out

def test_import_error_custom_command_subfolder(self):
"""
used to break, this is handled differently in conan
"""
mycommand = textwrap.dedent("""
import this_doesnt_exist
""")

client = TestClient()
command_file_path = os.path.join(client.cache_folder, 'extensions',
'commands', 'mycompany', 'cmd_mycommand.py')
client.save({f"{command_file_path}": mycommand})
# Call to any other command, it will fail loading the custom command,
client.run("list *")
assert "ERROR: Error loading custom command mycompany.cmd_mycommand" in client.out
# But it won't break the whole conan and you can still use the rest of it
client.run("config home")
assert client.cache_folder in client.out

def test_simple_custom_command(self):
mycommand = textwrap.dedent("""
Expand Down

0 comments on commit fc8226e

Please sign in to comment.