Repository: pypa/setuptools

Issue:
[BUG] FileNotFoundError jaraco/text/Lorem ipsum.txt when importing pkg_resources
### setuptools version

setuptools==60.7.0

### Python version

3.9

### OS

Debian 11

### Additional environment information

_No response_

### Description

FileNotFoundError when importing `pkg_resources`

```
  File "/home/aaa/env/lib/python3.9/site-packages/google/api_core/client_info.py", line 23, in <module>
    import pkg_resources
  File "/tmp/aaa/pypi__setuptools/pkg_resources/__init__.py", line 74, in <module>
    from pkg_resources.extern.jaraco.text import (
  File "/tmp/aaa/pypi__setuptools/pkg_resources/_vendor/jaraco/text/__init__.py", line 227, in <module>
    lorem_ipsum: str = files(__name__).joinpath('Lorem ipsum.txt').read_text()
  File "/home/aaa/.pyenv/versions/3.9.7/lib/python3.9/pathlib.py", line 1266, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/home/aaa/.pyenv/versions/3.9.7/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/home/aaa/.pyenv/versions/3.9.7/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/aaa/py_deps/pypi__setuptools/pkg_resources/_vendor/jaraco/text/Lorem ipsum.txt'
```

### Expected behavior

Pinning setuptools==60.6.0 in requirements.txt works fine. This issue started with setuptools==60.7.0.

### How to Reproduce

Using Bazel and [rules_python](https://github.com/bazelbuild/rules_python), build a zip executable using: 
```
bazel build //<target> --output_groups=python_zip_file
python3 bazel-bin/<target>/<target>.zip 
```

### Output

```console
...
  File "/home/aaa/env/lib/python3.9/site-packages/google/api_core/client_info.py", line 23, in <module>
    import pkg_resources
  File "/tmp/aaa/pypi__setuptools/pkg_resources/__init__.py", line 74, in <module>
    from pkg_resources.extern.jaraco.text import (
  File "/tmp/aaa/pypi__setuptools/pkg_resources/_vendor/jaraco/text/__init__.py", line 227, in <module>
    lorem_ipsum: str = files(__name__).joinpath('Lorem ipsum.txt').read_text()
  File "/home/aaa/.pyenv/versions/3.9.7/lib/python3.9/pathlib.py", line 1266, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/home/aaa/.pyenv/versions/3.9.7/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/home/aaa/.pyenv/versions/3.9.7/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/aaa/py_deps/pypi__setuptools/pkg_resources/_vendor/jaraco/text/Lorem ipsum.txt'
```



Discussion:
Hi @thinhha thank you very much for opening the issue.

I just double checked the `wheel` published on PyPI, and it seems to include the file:

![image](https://user-images.githubusercontent.com/320755/152170344-1a518235-4e61-4438-81a1-76f98f3f12f0.png)

If I build setuptools locally with `python -m build`, I am also able to find the file.

I also double checked in setuptools' own configuration file, and this file is being included in the `package_data` option:

https://github.com/pypa/setuptools/blob/763cf01ef7df80a5d0b64ec69427336a9cb419b8/setup.py#L16

which, according to [this table](https://github.com/abravalheri/experiment-setuptools-package-data#results-for-setuptools6060), indicates that they should be included in the distribution.

Any chance this problem is being caused by the tool you are using to build a zip executable? (e.g. if there is a problem in `bazel` or `rules_python` preventing them for including all the files that would be present in a wheel, in the generated `zip`; or if this technique of bundling dependencies together does not work with `importlib-resources` and `importlib-metadata`)

Can you reproduce this issue, without using `bazel`?
This is linked to the issue that Bazel [does not play nicely with spaces in file names](https://github.com/bazelbuild/bazel/issues/4327).

I've just been bitten with this whilst using a Poetry rule from Bazel, with no way to pin the `setuptools` version that I can find right now. So much for my deterministic build!

I've confirmed that renaming the file to use an underscore allows Bazel to symlink it into the runfiles just fine.
Thank you very much for the investigation @edward-watson-oxb !

Would you (or any other member of the community) be interested in providing a PR with the name change to https://github.com/jaraco/jaraco.text? (As a temporary workaround for the bazel limitation)

I think that if the reasons are well explained, the upstream project has good chances of accepting it... Otherwise I am afraid that the fix will have to come from the bazel side...
Actually, I'll be unlikely to accept a change to rename the file without spaces. Setuptools itself has [files that have spaces](https://github.com/pypa/setuptools/blob/main/setuptools/command/launcher%20manifest.xml) in the name. See #134. Can you look into how it is that bazel is able to build setuptools<60.7 without the issue?
One thing that makes this issue particularly problemmatic is that the behavior is invoked on import, and it's behavior that Setuptools doesn't even need. I'm slightly tempted to move the functionality to another library entirely. It just feels like a whole lot of overhead to create an entirely new project to host functionality to be shared by pkg_resources and setuptools.
I may consider patching jaraco.text when it's vendored to eliminate the file or rename it.
I looked into [making that property lazy-loaded](https://github.com/jaraco/jaraco.text/tree/feature/lazy-property), but that only led me to a [deficiency in doctests](https://bugs.python.org/issue46619). I'd probably have just inlined those two lines of text, except Python linters are hostile to long lines (hobgoblins of our collective minds), and I want to cleanly present the content (without introducing intervening characters to accommodate Python).

Resolving patch:
diff --git a/pkg_resources/_vendor/jaraco/text/__init__.py b/pkg_resources/_vendor/jaraco/text/__init__.py
--- a/pkg_resources/_vendor/jaraco/text/__init__.py
+++ b/pkg_resources/_vendor/jaraco/text/__init__.py
@@ -224,7 +224,6 @@ def unwrap(s):
     return '\n'.join(cleaned)
 
 
-lorem_ipsum: str = files(__name__).joinpath('Lorem ipsum.txt').read_text()
 
 
 class Splitter(object):
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -13,7 +13,6 @@
 package_data = dict(
     setuptools=['script (dev).tmpl', 'script.tmpl', 'site-patch.py'],
 )
-package_data.update({'pkg_resources._vendor.jaraco.text': ['*.txt']})
 
 force_windows_specific_files = (
     os.environ.get("SETUPTOOLS_INSTALL_WINDOWS_SPECIFIC_FILES", "1").lower()
diff --git a/tools/vendored.py b/tools/vendored.py
--- a/tools/vendored.py
+++ b/tools/vendored.py
@@ -37,6 +37,8 @@ def rewrite_jaraco_text(pkg_files, new_root):
         text = file.read_text()
         text = re.sub(r' (jaraco\.)', rf' {new_root}.\1', text)
         text = re.sub(r' (importlib_resources)', rf' {new_root}.\1', text)
+        # suppress loading of lorem_ipsum; ref #3072
+        text = re.sub(r'^lorem_ipsum.*\n$', '', text, flags=re.M)
         file.write_text(text)
 
 


Test patch: