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

Move JDK shims to munit.internal #441

Merged
merged 7 commits into from
Apr 10, 2022
Merged

Conversation

armanbilge
Copy link
Contributor

Will fix #437.

Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Thank you for this contribution! Looks good to me overall, just a few nitpick comments on undocumented conventions I try to keep in the codebase.

import java.util

import scala.collection.JavaConverters._

// Rough implementation of java.nio.Path, should work similarly for the happy
// path but has undefined behavior for error handling.
case class NodeNIOPath(filename: String) extends Path {
case class Path(filename: String) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's call this MUnitPath instead of Path. Or something else if you have suggestions. I don't like it when libraries use generic names for internal classes because it pollutes the namespace when you're auto-completing symbols.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, done.

@@ -1,10 +1,7 @@
package java.nio.file
package munit.internal
Copy link
Member

Choose a reason for hiding this comment

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

Let's introduce a new munit.internal.io package. The general structure I try to following is

  • flat list of classes/objects inside munit._ for public API
  • flat list of packages inside munit.internal._

The reason is mostly historical because MUnit includes a reimplementation of the java difflib and it didn't feel right to have all of those diff classes inside munit.internal._.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

package munit

package object internal {
type File = java.io.File
Copy link
Member

Choose a reason for hiding this comment

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

Can we add an object called PlatformIO that has these methods and aliases instead? I try to avoid package objects when possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the io-related ones to PlatformIO. I moved the reflect ones to PlatformCompat.

@olafurpg
Copy link
Member

You can run scalafixAll and scalafmtAll inside the sbt shell to fix the Scalafix error

@armanbilge
Copy link
Contributor Author

Thanks for the feedback and pointers. I'm still tracking down various linking issues for JS then I'll iterate on your feedback 👍

@olafurpg
Copy link
Member

Just a heads up, I've been thinking how to solve another issue #382 and just realized it's somewhat related to this issue. The root problem is that MUnit does file I/O at runtime, which is incompatible with JS in the browser and distributed build tools like Gradle and Bazel.

One alternative solution to this problem is to embed the full text contents of the file inside munit.Location. I avoided this approach in the past because I was concerned it would make the generated code too big (because every Location would have a fresh copy of the full source code). However, the string literal gets added to a constant pool so it only stored once. I validated that this is the case for JVM bytecode and Scala 3 tasty, but I haven't confirmed whether this is also true for Scala.js/Native IRs (I would be surprised if they don't have a constant pool).

@armanbilge
Copy link
Contributor Author

Uh-oh, maybe I should wait on this until you decide what you want to do? 😅 Removing I/O completely would be excellent for browsers and friends.

@olafurpg
Copy link
Member

You can try to implement this alternative approach if you're interested. The steps would be

  1. Add new textContents field to munit.Location, with a fallback constructor that defaults to textContents = ""
  2. Update the Scala 2 macro to inline the contents here
    New(c.mirror.staticClass(classOf[Location].getName()), path, line)
  3. Update the Scala 3 macro to inline the contents here
    '{ new Location(${Expr(path)}, ${Expr(startLine)}) }
  4. Update Lines.scala to use Location.textContents instead of file I/O

@olafurpg
Copy link
Member

I'm sorry for guiding you in the other direction, this idea didn't come up until after you opened the draft PR.

@olafurpg
Copy link
Member

The CI seems green so we can also merge this PR as is and I can look into the alternative later.

@armanbilge
Copy link
Contributor Author

armanbilge commented Oct 17, 2021

Apologies, getting a bit out-of-scope for me. As it stands this PR does the hard part of #437 by tracking down all the JDK stuff and condensing it in the internal package. All that's left is bike-shedding names according to unwritten convention which it seems you are the expert on 😉

@olafurpg
Copy link
Member

olafurpg commented Oct 17, 2021

All that's left is bike-shedding names according to unwritten convention which it seems you are the expert on

It's not bike-shedding IMO to follow conventions of existing code structure. I apologize if it seems like a waste of time or if your dissatisfied with the lack of documentation for how the code should be structured. I'm happy to merge this PR as is once the proposed renames and code structure suggestions have been implemented.

@olafurpg
Copy link
Member

I don't claim these conventions are the best conventions or that they're even standard conventions. They're just conventions that are part of this codebase, for better or worse (some of them are accidental while others are for historical reasons).

@armanbilge
Copy link
Contributor Author

Apologies, I regret my remark as it came across poorly I think 😕 I have ton of respect for projects and developers that keep things organized, extremely important for projects like this at the top of the dependency food chain. It's just hard for me to know exactly how you want things without a couple more iterations and it seems the future of this PR is dubious depending on some very good changes that might be forthcoming. So I'm happy to let this sit and if it's still relevant by the time 1.0 is solidifying then we can make it all perfect :)

@olafurpg
Copy link
Member

Sound good, we can let this PR wait. No worries!

@olafurpg
Copy link
Member

FYI I'm working on this issue here right now #41 and it may take a while to get through the finish line.

@armanbilge
Copy link
Contributor Author

@olafurpg we've been butting heads with this problem again in http4s/http4s#5758.

Would you be open to accepting the changes in this PR into 0.7.x series (after it's polished up)? I know it's technically a breaking change, but in fact anyone who experiences that breaking change likely had bugs/linking issues that were masked precisely because of these shims.

Thanks!

@armanbilge
Copy link
Contributor Author

armanbilge commented Jan 2, 2022

Oh, one more idea: if you really want to avoid a breaking change, we apply the changes here, and also move these shims into a new artifact. So anyone that experiences issues because of the change, can just add a new dependency to get the shims back (or alternatively, the shims can be included by default and downstreams where it causes a problem can remove that artifact explicitly).

@armanbilge armanbilge marked this pull request as ready for review April 9, 2022 13:34
@armanbilge
Copy link
Contributor Author

@olafurpg I've now renamed/re-organized everything as you suggested. Thanks for your patience on this one! :)

@armanbilge armanbilge requested a review from olafurpg April 9, 2022 13:35
Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

LGTM thank you!

@valencik valencik merged commit 68c2d13 into scalameta:main Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove Scala.js shims for JDK classes
3 participants