sbt / sbt

sbt, the interactive build tool
https://scala-sbt.org
Apache License 2.0
4.8k stars 935 forks source link

with `run / fork := true` I can't take user input from console #7055

Closed ajits089 closed 1 year ago

ajits089 commented 1 year ago

steps

import Dependencies._
import sbt.Keys.{libraryDependencies, resolvers}
import sbt.Resolver

ThisBuild / scalaVersion := "3.1.0"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
ThisBuild / scalacOptions ++= Seq("-language:implicitConversions", "-deprecation", "-feature")

val AkkaVersion = "2.6.17"
lazy val root = (project in file("."))
 .settings(
   name := "sensor-nws",
   libraryDependencies ++= Seq(
     "org.scalameta" %% "munit" % "0.7.26" % Test,
     excludes(("com.typesafe.akka" %% "akka-stream" % AkkaVersion).cross(CrossVersion.for3Use2_13)),
     excludes(("org.apache.spark" %% "spark-core" % "3.2.0").cross(CrossVersion.for3Use2_13)),
     excludes(("org.apache.spark" %% "spark-sql" % "3.2.0").cross(CrossVersion.for3Use2_13))),
   resolvers ++= Seq(Resolver.typesafeIvyRepo("releases"),
     "Artima Maven Repository" at "https://repo.artima.com/releases",
     "spark.jars.repositories" at "https://oss.sonatype.org/content/repositories/releases"
   )
 )

//netty-all replaces all these excludes
def excludes(m: ModuleID): ModuleID =
 m.exclude("io.netty", "netty-common").
   exclude("io.netty", "netty-handler").
   exclude("io.netty", "netty-transport").
   exclude("io.netty", "netty-buffer").
   exclude("io.netty", "netty-codec").
   exclude("io.netty", "netty-resolver").
   exclude("io.netty", "netty-transport-native-epoll").
   exclude("io.netty", "netty-transport-native-unix-common").
   exclude("javax.xml.bind", "jaxb-api").
   exclude("jakarta.xml.bind", "jaxb-api").
   exclude("javax.activation", "activation").
   exclude("jakarta.annotation", "jakarta.annotation-api").
   exclude("javax.annotation", "javax.annotation-api")

run / fork := true
Test / fork := true

problem

SBT doesn't take user input as expected!

Removing the run fork results in the hadoop jar missing and adding the fork run will not let take user input.

*if run / fork := true is removed from sbt then: Caused by: java.io.FileNotFoundException: /Users/ajitkumar/Downloads/flice/sensor-nws/target/bg-jobs/sbt_4be36759/target/135c9252/81ecd14d/hadoop-client-api-3.3.1.jar (No such file or directory)

if not removed the below code results in

    println("Enter the directory path")
    val dir = scala.io.StdIn.readLine()
    print("dir ->", dir)
[info] Enter the directory path
[error] Exception in thread "main" java.lang.NullPointerException

project: https://github.com/ajits089/sensor-nws stackOverflow: https://stackoverflow.com/questions/74132055/removing-run-fork-true-from-sbt-results-in-runtime-notfound-exception-a

expectation

Errors are misleading

notes

SethTisue commented 1 year ago

is the problem reproducible without any external dependencies, in a minimal self-contained build?

ajits089 commented 1 year ago

I tried to reproduce the issue with having just following in the build.sbt and a source file: build.sbt:

ThisBuild / scalaVersion := "3.1.0"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
  .settings(
    name := "sensor-nws"
  )

run / fork := true

source file:

object testObj extends App {
  println("submit the name")
  val name = scala.io.StdIn.readLine()
  println(s"name: $name")
}

result:

$ sbt run
[info] welcome to sbt 1.7.2 (Amazon.com Inc. Java 1.8.0_282)
[info] loading global plugins from /Users/ajitkumar/.sbt/1.0/plugins
[info] loading project definition from /Users/ajitkumar/Downloads/test123/project
[info] loading settings for project root from build.sbt ...
[info] set current project to sensor-nws (in build file:/Users/ajitkumar/Downloads/test123/)
[info] compiling 1 Scala source to /Users/ajitkumar/Downloads/test123/target/scala-3.1.0/classes ...
[info] running (fork) testObj 
[info] submit the name
[info] name: null
[success] Total time: 6 s, completed 21 Oct, 2022 5:08:00 PM

Please let me know if you need any other info.

SethTisue commented 1 year ago

duplicate of https://github.com/sbt/sbt/issues/229 ?

note that connectInput in run := true is old syntax; modern syntax would be run / connectInput := true

(just as a small aside, your report was difficult to read because it lacked Markdown code fences to delimit the code. @adpi2 and I have edited your comments to use them — I hope you'll use them in future comments & reports)

ajits089 commented 1 year ago

The problem gets solved after adding run / connectInput := true to build.sbt. Thank you @SethTisue