Instagram / LibCST

A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
https://libcst.readthedocs.io/
Other
1.56k stars 192 forks source link

`ValueError` when using `FullyQualifiedNameProvider` #722

Open zwade opened 2 years ago

zwade commented 2 years ago

Hi!

I'm getting the following ValueError when using the FullyQualifiedNameProvider in my Transformer.

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/bin/cst", line 8, in <module>
    sys.exit(main())
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/sync_to_async/cli.py", line 5, in main
    tool_main("cdm", sys.argv[1:])
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/tool.py", line 835, in main
    return lookup.get(args.action or None, _invalid_command)(proc_name, command_args)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/tool.py", line 572, in _codemod_impl
    result = parallel_exec_transform_with_prettyprint(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/codemod/_cli.py", line 588, in parallel_exec_transform_with_prettyprint
    metadata_manager.resolve_cache()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/metadata/full_repo_manager.py", line 66, in resolve_cache
    cache[provider] = handler(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/metadata/name_provider.py", line 115, in gen_cache
    cache = {path: calculate_module_and_package(".", path) for path in paths}
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/metadata/name_provider.py", line 115, in <dictcomp>
    cache = {path: calculate_module_and_package(".", path) for path in paths}
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/site-packages/libcst/helpers/module.py", line 136, in calculate_module_and_package
    relative_filename = PurePath(filename).relative_to(repo_root)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/nuvo/lib/python3.10/pathlib.py", line 816, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/Users/zacharywade/Documents/work/nuvo/nuvo/src/backend/ops/Company.py' is not in the subpath of '' OR one path is relative and the other is absolute.

The culprit seems to be this line of name_provider.py. I'm not entirely sure exactly how the module resolution of this provider is supposed to work, but on a whim I changed "." to root_path and it resolved my issue locally.

I'm not sure if this is a bug in how I'm using the FullyQualifiedNameProvider, or a bug in the provider itself. If it's the latter, I'm happy to submit a PR. If it's the former, I any suggestions on what I might be missing would be greatly appreciated!

Thanks!

zsol commented 2 years ago

Cc @lpetre