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

Switch to JarsToLabels provider and rework/cleanup scala_import #477

Closed
wants to merge 5 commits into from
Closed

Switch to JarsToLabels provider and rework/cleanup scala_import #477

wants to merge 5 commits into from

Conversation

andyscott
Copy link
Contributor

@andyscott andyscott commented Apr 9, 2018

WIP.

This reworks scala_import under the assumption that you're using a newish version of Bazel. Jars-to-labels mappings are now provided with a provider.

I haven't tested this yet with IntelliJ.

@andyscott
Copy link
Contributor Author

Currently failing on the strict deps tests (Test "bazel build test/... --strict_java_deps=ERROR" failed (39 sec)). I'll update this over the next few days.

@@ -1,114 +1,49 @@
#intellij part is tested manually, tread lightly when changing there
#if you change make sure to manually re-import an intellij project and see imports
#are resolved (not red) and clickable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we delete the comment here? Seems like it is still relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overlooked it while porting this code over. I'll add it back (and actually prove out that this works for IntelliJ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added back in the body of the method.

@andyscott
Copy link
Contributor Author

Now we're failing a bit further:

'bazel build test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_user' should have logged "buildozer 'add deps //test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency' //test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_user".

My jars2deps isn't fully comprehensive... yet.

@ittaiz
Copy link
Member

ittaiz commented Apr 10, 2018

What’s the motivation here?
One motivation I think I see is move to providers (which should be a pure refactor) but you also mention:
“One notable change is that exported dependencies aren't included on the JavaInfo's compile_jars but they are available via transitive_compile_time_jars.”
Why did you do this change? This is a significant feature for us since we’re using strict-deps

@andyscott
Copy link
Contributor Author

I was aiming to clean up the code and use newer APIs. The export change has been redacted in the more recent commit and was there simply because I hadn't tried all the tests yet.

The changes still aren't passing yet, I'll take a look later today.

@ittaiz
Copy link
Member

ittaiz commented Apr 10, 2018

Two additional points:

  1. There's discussion in the bazel space about killing the Jars2Labels solution in favor of encoding the jar's label in the ijar manifest. There are caveats to that (mainly with exports) but you should probably keep that in mind (not blocking this PR).
  2. The Jars2Labels provider IMHO isn't part of scala_import since scala_* rules also use it and even were the first to propagate it so WDYT about moving it to its own bzl file?

@andyscott andyscott changed the title [WIP] Rework/cleanup scala_import Switch to JarsToLabels provider and rework/cleanup scala_import Apr 10, 2018
@andyscott
Copy link
Contributor Author

  1. Makes sense to me. Related, I'm fairly certain we could further isolate/decouple the Jars2Labels calculation code by moving it into an aspect that the scala_library and related rules leverage.
  2. Also makes sense to me. I was thinking about having a separate providers.bzl file where public/shared providers can live.

@ittaiz
Copy link
Member

ittaiz commented Apr 10, 2018

Makes sense to me. Related, I'm fairly certain we could further isolate/decouple the Jars2Labels calculation code by moving it into an aspect that the scala_library and related rules leverage.

How would we use the aspect when we need to get the list in the compilation action? I'm very rusty w.r.t aspects but I sort of remembered aspects are for meta-execution

def _scala_import_impl(ctx):
target_data = _code_jars_and_intellij_metadata_from(ctx.attr.jars)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andyscott why did you remove all the private methods? current code feels a bit like a wall of text

@andyscott
Copy link
Contributor Author

When defining a rule you can specify that an aspect be automatically evaluated for a particular attribute. A jars-to-labels aspect could evaluate over all deps, runtime_deps, etc, for scala rules and automatically provide the JarsToLabels info.

I don't think it's work exploring that in this PR but it might be a nice way to organize the logic down the road.

@jjudd
Copy link
Contributor

jjudd commented Apr 13, 2018

I ran into #476 and noticed this PR to refactor scala_import. I opened a PR to this branch to add a DefaultInfo with files: https://github.com/andyscott/rules_scala/pull/1

I'm not very familiar with the DefaultInfo provider, and the doc is a bit sparse. Hopefully this is the right approach.

@ittaiz
Copy link
Member

ittaiz commented Apr 13, 2018

I think there is a bug here with the exports.
Previously we had a comment saying the labels of exports+current target are added "last to override the label of the export compile jars to the current target" but this PR break this.
The bummer is that there is no test for this so I think we should add a failing test and then reverse the order (or maybe just use ctx.label for the overrides)

@ittaiz
Copy link
Member

ittaiz commented Apr 13, 2018

I've tried to write the failing test but couldn't 😠
I still feel there is an issue here, @andyscott can you take a stab at a failing test?
The behavior needs to be that if I have:
A->B->C->(exports)->D
and A transitively uses D the label will be of C
Note that scala_library has a different implementation for how it handles exports (though same result) so I think this is part of why it's hard to recreate (and why I think there's a 10% chance there's no bug).

From the code it sounds like your current version (where exports get their label and not the target's label) sounds wrong but we should probably have a test for it.

@andyscott
Copy link
Contributor Author

This definitely needs a bit more work and the testing is tricky. I can take a look over the second half of next week.

Add files to DefaultInfo provider in scala_import
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@andyscott
Copy link
Contributor Author

@jjudd looks like @googlebot is really on top of its game and you'll need to sign the CLA.

@ittaiz
Copy link
Member

ittaiz commented Apr 13, 2018 via email

@jjudd
Copy link
Contributor

jjudd commented Apr 13, 2018

I consent to my contributions going in

for file in jar.files:
all_jar_files.append(file)
if not file.basename.endswith("-sources.jar"):
direct_binary_jars += [file]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use += here but append above? Can we use append here too?

Copy link
Contributor Author

@andyscott andyscott Apr 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's proof that I am not as consistent as I wish to be. (Good catch, I'll fix it!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that I wrote the .append and andy wrote the +=. It seems it is proof that I don't always read the surrounding code as well as I should :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're touching this, wdyt about moving it to a private method as well? same level of abstraction for the top level

for file in jar.files:
all_jar_files.append(file)
if not file.basename.endswith("-sources.jar"):
direct_binary_jars += [file]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're touching this, wdyt about moving it to a private method as well? same level of abstraction for the top level

def _scala_import_java_info(ctx, direct_binary_jars):
# merge all deps, exports, and runtime deps into single JavaInfo instances

s_deps = java_common.merge(_collect(JavaInfo, ctx.attr.deps))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the s_ notation and do we need it?

for entry in ctx.attr.exports:
if JavaInfo in entry:
for jar in entry[JavaInfo].compile_jars:
lookup[jar.path] = entry.label
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we won't change this to ctx.label (per exports discussion) then can we refactor these two loops to a common method? if ctx.label doesn't change they're exactly the same

@jjudd
Copy link
Contributor

jjudd commented Apr 18, 2018

I'm diving in to this PR + IntelliJ today. I've been running off of a branch that uses it and I'm experiencing some red, non-clickable symbols and trying to figure out if this is the cause.

@johnynek
Copy link
Member

can we make sure to merge master on this?

I added a test that we don't use incompatible features and it is easy to break that test since we don't automatically retest against master.

@jjudd
Copy link
Contributor

jjudd commented Apr 19, 2018

I did some digging into the IntelliJ plugin today. It doesn't look like it supports the new JavaInfo provider. It appears to be a hybrid of the legacy java and java_output providers.

I'm also not certain how to create a JavaInfo provider that has the information we need, considering that java_common.create_provider is deprecated in favor of the JavaInfo provider. I asked more about that here: https://groups.google.com/forum/#!topic/bazel-discuss/hQ7qOoZ53Fw

I think that JavaInfo now has everything needed to support the IntelliJ plugin. I'm not 100% sure, but an initial reading of the code suggests everything is there.

Based on the responses I get on my thread in bazel-discuss, I may take a crack at modifying the IntelliJ plugin to support the new JavaInfo as well as the legacy provider.

I see two options for this PR:

  1. We modify the IntelliJ plugin to support JavaInfo
  2. We return a legacy java provider everywhere we return a JavaInfo provider.

Option 2 would unblock this PR from being merged as soon as it is finished, however it's cumbersome. Option 1 would unblock the PR as soon as the new IntelliJ plugin is released, assuming we are ok with making a backwards incompatible change.

@ittaiz
Copy link
Member

ittaiz commented Apr 19, 2018 via email

@jjudd
Copy link
Contributor

jjudd commented Apr 19, 2018

I think we should move scala_library to the new provider. I don't think it should be part of this PR, unless there's a reason to move both at the same time.

@ittaiz
Copy link
Member

ittaiz commented Apr 19, 2018 via email

@andyscott
Copy link
Contributor Author

Perhaps we can refactor so that provider creation is handled by a private method that can initially create the legacy structures to support IntelliJ? In parallel, we can work to update the IntelliJ plugin to support JavaInfo.

@jjudd
Copy link
Contributor

jjudd commented Apr 19, 2018

@andyscott I like that idea. I'll take a stab at that today.

@jjudd
Copy link
Contributor

jjudd commented Apr 19, 2018

@ittaiz let's start here, see how this PR goes, and based on that and the label stamping work let's decide what we want to do moving forward.

@jjudd
Copy link
Contributor

jjudd commented Apr 20, 2018

I added IntelliJ support, rebased master, and fixed the incompatible errors. The PR to @andyscott's branch is here https://github.com/andyscott/rules_scala/pull/2.

The incompatible errors are fixed in this commit: andyscott@3dd70d7

The IntelliJ support is in this commit: andyscott@a457c06

@jjudd
Copy link
Contributor

jjudd commented Apr 20, 2018

The IntelliJ support this time around includes support for source jars. The IntelliJ plugin needs a modification to automatically identify them. Until then you need to click 'Attach downloaded source' or 'Attach source' in IntelliJ to get them to show up.

@johnynek
Copy link
Member

can we send a PR to the intellj plugin to get it able to use JavaInfo? I would really like to move to modern providers everywhere and not keep the old style we have currently.

@jjudd
Copy link
Contributor

jjudd commented Apr 20, 2018

I'm happy to work on that as well. I will have time next week. We're close to switching all of engineering over to Bazel and are pushing hard to get that done.

@johnynek
Copy link
Member

johnynek commented Apr 20, 2018 via email

@jjudd
Copy link
Contributor

jjudd commented Apr 20, 2018

Awesome. That's fantastic to hear. It's the next thing on our list.

@ittaiz
Copy link
Member

ittaiz commented Apr 29, 2018

ok, @andyscott @jjudd this PR feels a bit lost right now (thanks to all of us :) )
There are some unanswered comments I made, most importantly the one about exports.
This is no rush to me but just wanted to make sure we're not waiting on each other by mistake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants