Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using rules_scala documented scalafmt support breaks scala dependency discovery #1824

Open
gergelyfabian opened this issue May 13, 2020 · 10 comments
Assignees
Labels
awaiting-maintainer Awaiting review from Bazel team on issues lang: scala Scala rules integration product: IntelliJ IntelliJ plugin type: bug

Comments

@gergelyfabian
Copy link

gergelyfabian commented May 13, 2020

If you define a scalafmt rule as documented in rules_scala it won't be recognized by intellij plugin as a Scala target, as the rule names for rules_scala are hard-coded.

https://github.com/bazelbuild/rules_scala/blob/master/docs/phase_scalafmt.md

E.g. you define and use scalafmt_scala_binary or scalafmt_scala_library rules, then those won't be recognized as Scala, but rather Java rules.

In effect the external dependencies for such targets are not discovered by IntelliJ.

The reason is that the Scala targets generated by rules_scala do not expose a jdeps interface, hence intellij BlazeJavaWorkspaceImporter cannot find the dependencies for them (assuming they are not in the working set).

I've created a possible fix in gergelyfabian@265d376.

@gergelyfabian
Copy link
Author

gergelyfabian commented May 13, 2020

As rules_scala maintainer, @ittaiz, what do you think about this? Should it be solved the way I proposed in gergelyfabian@265d376, or do we need some changes also/rather to rules_scala, like making the scalafmt functionality into officially supported rule names?
Or maybe the rules_scala documentation should be changed to say that I should create rule names that I know that the plugin would recognize:

load("//scala:advanced_usage/scala.bzl", "make_scala_binary", "make_scala_library")
load("//scala/scalafmt:phase_scalafmt_ext.bzl", "ext_scalafmt")

scala_binary = make_scala_binary(ext_scalafmt)
scala_library = make_scala_library(ext_scalafmt)

And then use these alternative scala_library/scala_binary rules...

Anyhow, I think the root cause is that intellij plugin in hard coding the expected rules_scala rule names, while rules_scala provides a macro interface, that enables you create new scala rules with specific settings (that the Scalafmt docs suggest to use). I'd maybe call it an API mismatch.

@ittaiz
Copy link
Member

ittaiz commented May 15, 2020

That’s a great question.
Q- if we would have exposed jdeps then it wouldn’t have mattered how the rule is called (assuming JavaInfo of course)?
If so I think that maybe we should consider exposing jdeps (optionally maybe?).

Main problem is that the IJ team capacity for community related changes like generic support of scala or something is very limited these days (AFAIK)

@gergelyfabian
Copy link
Author

gergelyfabian commented May 15, 2020 via email

@ittaiz
Copy link
Member

ittaiz commented May 15, 2020

  1. I think custom scala support was needed for a few reasons and not only target identification so maybe more will be lost?
  2. Need to remember jdeps support doesn’t come for free (impl, maintenance, performance)
  3. Sounds like a documentation PR in rules_scala would definitely be valuable for now

@gergelyfabian
Copy link
Author

Regarding the documentation PR, what should be the general advice at this moment?

Mention that intellij plugin supports only a set of names, and using custom-named rules, defined by using macros (including the call to add scalafmt support) makes intellij plugin not recognize those as Scala targets? That if you need extended support from rules_scala (including scalafmt), you should rather name those rules the same way as in rules_scala (and use them from your own scope)? Is this the best workaround we have currently? Anything else?

@ittaiz
Copy link
Member

ittaiz commented May 18, 2020

I think so.
Anything else you can think of?

@gergelyfabian
Copy link
Author

gergelyfabian commented May 20, 2020

I sent the documentation PR for rules_scala: bazelbuild/rules_scala#1047

@mai93 mai93 added lang: scala Scala rules integration type: bug labels Feb 11, 2021
@odisseus
Copy link
Contributor

odisseus commented May 5, 2022

I found a workaround for this issue that does not require forking the entire Bazel plugin.

It is possible to create a tiny plugin that would extend the Bazel plugin with a custom TargetKindProvider which would define the language and rule type for custom kind names.

This should go into plugin.xml:

<extensions defaultExtensionNs="com.google.idea.blaze">
  <TargetKindProvider implementation="com.foobar.bazel.kind.FoobarScalaBlazeRules"/>
</extensions>

The kind provider code could look like this:

package com.foobar.bazel.kind;

import com.google.common.collect.ImmutableSet;
import com.google.idea.blaze.base.model.primitives.Kind;
import com.google.idea.blaze.base.model.primitives.LanguageClass;
import com.google.idea.blaze.base.model.primitives.RuleType;

public class FoobarScalaBlazeRules implements Kind.Provider {

  private static final Kind SCALA_FOOBAR_LIBRARY =
      Kind.Provider.create("_scala_foobar_library", LanguageClass.SCALA, RuleType.LIBRARY);

  @Override
  public ImmutableSet<Kind> getTargetKinds() {
    return ImmutableSet.of(SCALA_FOOBAR_LIBRARY);
  }
}

Of course, you would have to build this plugin and distribute it within your organization, but this is probably easier than maintaining a fork of the Bazel plugin and tracking the upstream changes.

@github-actions
Copy link

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 github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label Mar 16, 2023
@gergelyfabian
Copy link
Author

I think this is still valid.

@github-actions github-actions bot removed the stale Issues or PRs that are stale (no activity for 30 days) label Mar 23, 2023
@sgowroji sgowroji added the awaiting-maintainer Awaiting review from Bazel team on issues label Mar 23, 2023
@sgowroji sgowroji added the product: IntelliJ IntelliJ plugin label Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-maintainer Awaiting review from Bazel team on issues lang: scala Scala rules integration product: IntelliJ IntelliJ plugin type: bug
Projects
None yet
Development

No branches or pull requests

6 participants