Repository: canonical/charmcraft

Issue:
Running `charmcraft fetch-lib` on a charm with dashes should fetch to a path with underscores
If you have a library for a charm (such as nginx-ingress-integrator) that has dashes in the name, and run `charmcraft fetch-lib charms.nginx-ingress-integrator.v0.charms` it should still fetch to a path on disk that has underscores rather than dashes, so it's importable.

Discussion:

Patch:
diff --git a/charmcraft/commands/store/__init__.py b/charmcraft/commands/store/__init__.py
index cd187442..e1589462 100644
--- a/charmcraft/commands/store/__init__.py
+++ b/charmcraft/commands/store/__init__.py
@@ -922,9 +922,13 @@ def _get_lib_info(*, full_name=None, lib_path=None):
         # dir and Python extension.
         #    e.g.: charms.mycharm.v4.foo -> lib/charms/mycharm/v4/foo.py
         try:
-            charmsdir, importable_charm_name, v_api, libfile = full_name.split(".")
+            charmsdir, charm_name, v_api, libfile = full_name.split(".")
         except ValueError:
             raise _BadLibraryNameError(full_name)
+
+        # the lib full_name includes the charm_name which might not be importable (dashes)
+        importable_charm_name = create_importable_name(charm_name)
+
         if charmsdir != "charms":
             raise _BadLibraryNameError(full_name)
         path = pathlib.Path("lib")


Test patch:
diff --git a/tests/commands/test_store_commands.py b/tests/commands/test_store_commands.py
index 76f1309a..a80936ea 100644
--- a/tests/commands/test_store_commands.py
+++ b/tests/commands/test_store_commands.py
@@ -2469,6 +2469,13 @@ def test_getlibinfo_bad_name(name):
     )
 
 
+def test_getlibinfo_not_importable_charm_name():
+    """Libraries should be save under importable paths."""
+    lib_data = _get_lib_info(full_name="charms.operator-libs-linux.v0.apt")
+    assert lib_data.charm_name == "operator-libs-linux"
+    assert lib_data.path == pathlib.Path("lib/charms/operator_libs_linux/v0/apt.py")
+
+
 @pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
 @pytest.mark.parametrize(
     "path",