pantsbuild / pants

The Pants Build System
https://www.pantsbuild.org
Apache License 2.0
3.26k stars 626 forks source link

Help users when having Import-Errors #13763

Open a-teammate opened 2 years ago

a-teammate commented 2 years ago

When I transfer old code to have its dependencies resolved via pants, at some point I likely get ImportError: could not resolve some.package.in.our.monorepo.or.thirdparty.package. This seems to be a normal part of using pants for dependency management, until I arrive at the point that pants has enough information to resolve all dependencies.

But currently I am left alone and need to understand the internals of pants and some advanced usage tricks to get further. I would love to see some first level support for this use-case, for beginner users. When my coworkers will need to do the same, I don't want to tell them "ah yeah, you need to set these 4 flags and then open the sandbox and maybe with some luck and inspecting that start command, you'll find out what you should have actually imported instead of the name you referenced in your python file".

Solution

Generally it would be beneficial from my perspective to focus this use-case in the pants development a little more, if that is not the case yet: The scenario "a beginner user wants to use pants for dependencies and their build. They have an import error and need to fix it in order to successfully build with pants." might be checked from time-to-time with a beginner user to continuously make this easier for them. Maybe this is already done.

Concrete proposal:

Given the user wants to add a new piece of code, using 3rdparty libs and internal libs

  1. During pants test or pants run, all "import errors" are caught by pants
  2. for each importerror in the run, an educated fix is proposed to the user:
    • all possible imports (in the target language!) optimally one would present those possible imports first, which are similar to the failed import!
    • pants internal information helpful for debugging:
      • all targets (in the language the importerror happened)
      • all source roots

Example:

[pants] main_component/some_package/main.py: failed importing other_package.v2
[pants] did you mean:
                - support_component.other_package.v2
                - other_package.v1
                ...
           The following folders are not exposed, they are missing BUILD files with a python_library target:
                  - src/proposal_component/other_package/v2/**
                  - src/main_component/other_package/v2/**
                  ...
           More debug info:
                  - targets in the repo: ...
                  - source roots: ...
Eric-Arellano commented 2 years ago

Thanks, @a-teammate ! I agree this would be really great to have.

Generally, we believe we shouldn't rely on people reading the docs. While we hope folks do, realistically, for most Pants users Pants is just another tool they use. So, we want Pants's UX to be intuitive enough you can mostly figure it out without needing docs, e.g. good error messages.

We don't currently have any precedent for "catching" exceptions and inserting our own suggestions. Although I'd like to get there.