Skip to content
New issue

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

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

Already on GitHub? # to your account

[2.x] import sbt.given and source compatibility with sbt 1.x #7737

Closed
adpi2 opened this issue Oct 8, 2024 · 9 comments
Closed

[2.x] import sbt.given and source compatibility with sbt 1.x #7737

adpi2 opened this issue Oct 8, 2024 · 9 comments
Milestone

Comments

@adpi2
Copy link
Member

adpi2 commented Oct 8, 2024

In sbt 2.0.0-M2 we translated a few implicit def to given or Conversion. Those new context abstractions in Scala 3 requires the explicit import sbt.given.

This makes it hard to cross-compile some plugins, because the import sbt.given is rejected by the Scala 2 compiler:

error] /home/piquerez/github/sbt/sbt-buildinfo/src/main/scala/sbtbuildinfo/BuildInfoPlugin.scala:5:8: object given is not a member of package sbt
[error] import sbt.given
[error]        ^
[error] one error found

I suggest we revert back to using implicit def in sbt 2.x, to fix this issue.

@adpi2 adpi2 added this to the 2.0.0 milestone Oct 8, 2024
@adpi2
Copy link
Member Author

adpi2 commented Oct 8, 2024

For instance, in sbt-buildinfo: sbt/sbt-buildinfo#207 (comment)

@adpi2
Copy link
Member Author

adpi2 commented Oct 8, 2024

Also in https://github.com/playframework/twirl/pull/840/files#diff-a2e196749d6ef985b6b6726915ccae8d35957839730761750e748a54bffeaef8R10

Interesting! So it seems Scala 2.12 is lenient on import sbt.{given, _} but not on sbt.given.

That's enough to fix the issue. But it should be mentioned in the sbt 2.x migration guide.

@adpi2 adpi2 closed this as completed Oct 8, 2024
@jtjeferreira
Copy link
Contributor

Also in playframework/twirl#840 (files)

Interesting! So it seems Scala 2.12 is lenient on import sbt.{given, _} but not on sbt.given.

That's enough to fix the issue. But it should be mentioned in the sbt 2.x migration guide.

I misunderstood you then. I though you wanted to facilitate the migration/cross compilation, so we don't have to change import sbt._ to import sbt.{given,_}. It was a facing this problem in some slash syntax that was failing to compile...

@adpi2
Copy link
Member Author

adpi2 commented Oct 8, 2024

I misunderstood you then. I though you wanted to facilitate the migration/cross compilation, so we don't have to change import sbt._ to import sbt.{given,_}. It was a facing this problem in some slash syntax that was failing to compile...

I thought it was almost impossible to cross-compile.. But since the given import is the new convention in Scala 3, I think it's fine to require it when cross-compiling.

@jtjeferreira
Copy link
Contributor

I misunderstood you then. I though you wanted to facilitate the migration/cross compilation, so we don't have to change import sbt._ to import sbt.{given,_}. It was a facing this problem in some slash syntax that was failing to compile...

I thought it was almost impossible to cross-compile.. But since the given import is the new convention in Scala 3, I think it's fine to require it when cross-compiling.

I am mostly still programming with scala2, so excuse me in advance if I am missing something obvious, but if we do what you suggested ("revert back to using implicit def in sbt 2.x"), wouldn't import sbt._ be enough?

@adpi2
Copy link
Member Author

adpi2 commented Oct 8, 2024

Generally, a normal wildcard selector * brings all definitions other than givens or extensions into scope whereas a given selector brings all givens (including those resulting from extensions) into scope.

There are two main benefits arising from these rules:

  • It is made clearer where givens in scope are coming from. In particular, it is not possible to hide imported givens in a long list of regular wildcard imports.
  • It enables importing all givens without importing anything else. This is particularly important since givens can be anonymous, so the usual recourse of using named imports is not practical.

https://docs.scala-lang.org/scala3/reference/contextual/given-imports.html

@jtjeferreira
Copy link
Contributor

docs.scala-lang.org/scala3/reference/contextual/given-imports.html

Thanks.

In some version after 3.1, old-style implicits accessed through a * wildcard import will give a compiler error.

In which version this happened? 3.3?

Inspired by that migration section, what do you think about sbt 2.0.x using old style implicits, and sbt 2.1.x using given?

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

No branches or pull requests

3 participants