Repository: pypa/setuptools

Issue:
package_dir and setup.cfg version attr: <foo> mutually exclusive?
I was messing around with the new ``setup.cfg`` support for metadata, and it looks like there is a poor interaction with ``package_dir`` and the version field support, specifically if you have a ``setup.cfg`` like:

```
[metadata]
version = attr: foobar.__version__

[options]
package_dir =
    = src
```

You'll get an error trying to import foobar. This is because it simply appends the current directory to ``sys.path`` and then attempts to import the above.


Discussion:
@jmbowman Another one you can look into.

Is it possible that #1358 and #1359 already solve this problem?

This may be a separate issue, but shouldn't be hard to fix if so; I'll take a look and create a test case for it.

Resolving patch:
diff --git a/setuptools/config.py b/setuptools/config.py
--- a/setuptools/config.py
+++ b/setuptools/config.py
@@ -102,14 +102,14 @@ def parse_configuration(
         If False exceptions are propagated as expected.
     :rtype: list
     """
-    meta = ConfigMetadataHandler(
-        distribution.metadata, command_options, ignore_option_errors)
-    meta.parse()
-
     options = ConfigOptionsHandler(
         distribution, command_options, ignore_option_errors)
     options.parse()
 
+    meta = ConfigMetadataHandler(
+        distribution.metadata, command_options, ignore_option_errors, distribution.package_dir)
+    meta.parse()
+
     return meta, options
 
 
@@ -281,7 +281,7 @@ def _read_file(filepath):
             return f.read()
 
     @classmethod
-    def _parse_attr(cls, value):
+    def _parse_attr(cls, value, package_dir=None):
         """Represents value as a module attribute.
 
         Examples:
@@ -301,7 +301,21 @@ def _parse_attr(cls, value):
         module_name = '.'.join(attrs_path)
         module_name = module_name or '__init__'
 
-        sys.path.insert(0, os.getcwd())
+        parent_path = os.getcwd()
+        if package_dir:
+            if attrs_path[0] in package_dir:
+                # A custom path was specified for the module we want to import
+                custom_path = package_dir[attrs_path[0]]
+                parts = custom_path.rsplit('/', 1)
+                if len(parts) > 1:
+                    parent_path = os.path.join(os.getcwd(), parts[0])
+                    module_name = parts[1]
+                else:
+                    module_name = custom_path
+            elif '' in package_dir:
+                # A custom parent directory was specified for all root modules
+                parent_path = os.path.join(os.getcwd(), package_dir[''])
+        sys.path.insert(0, parent_path)
         try:
             module = import_module(module_name)
             value = getattr(module, attr_name)
@@ -400,6 +414,12 @@ class ConfigMetadataHandler(ConfigHandler):
 
     """
 
+    def __init__(self, target_obj, options, ignore_option_errors=False,
+                 package_dir=None):
+        super(ConfigMetadataHandler, self).__init__(target_obj, options,
+                                                    ignore_option_errors)
+        self.package_dir = package_dir
+
     @property
     def parsers(self):
         """Metadata item name to parser function mapping."""
@@ -440,7 +460,7 @@ def _parse_version(self, value):
                 ))
             return version
 
-        version = self._parse_attr(value)
+        version = self._parse_attr(value, self.package_dir)
 
         if callable(version):
             version = version()


Test patch:
diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py
--- a/setuptools/tests/test_config.py
+++ b/setuptools/tests/test_config.py
@@ -10,13 +10,15 @@ class ErrConfigHandler(ConfigHandler):
 
 
 def make_package_dir(name, base_dir):
-    dir_package = base_dir.mkdir(name)
+    dir_package = base_dir
+    for dir_name in name.split('/'):
+        dir_package = dir_package.mkdir(dir_name)
     init_file = dir_package.join('__init__.py')
     init_file.write('')
     return dir_package, init_file
 
 
-def fake_env(tmpdir, setup_cfg, setup_py=None):
+def fake_env(tmpdir, setup_cfg, setup_py=None, package_path='fake_package'):
 
     if setup_py is None:
         setup_py = (
@@ -28,7 +30,7 @@ def fake_env(tmpdir, setup_cfg, setup_py=None):
     config = tmpdir.join('setup.cfg')
     config.write(setup_cfg)
 
-    package_dir, init_file = make_package_dir('fake_package', tmpdir)
+    package_dir, init_file = make_package_dir(package_path, tmpdir)
 
     init_file.write(
         'VERSION = (1, 2, 3)\n'
@@ -285,6 +287,51 @@ def test_version_file(self, tmpdir):
             with get_dist(tmpdir) as dist:
                 _ = dist.metadata.version
 
+    def test_version_with_package_dir_simple(self, tmpdir):
+
+        _, config = fake_env(
+            tmpdir,
+            '[metadata]\n'
+            'version = attr: fake_package_simple.VERSION\n'
+            '[options]\n'
+            'package_dir =\n'
+            '    = src\n',
+            package_path='src/fake_package_simple'
+        )
+
+        with get_dist(tmpdir) as dist:
+            assert dist.metadata.version == '1.2.3'
+
+    def test_version_with_package_dir_rename(self, tmpdir):
+
+        _, config = fake_env(
+            tmpdir,
+            '[metadata]\n'
+            'version = attr: fake_package_rename.VERSION\n'
+            '[options]\n'
+            'package_dir =\n'
+            '    fake_package_rename = fake_dir\n',
+            package_path='fake_dir'
+        )
+
+        with get_dist(tmpdir) as dist:
+            assert dist.metadata.version == '1.2.3'
+
+    def test_version_with_package_dir_complex(self, tmpdir):
+
+        _, config = fake_env(
+            tmpdir,
+            '[metadata]\n'
+            'version = attr: fake_package_complex.VERSION\n'
+            '[options]\n'
+            'package_dir =\n'
+            '    fake_package_complex = src/fake_dir\n',
+            package_path='src/fake_dir'
+        )
+
+        with get_dist(tmpdir) as dist:
+            assert dist.metadata.version == '1.2.3'
+
     def test_unknown_meta_item(self, tmpdir):
 
         fake_env(