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

sbt-scalafix should be able to read resolver credentials from the filesystem #1571

Closed
nasadorian opened this issue Mar 22, 2022 · 5 comments
Closed

Comments

@nasadorian
Copy link

When overriding the resolvers used by sbt-scalafix, we use the scalafixResolvers key to specify custom (internal corporate) resolvers. The types used by this key are different from SBT's Resolver class and are instead from the Coursier API, and they only accept credentials programmatically. The plugin does not seem to respect the contents of ~/.sbt/repositories nor ~/.ivy2/.credentials nor ~/.config/coursier/credentials.properties.

It would be nice to have sbt-scalafix adhere to the same resolvers as SBT itself, though I'm not sure how feasible that is.
As a stop-gap, what if the plugin could read credentials files instead to configure itself? Right now the only solutions I see when using custom resolvers are to read user and pass from environment variables or parse an existing credentials file like the below.

lazy val ivyToCoursierCreds: Credentials = {
  val creds = scala.io.Source
    .fromFile(Path.userHome / ".ivy2" / ".credentials")
    .getLines()
    .foldLeft(Map.empty[String, String]) { case (m, l) =>
      l.trim.split("=") match {
        case Array("user", user) => m + ("user" -> user)
        case Array("password", password) => m + ("password" -> password)
        case _ => m
      }
    }
  Credentials.of(creds("user"), creds("password"))
}
@bjaglin
Copy link
Collaborator

bjaglin commented Mar 22, 2022

Thanks for the report. I agree it's currently a pain for corporate users that resolvers and credentials are not inherited from sbt.

The plugin does not seem to respect the contents of ~/.sbt/repositories nor ~/.ivy2/.credentials nor ~/.config/coursier/credentials.properties.

The last file seems to be opened at least, maybe you are not using the right format?

➜  sbt-scalafix git:(main) ✗ touch ~/.config/coursier/credentials.properties
➜  sbt-scalafix git:(main) ✗ chmod 000 ~/.config/coursier/credentials.properties                                         
➜  sbt-scalafix git:(main) ✗ sbt scalafixAll
[info] welcome to sbt 1.6.2 (Ubuntu Java 11.0.14)
[info] loading global plugins from /home/brice/.sbt/1.0/plugins
[info] loading settings for project sbt-scalafix-build-build from plugins.sbt ...
[info] loading project definition from /home/brice/git/opensource/sbt-scalafix/project/project
[info] loading settings for project sbt-scalafix-build from plugins.sbt ...
[info] loading project definition from /home/brice/git/opensource/sbt-scalafix/project
[info] loading settings for project sbt-scalafix from build.sbt ...
[info] Welcome to sbt-scalafix 0.9.34+23-0a4dfc6a+20220322-2103-SNAPSHOT
[error] java.nio.file.AccessDeniedException: /home/brice/.config/coursier/credentials.properties
[error] 	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
[error] 	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
[error] 	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
[error] 	at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:219)
[error] 	at java.base/java.nio.file.Files.newByteChannel(Files.java:371)
[error] 	at java.base/java.nio.file.Files.newByteChannel(Files.java:422)
[error] 	at java.base/java.nio.file.Files.readAllBytes(Files.java:3206)
[error] 	at coursierapi.shaded.coursier.credentials.FileCredentials.get(FileCredentials.scala:22)
[error] 	at coursierapi.shaded.coursier.cache.FileCache.$anonfun$allCredentials0$1(FileCache.scala:51)
[error] 	at coursierapi.shaded.scala.collection.StrictOptimizedIterableOps.flatMap(StrictOptimizedIterableOps.scala:118)
[error] 	at coursierapi.shaded.scala.collection.StrictOptimizedIterableOps.flatMap$(StrictOptimizedIterableOps.scala:105)
[error] 	at coursierapi.shaded.scala.collection.immutable.ArraySeq.flatMap(ArraySeq.scala:35)
[error] 	at coursierapi.shaded.coursier.cache.FileCache.allCredentials0$lzycompute(FileCache.scala:51)
[error] 	at coursierapi.shaded.coursier.cache.FileCache.allCredentials0(FileCache.scala:50)
[error] 	at coursierapi.shaded.coursier.cache.FileCache.$anonfun$allCredentials$1(FileCache.scala:54)
[error] 	at coursierapi.shaded.coursier.util.Task$.wrap(Task.scala:82)
[error] 	at coursierapi.shaded.coursier.util.Task$.$anonfun$delay$2(Task.scala:47)
[error] 	at coursierapi.shaded.scala.concurrent.Future$.$anonfun$apply$1(Future.scala:678)
[error] 	at coursierapi.shaded.scala.concurrent.impl.Promise$Transformation.run(Promise.scala:467)
[error] 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[error] 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[error] 	at java.base/java.lang.Thread.run(Thread.java:829)
[error] (scalafixAll) java.nio.file.AccessDeniedException: /home/brice/.config/coursier/credentials.properties
[error] Total time: 0 s, completed Mar 22, 2022, 9:03:59 PM

Check out scalameta/sbt-scalafmt#108 (comment), since it's essentially the same environment.

@nasadorian
Copy link
Author

Thanks @bjaglin I'm going to experiment with that coursier file and see if we can make it work. Will report back!

@bjaglin
Copy link
Collaborator

bjaglin commented Jun 30, 2023

@nasadorian any update on this?

@bjaglin
Copy link
Collaborator

bjaglin commented Feb 5, 2024

@nasadorian please reopen if you still have the issue

@nasadorian
Copy link
Author

@bjaglin Thanks for the followup, and I don't remember what ended up happening with this. I haven't written Scala since the year this issue was opened, sadly.

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

No branches or pull requests

2 participants