intellij icon indicating copy to clipboard operation
intellij copied to clipboard

IntelliJ IDEA 2019.1.3 fails to resolve dependencies

Open CharlieCrisp opened this issue 5 years ago • 17 comments

Tldr; "How do I reset ALL plugin state when things go wrong".

I have been using the bazel plugin for IntelliJ 2019.1.3, bazel 0.25.2, and plugin version v2019.05.13.0.2. Running bazel build //... always works and Sync project with BUILD files always completes successfully. However, IntelliJ sometimes (sometimes but not always) fails to resolve dependencies like so: image

This only happens with dependencies which are being downloaded as jars (not compiled from source) e.g. any maven dependencies. (We are using the rules_jvm_external maven_install rule to download dependencies but this happens with other rules too.)

Although all these dependency jars show up in bazel-REPO_NAME/external/..., they are not appearing in the 'External Libraries' explorer area. The .ijwb/.idea/libraries is not being populated with the correct xml entries. Adding libraries manually through the Project Structure dialog (and pointing to the jar used by bazel) works fine and then the dependencies are resolved fine, but the plugin is unable to do this automatically.

This problem can often be fixed by just adding a comma or whitespace to the BUILD file which causes the plugin to retry the dependency resolution. Resetting this whitespace will cause the problem to reappear, seemingly due to some cacheing. I am looking for a way to reliably reset all the state used by the plugin so that it will retry the external libraries resolution without having to add random whitespace or commas to BUILD files. Things tried so far to no avail:

  • File > Invalidate Caches / Restart
  • Reinstalling plugin
  • Bazel -> Sync -> Sync Project
  • Deleting .ijwb and reimporting project

CharlieCrisp avatar Jun 04 '19 17:06 CharlieCrisp

Bazel -> Sync -> Sync Project with BUILD Files. Or use the non-incremental version in Ctrl-Shift-A if that doesn't work.

chaoren avatar Jun 04 '19 17:06 chaoren

You could also just delete the hidden .ijwb directory in your project root, and reimport the project.

chaoren avatar Jun 04 '19 17:06 chaoren

@chaoren I've now updated the issue to reflect this but neither of these solutions work. Indeed in the latest version of the plugin for mac there is no option at all to do a non-incremental sync.

Edit to add more detail: after deleting .ijwb and reimporting, the Sync Project with BUILD Files completes successfully but all files appear in the explorer as (unsynced). At this point the only way I've found to trigger them to sync (all the options in the Sync menu don't work) is by changing the order of dependencies in the BUILD files.

CharlieCrisp avatar Jun 05 '19 08:06 CharlieCrisp

When you modify a file, the directory containing the file is added to your working set, and the sync will include your working set by default. It looks like you just didn't include the correct targets in your project view, otherwise you wouldn't need them to be in the working set to pick them up.

chaoren avatar Jun 05 '19 15:06 chaoren

Please share your .ijwb/.bazelproject file and the target label that contains the files you want to be synced.

chaoren avatar Jun 05 '19 15:06 chaoren

Thanks for getting back to us. Unfortunately there are no clear repro steps, but we suspect it could be related to our usage of Kotlin as it may not have received as much test coverage.

.ijwb/.bazelproject

import .bazelproject
additional_languages:
  kotlin

.bazelproject

directories:
  .

targets:
  //...

additional_languages:

Can we please re-open this issue? We are seeing frequent syncing issues that are only fixed when making trivial edits to BUILD files. It seems the plugin needs to consider the package to be within the working set in order to link dependencies in Intellij.

This is likely caused by the OSS kotlin rules not passing jdeps information through to the starlark Java API provider. The plugin relies on jdeps to trim dependencies.

We could work around this by temporarily indexing all direct dependencies of kotlin targets for bazel, until the rules provide this information.

brendandouglas avatar Jul 01 '19 16:07 brendandouglas

Thanks for the response Brendan!

I'd be in support of a workaround to alleviate this issue, although am trying to understand the mechanism before requesting a specific change.

Would you please help me understand what is happening here?

When I repro this issue by removing a BUILD file from the working set I can see the difference in idea.log

Normal

base.scope.scopes.IdeaLogScope - Workspace has 21 libraries

Error

base.scope.scopes.IdeaLogScope - Workspace has 17 libraries

So it seems the libraries param here has lost entries. I don't get the impression anything has changed with the jdeps file although this is not something I am familiar with.

More context... normal:

2019-07-01 17:25:53,799 [279794664]   INFO - base.scope.scopes.IdeaLogScope - Total rules: 54, new/changed: 1, removed: 0
2019-07-01 17:25:53,800 [279794665]   INFO - base.scope.scopes.IdeaLogScope - Reading IDE info result...
2019-07-01 17:25:53,800 [279794665]   INFO - base.scope.scopes.IdeaLogScope - Updating target map
2019-07-01 17:25:53,801 [279794666]   INFO - base.scope.scopes.IdeaLogScope - Loaded 1 aspect files, total size 2kB
2019-07-01 17:25:53,802 [279794667]   INFO - base.scope.scopes.IdeaLogScope - Target map size: 47
2019-07-01 17:25:53,804 [279794669]   INFO - base.scope.scopes.IdeaLogScope - Reading jdeps files...
2019-07-01 17:25:53,805 [279794670]   INFO - base.scope.scopes.IdeaLogScope - Loaded 1 jdeps files, total size 0kB
2019-07-01 17:25:53,806 [279794671]   INFO - base.scope.scopes.IdeaLogScope - Reading package manifests...
2019-07-01 17:25:53,807 [279794672]   INFO - base.scope.scopes.IdeaLogScope - Java content entry count: 1
2019-07-01 17:25:53,808 [279794673]   INFO - base.scope.scopes.IdeaLogScope - Updating Jar Cache...
2019-07-01 17:25:53,809 [279794674]   INFO - base.scope.scopes.IdeaLogScope - Prefetching files...
2019-07-01 17:25:53,809 [279794674]   INFO - base.scope.scopes.IdeaLogScope - Refreshing files
2019-07-01 17:25:53,870 [279794735]   INFO - base.scope.scopes.IdeaLogScope - Computing directory structure...
2019-07-01 17:25:53,870 [279794735]   INFO - base.scope.scopes.IdeaLogScope - Committing project structure...
2019-07-01 17:25:53,910 [279794775]   INFO - base.scope.scopes.IdeaLogScope - Workspace has 21 libraries
2019-07-01 17:25:53,946 [279794811]   INFO - base.scope.scopes.IdeaLogScope - Workspace has 2 modules
2019-07-01 17:25:53,956 [279794821]   INFO - pl.ProjectRootManagerComponent - project roots have changed

Error:

2019-07-01 17:27:09,896 [279870761]   INFO - base.scope.scopes.IdeaLogScope - Total rules: 54, new/changed: 1, removed: 0
2019-07-01 17:27:09,896 [279870761]   INFO - base.scope.scopes.IdeaLogScope - Reading IDE info result...
2019-07-01 17:27:09,896 [279870761]   INFO - base.scope.scopes.IdeaLogScope - Updating target map
2019-07-01 17:27:09,897 [279870762]   INFO - base.scope.scopes.IdeaLogScope - Loaded 1 aspect files, total size 2kB
2019-07-01 17:27:09,898 [279870763]   INFO - base.scope.scopes.IdeaLogScope - Target map size: 47
2019-07-01 17:27:09,899 [279870764]   INFO - base.scope.scopes.IdeaLogScope - Reading jdeps files...
2019-07-01 17:27:09,900 [279870765]   INFO - base.scope.scopes.IdeaLogScope - Loaded 1 jdeps files, total size 0kB
2019-07-01 17:27:09,902 [279870767]   INFO - base.scope.scopes.IdeaLogScope - Reading package manifests...
2019-07-01 17:27:09,903 [279870768]   INFO - base.scope.scopes.IdeaLogScope - Java content entry count: 1
2019-07-01 17:27:09,904 [279870769]   INFO - base.scope.scopes.IdeaLogScope - Updating Jar Cache...
2019-07-01 17:27:09,905 [279870770]   INFO - base.scope.scopes.IdeaLogScope - Prefetching files...
2019-07-01 17:27:09,905 [279870770]   INFO - base.scope.scopes.IdeaLogScope - Refreshing files
2019-07-01 17:27:09,941 [279870806]   INFO - base.scope.scopes.IdeaLogScope - Computing directory structure...
2019-07-01 17:27:09,941 [279870806]   INFO - base.scope.scopes.IdeaLogScope - Committing project structure...
2019-07-01 17:27:09,990 [279870855]   INFO - base.scope.scopes.IdeaLogScope - Workspace has 17 libraries
2019-07-01 17:27:10,004 [279870869]   INFO - base.scope.scopes.IdeaLogScope - Workspace has 2 modules
2019-07-01 17:27:10,011 [279870876]   INFO - pl.ProjectRootManagerComponent - project roots have changed

A library is generally only added to the project if it's needed to resolve source code (determined via jdeps).

An exception is for files in the project's 'working set' (modified files), when 'Bazel > Sync > Expand to working set' is checked. In that case, all direct deps of a java/kotlin/scala target are added to the project libraries.

brendandouglas avatar Jul 01 '19 18:07 brendandouglas

Thanks for the explanation.

Strangely after turning off "expand to working set" I still find targets within the working set to be fixed after re-syncing.

Nonetheless, it seems that when direct dependencies are indexed this issue does not occur, so if a temporary workaround is available then it would be much appreciated.

Ah yes, it doesn't have anything to do with that 'expand to working set' option -- that just determines what is synced, not how we treat dependencies.

brendandouglas avatar Jul 02 '19 13:07 brendandouglas

thanks for clarifying. If you are available on bazelbuild slack it would be great to chat -- with a bit more background on how this stuff fits together we may be able to help with fixes going forward.

I think I'm hitting a similar issue. External dependencies referenced through maven_install didn't have the right jars in the project.

Looking into Project Settings > Libraries, I noticed that the path to those binaries were incorrect.

Example from my windows machine: commons-lang3-3.9.jar was referencing a path that didn't exist. [Project Root]\external\maven\v1\https\repo1.maven.org\maven2\org\apache\commons\commons-lang3\3.9\commons-lang3-3.9.jar

I noticed that [Project Root]\external should have been [Project Root]\bazel-[projectname]\external instead.

As a workaround, I created a symlink from [Project Root]\external to [Project Root]\bazel-[projectname]\external and IntelliJ started working properly.

HTH

occ avatar Jul 14 '19 22:07 occ

@jin Just commenting to say that this still hasn't been resolved

CharlieCrisp avatar Oct 21 '19 09:10 CharlieCrisp

Reopening this issue as multiple users have reported that the issue hasn't been resolved. May also be related to https://github.com/bazelbuild/intellij/issues/1256

jin avatar Nov 05 '19 20:11 jin

I am importing a bazel project which use scala play and the libraries does not include the play-exceptions.jar because the class from that jar (PlayException.class) is only used in tests, is there any workaround beside adding the library manually, I would like to have them added automatically

ldebello-ddl avatar Jul 18 '22 16:07 ldebello-ddl

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] avatar Mar 16 '23 02:03 github-actions[bot]

Closing this issue because IntelliJ IDEA 2019.1.3 is no longer supported by the plugin, please file a new issue if the problem is reproducible with IntelliJ 2022.2 or 2022.3.

mai93 avatar Mar 23 '23 06:03 mai93