bazelbuild / intellij

IntelliJ plugin for Bazel projects
https://ij.bazel.build/
Apache License 2.0
761 stars 303 forks source link

Plugin doesn't recognise python source roots #350

Open hsestupin opened 6 years ago

hsestupin commented 6 years ago

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 ?

brendandouglas commented 6 years ago

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).

PoncinMatthieu commented 6 years ago

We have a very similar issue. Imports are not resolved between subpackages. For example we define tests as a subpackages, so we have something like this:

/my_package/BUILD
/my_package/tests/BUILD

And projectview file would look like this:

directories:
  my_package/

When syncing our project, intellij will set my_package in the python_source_root, but our subpackage is not added and therefore any imports made on the tests subpackage is not resolved by IntelliJ. We are also able to fix it by directly editing the python_source_root. However IntelliJ UI prevents us from doing that directly because it consider the new directory conflicting with the current one, but we are able to trick it into adding it by first excluding the tests folder from the current source root. Note that bazel plugin also doesn't take into account the import attributes nor the genfiles (https://github.com/bazelbuild/intellij/issues/389) Should I report this as a separate issue or keep this here?

ghost commented 5 years ago

@brendandouglas Is this on Google's IJ team's roadmap to fix anytime soon? This is a recently discovered pain point for one of our teams that has migrated to Bazel. It's my job to make sure they don't revolt, so without an upstream fix coming, I may have to try fixing it myself. I don't mind rolling up my sleeves, but any guidance would be appreciated. :)

brendandouglas commented 5 years ago

Hi @beasleyr-vmw, this isn't on our roadmap at the moment. The hard part here is exposing the additional source roots to starlark in a usable form (ideally after make variable substitution).

knarukulla commented 2 years ago

Any workaround to the issue would be helpful

thirtyseven commented 2 years ago

A workaround we use is to have the python source root be separately listed in the directories: section of the project view file. However, for this to work, none of its parent directories (including .) can be listed.

thomaslangston-mongodb commented 2 years ago

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

github-actions[bot] commented 1 year ago

Thank you for contributing to the IntelliJ repository! This issue has been marked as stale since it has not had any activity in the last 6 months. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-maintainer". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

thirtyseven commented 1 year ago

@bazelbuild/triage this issue is still relevant

gmishkin commented 1 year ago

Specifically I'm curious if

The hard part here is exposing the additional source roots to starlark

Is not a concern anymore since rules_python was (I think?) moved to Starlark recently.

github-actions[bot] commented 11 months ago

Thank you for contributing to the IntelliJ repository! This issue has been marked as stale since it has not had any activity in the last 6 months. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-maintainer". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

github-actions[bot] commented 10 months ago

This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please reach out to the triage team (@bazelbuild/triage). Thanks!

honnix commented 9 months ago

I believe this is still a very relevant feature request.

WearyMonkey commented 9 months ago

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.

github-actions[bot] commented 3 months ago

Thank you for contributing to the IntelliJ repository! This issue has been marked as stale since it has not had any activity in the last 6 months. It will be closed in the next 14 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

phil-osk commented 3 months ago

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

luke-robertson commented 1 month ago

+1

tpasternak commented 3 weeks ago

I didn't have time to check that, but it seems the problem is at least partially covered https://github.com/bazelbuild/intellij/issues/6599#issuecomment-2299246575