@@ -372,3 +372,58 @@ def test_hatch_register_metadata_hook():
372372 hook_class = hatch_register_metadata_hook ()
373373
374374 assert hook_class is PluxMetadataHook
375+
376+
377+ class TestHatchlingPackageFinderPath :
378+ """Tests for HatchlingPackageFinder.path property."""
379+
380+ def _make_finder (self , sources , root , packages = None ):
381+ pytest .importorskip ("hatchling" )
382+ from unittest .mock import MagicMock
383+ from plux .build .hatchling import HatchlingPackageFinder
384+
385+ builder_config = MagicMock ()
386+ builder_config .sources = sources
387+ builder_config .root = root
388+ builder_config .packages = packages or []
389+ return HatchlingPackageFinder (builder_config )
390+
391+ def test_path_with_packages_in_subdirectory (self , tmp_path ):
392+ """Regression test: KeyError when packages live in a subdirectory.
393+
394+ When hatchling is configured with ``packages = ["localstack-core/localstack"]``,
395+ builder_config.sources becomes ``{'localstack-core/': ''}`` — the key is the
396+ source directory, not ``''``. The old code did ``sources[""]`` which raised a
397+ KeyError. The fix must return the sources root (``{root}/localstack-core``)
398+ so that ``_list_module_names`` can build correct file-system paths for each
399+ discovered package name.
400+ """
401+ finder = self ._make_finder (
402+ sources = {"localstack-core/" : "" },
403+ root = str (tmp_path ),
404+ packages = ["localstack-core/localstack" ],
405+ )
406+
407+ # Must not raise KeyError, and must return the sources root directory
408+ path = finder .path
409+ import os
410+ assert path == os .path .join (str (tmp_path ), "localstack-core" )
411+
412+ def test_path_with_empty_sources (self , tmp_path ):
413+ """When sources is empty, path falls back to the project root."""
414+ finder = self ._make_finder (sources = {}, root = str (tmp_path ))
415+
416+ assert finder .path == str (tmp_path )
417+
418+ def test_path_with_packages_in_root (self , tmp_path ):
419+ """When packages are in the project root (sources = {'': ''}), path returns root."""
420+ finder = self ._make_finder (sources = {"" : "" }, root = str (tmp_path ))
421+
422+ assert finder .path == str (tmp_path )
423+
424+ def test_path_with_src_layout (self , tmp_path ):
425+ """When sources maps '' -> 'src', path returns the sources root joined to the project root."""
426+ import os
427+ finder = self ._make_finder (sources = {"" : "src" }, root = str (tmp_path ))
428+
429+ assert finder .path == os .path .join (str (tmp_path ), "src" )
0 commit comments