Repository: pypa/setuptools

Issue:
AssertionError on len(sdists) == 1 when sdist format is zip
In some Windows projects, users expect sdists to be released in zip form. For that reason, I've added the following to a project's setup.cfg:

```
[sdist]
formats=zip
```

With this setting in place, performing a `pep517.build .` on such a project results in the following traceback:

```
Traceback (most recent call last):
  File "/Users/jaraco/code/main/jaraco.video/.tox/release/lib/python3.7/site-packages/pep517/_in_process.py", line 207, in <module>
    main()
  File "/Users/jaraco/code/main/jaraco.video/.tox/release/lib/python3.7/site-packages/pep517/_in_process.py", line 197, in main
    json_out['return_val'] = hook(**hook_input['kwargs'])
  File "/Users/jaraco/code/main/jaraco.video/.tox/release/lib/python3.7/site-packages/pep517/_in_process.py", line 170, in build_sdist
    return backend.build_sdist(sdist_directory, config_settings)
  File "/var/folders/c6/v7hnmq453xb6p2dbz1gqc6rr0000gn/T/pep517-build-env-rgz_zb4_/lib/python3.7/site-packages/setuptools/build_meta.py", line 181, in build_sdist
    assert len(sdists) == 1
AssertionError
```

Even though the output from building the sdist was seen above. I suspect the builder expects the sdist to have a particular extension.
AssertionError on len(sdists) == 1 when sdist format is zip
In some Windows projects, users expect sdists to be released in zip form. For that reason, I've added the following to a project's setup.cfg:

```
[sdist]
formats=zip
```

With this setting in place, performing a `pep517.build .` on such a project results in the following traceback:

```
Traceback (most recent call last):
  File "/Users/jaraco/code/main/jaraco.video/.tox/release/lib/python3.7/site-packages/pep517/_in_process.py", line 207, in <module>
    main()
  File "/Users/jaraco/code/main/jaraco.video/.tox/release/lib/python3.7/site-packages/pep517/_in_process.py", line 197, in main
    json_out['return_val'] = hook(**hook_input['kwargs'])
  File "/Users/jaraco/code/main/jaraco.video/.tox/release/lib/python3.7/site-packages/pep517/_in_process.py", line 170, in build_sdist
    return backend.build_sdist(sdist_directory, config_settings)
  File "/var/folders/c6/v7hnmq453xb6p2dbz1gqc6rr0000gn/T/pep517-build-env-rgz_zb4_/lib/python3.7/site-packages/setuptools/build_meta.py", line 181, in build_sdist
    assert len(sdists) == 1
AssertionError
```

Even though the output from building the sdist was seen above. I suspect the builder expects the sdist to have a particular extension.


Discussion:
I see now this issue is clearly an issue with setuptools.build_meta, where [the code hard-codes Unix endings](https://github.com/pypa/setuptools/blob/f4eb1393730fea7e522b5622c1e2efaea9fd2cc3/setuptools/build_meta.py#L178-L179).
As pointed out in the pull request, this error is somewhat expected. The build_sdist command is specified to always built a gzipped tarfile. The code should be updated to enforce that expectation. For now, projects building zip files can work around the issue by removing that declaration.
I see now this issue is clearly an issue with setuptools.build_meta, where [the code hard-codes Unix endings](https://github.com/pypa/setuptools/blob/f4eb1393730fea7e522b5622c1e2efaea9fd2cc3/setuptools/build_meta.py#L178-L179).
As pointed out in the pull request, this error is somewhat expected. The build_sdist command is specified to always built a gzipped tarfile. The code should be updated to enforce that expectation. For now, projects building zip files can work around the issue by removing that declaration.

Resolving patch:
diff --git a/setuptools/build_meta.py b/setuptools/build_meta.py
--- a/setuptools/build_meta.py
+++ b/setuptools/build_meta.py
@@ -170,7 +170,7 @@ def build_wheel(wheel_directory, config_settings=None,
 def build_sdist(sdist_directory, config_settings=None):
     config_settings = _fix_config(config_settings)
     sdist_directory = os.path.abspath(sdist_directory)
-    sys.argv = sys.argv[:1] + ['sdist'] + \
+    sys.argv = sys.argv[:1] + ['sdist', '--formats', 'gztar'] + \
         config_settings["--global-option"] + \
         ["--dist-dir", sdist_directory]
     _run_setup()


Test patch:
diff --git a/setuptools/tests/test_build_meta.py b/setuptools/tests/test_build_meta.py
--- a/setuptools/tests/test_build_meta.py
+++ b/setuptools/tests/test_build_meta.py
@@ -172,18 +172,21 @@ def test_build_sdist_version_change(build_backend):
     sdist_name = build_backend.build_sdist(sdist_into_directory)
     assert os.path.isfile(os.path.join(sdist_into_directory, sdist_name))
 
-    # if the setup.py changes subsequent call of the build meta should still succeed, given the
+    # if the setup.py changes subsequent call of the build meta
+    # should still succeed, given the
     # sdist_directory the frontend specifies is empty
     with open(os.path.abspath("setup.py"), 'rt') as file_handler:
         content = file_handler.read()
     with open(os.path.abspath("setup.py"), 'wt') as file_handler:
-        file_handler.write(content.replace("version='0.0.0'", "version='0.0.1'"))
+        file_handler.write(
+            content.replace("version='0.0.0'", "version='0.0.1'"))
 
     shutil.rmtree(sdist_into_directory)
     os.makedirs(sdist_into_directory)
 
     sdist_name = build_backend.build_sdist("out_sdist")
-    assert os.path.isfile(os.path.join(os.path.abspath("out_sdist"), sdist_name))
+    assert os.path.isfile(
+        os.path.join(os.path.abspath("out_sdist"), sdist_name))
 
 
 def test_build_sdist_setup_py_exists(tmpdir_cwd):
@@ -215,3 +218,21 @@ def test_build_sdist_setup_py_manifest_excluded(tmpdir_cwd):
     with tarfile.open(os.path.join("temp", targz_path)) as tar:
         assert not any('setup.py' in name for name in tar.getnames())
 
+
+def test_build_sdist_builds_targz_even_if_zip_indicated(tmpdir_cwd):
+    files = {
+        'setup.py': DALS("""
+            __import__('setuptools').setup(
+                name='foo',
+                version='0.0.0',
+                py_modules=['hello']
+            )"""),
+        'hello.py': '',
+        'setup.cfg': DALS("""
+            [sdist]
+            formats=zip
+            """)
+    }
+
+    build_files(files)
+    build_sdist("temp")