Unfriendly error when the version can't be determined
SpecLad opened this issue · comments
Given this setup:
$ ls
changes towncrier.toml
$ cat towncrier.toml
[tool.towncrier]
directory = "changes"
running towncrier build
yields this error message:
Loading template...
Finding news fragments...
Rendering news fragments...
Traceback (most recent call last):
File "/home/dpb/.local/bin/towncrier", line 8, in <module>
sys.exit(cli())
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/towncrier/build.py", line 89, in _main
return __main(
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/towncrier/build.py", line 147, in __main
project_version = get_version(
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/towncrier/_project.py", line 40, in get_version
module = _get_package(package_dir, package)
File "/home/dpb/.local/pipx/venvs/towncrier/lib/python3.10/site-packages/towncrier/_project.py", line 19, in _get_package
module = import_module(package)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1047, in _gcd_import
File "<frozen importlib._bootstrap>", line 981, in _sanity_check
ValueError: Empty module name
This in no way describes the actual problem, which is that towncrier couldn't determine the project version. It would be nice if it could display a friendlier message, e.g. "Unable to determine the project version. Use the --version option."
Thanks for the report.
The final error is "ValueError: Empty module name"
It looks like the problem is not the version.
To me it looks like, towncrier has no idea for which project/source it's supposed to work on.
Towncrier is designed to be used by software developers.
So the error show a full traceback in an attempt to be developer friendly and not end-user friendly :)
Have you reviewed the docs from https://towncrier.readthedocs.io/en/stable/quickstart.html ?
From your cat
, I don't see the package
and package_dir
configurations.
The most basic configuration is annotated below:
[tool.towncrier]
# The name of your Python package
package = "myproject"
# The path to your Python package.
# If your package lives in 'src/myproject/', it must be 'src',
# but if you don't keep your code in a 'src' dir, remove the
# config option
package_dir = "src"
# Where you want your news files to come out. This can be .rst
# or .md, towncrier's default template works with both.
filename = "NEWS.rst"
My config file is actually the first example from here: https://towncrier.readthedocs.io/en/stable/index.html#quick-start.
In this example (and the project I'm considering to use towncrier in) there is no Python project, and you're supposed to supply the version number manually.
True. Thanks for the details
So the issue is something like this.
If no version is found, towncrier fallbacks to detect the version via python auto-detections, but if Python project is not defined/found, it ends up with the ValueError: Empty module name
error.
Not sure how to fix it
I guess that in towncrier/build.py
when get_version
is called, it needs to be enclosed in a try/catch and show a better error message
I am leaving this report open and happy to review a fix for it :)
Thanks again for the report
Another option to fix this, is throwing more documentation into trying to fix this, and highlight that for non-python project, a version is required.
There is room for improvements, we just need time for this :)
I guess that in towncrier/build.py when get_version is called, it needs to be enclosed in a try/catch and show a better error message
It could just display the error message if config.package
is blank at that point.