intellij: Plugin doesn't recognise python source roots

Sample project https://github.com/hsestupin/bazel-python-source-root

Open file https://github.com/hsestupin/bazel-python-source-root/blob/master/python_source_root/mymodule/submodule/b.py

What I expect is that absolute mymodule import is normally resolved and user is able to jump via cmd+b from sub_a.aa() call expresison to adjacent module a.py.

Now it’s impossible since Idea says that mymodule is unresolved.

screen shot 2018-06-27 at 13 09 19

However this could be fixed by manually setting python_source_root directory as a source root in project settings. But obviously this change will be immediately discarded after the next project sync.

May be my project setup is incomplete or there is some workaround ?

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 13
  • Comments: 19 (6 by maintainers)

Most upvoted comments

I believe this is still a very relevant feature request.

Copying my understanding of the problem from my duplicate issue: https://github.com/bazelbuild/intellij/issues/3783

The Bazel IntelliJ plugin appears to detect IntelliJ project source roots by the presence of specific Java centric identifiers.

https://github.com/bazelbuild/intellij/blob/master/java/src/com/google/idea/blaze/java/sync/source/SourceDirectoryCalculator.java

Manually editing the source roots via the context menu by clicking on a folder in the Project view and selecting “Mark Directory As -> Sources Root” is discarded on each IntelliJ Bazel plugin Sync.

For Python files and targets, this means their source roots are never detected and cannot be persisted between IntelliJ Bazel plugin Syncs.

Source root detection doesn’t appear to work even for Java files in some cases : https://github.com/bazelbuild/intellij/issues/1992

I can think of a few different paths to address this problem:

Stop automatically setting source roots on each Sync. Force them to be manually configured like a regular IntelliJ project. Maybe still allow automatic detection only on the first IntelliJ project import? Allow manual configurations via IntelliJ’s menus to work as an override that persists through syncs. Make a BUILD target / .bazelproject setting / etc. that flags a directory during Sync as a source root. Refine the automatic detection logic so it can detect the correct source roots under a variety of scenarios including Python only directories

This is still an issue that everyone with a monorepo has to work around.

Still relevant, we have mono repository with typescript project under a sub-directory. There’s currently no way to set the typescript projects src/ as a sources root.

@bazelbuild/triage this issue is still relevant

Thanks for letting us know. This is a bazel-specific issue, which is why we hadn’t noticed earlier – per-target custom source roots aren’t allowed in the internal version of bazel.

This shouldn’t be too difficult to fix. We’ll need to change our skylark aspect to collect the ‘imports’ attribute data, and either set up source roots as you suggest, or add some additional custom import resolvers.

Source roots would be a much more reliable solution of course, but aren’t a python-specific concept in IJ – for projects with multiple languages, source roots can conflict. If we can’t find a way around that, we’ll just continue with custom import resolvers (which already provide resolution relative to the workspace root and genfiles directory).