andreoliwa / nitpick

Enforce the same settings on multiple projects
https://nitpick.readthedocs.io/
MIT License
393 stars 24 forks source link

"AttributeError: 'Project' object has no attribute '_confirmed_root'" when running nitpick init in an empty directory #644

Open valberg opened 7 months ago

valberg commented 7 months ago

Expected behavior

I was going to play around with nitpick, and thought the best course of action was to try to apply a custom style to an empty directory. But it is maybe not a supported behaviour? I would expect nitpick to give me an error message telling me what is wrong.

Current behavior

When nitpick init I get (this is expected I guess since there is no configuration yet):

Nothing to do. 😴 Either pass at least one style URL or use --suggest to add styles based on the files in the project root (you can do both at the same time).

I then try nitpick init --suggest which results in the following stacktrace:

$ nitpick init --suggest
Traceback (most recent call last):
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/core.py", line 248, in root
    root = self._confirmed_root
           ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Project' object has no attribute '_confirmed_root'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/valberg/.local/bin/nitpick", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/__main__.py", line 14, in main
    nitpick_cli(auto_envvar_prefix=PROJECT_NAME)
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/cli.py", line 197, in init
    config = nit.project.read_configuration()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/core.py", line 295, in read_configuration
    config_file = self.config_file_or_default()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/core.py", line 266, in config_file_or_default
    config_file = self.config_file()
                  ^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/core.py", line 275, in config_file
    existing: Path = self.root / possible
                     ^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/core.py", line 250, in root
    root = self._confirmed_root = confirm_project_root(self._chosen_root)
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valberg/.local/pipx/venvs/nitpick/lib/python3.11/site-packages/nitpick/core.py", line 179, in confirm_project_root
    raise QuitComplainingError(Reporter().make_fuss(ProjectViolations.NO_ROOT_DIR))
nitpick.exceptions.QuitComplainingError

Steps to reproduce

  1. Run nitpick init --suggest in an empty directory.

Possible Solution

Give a more informative error about nitpick not working on an empty directory. Or implement a way for nitpick to be able to apply styles to an empty directory (create files with desired content etc.)

Context

I'm trying to apply a style to an empty directory to "kickstart a project".

andreoliwa commented 7 months ago

Thanks for the bug report. 👍🏻 Yeah, it should definitely output an informative error...