Skip to content

Commit e0d4779

Browse files
Put current dir in PYTHONPATH in 'scala-cli repl --python'
1 parent 59964cf commit e0d4779

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

modules/cli/src/main/scala/scala/cli/commands/repl/Repl.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import scala.build.internal.{Constants, Runner}
1414
import scala.build.options.{BuildOptions, JavaOpt, MaybeScalaVersion, Scope}
1515
import scala.cli.CurrentParams
1616
import scala.cli.commands.publish.ConfigUtil.*
17-
import scala.cli.commands.run.Run.{maybePrintSimpleScalacOutput, orPythonDetectionError}
17+
import scala.cli.commands.run.Run.{
18+
maybePrintSimpleScalacOutput,
19+
orPythonDetectionError,
20+
pythonPathEnv
21+
}
1822
import scala.cli.commands.run.RunMode
1923
import scala.cli.commands.shared.SharedOptions
2024
import scala.cli.commands.{ScalaCommand, WatchUtil}
@@ -264,20 +268,25 @@ object Repl extends ScalaCommand[ReplOptions] {
264268
}
265269
.params
266270

267-
val scalapyJavaOpts =
271+
val (scalapyJavaOpts, scalapyExtraEnv) =
268272
if (setupPython) {
269273
val props = value {
270274
val python = Python()
271275
val propsOrError = python.scalapyProperties
272276
logger.debug(s"Python Java properties: $propsOrError")
273277
propsOrError.orPythonDetectionError
274278
}
275-
props.toVector.sorted.map {
279+
val props0 = props.toVector.sorted.map {
276280
case (k, v) => s"-D$k=$v"
277281
}
282+
// Putting current dir in PYTHONPATH, see
283+
// https://github.com/VirtusLab/scala-cli/pull/1616#issuecomment-1333283174
284+
// for context.
285+
val dirs = buildOpt.map(_.inputs.workspace).toSeq ++ Seq(os.pwd)
286+
(props0, pythonPathEnv(dirs: _*))
278287
}
279288
else
280-
Nil
289+
(Nil, Map.empty[String, String])
281290

282291
def additionalArgs = {
283292
val pythonArgs =
@@ -365,7 +374,7 @@ object Repl extends ScalaCommand[ReplOptions] {
365374
maybeAdaptForWindows(replArgs),
366375
logger,
367376
allowExecve = allowExit,
368-
extraEnv = extraEnv
377+
extraEnv = scalapyExtraEnv ++ extraEnv
369378
).waitFor()
370379
if (retCode != 0)
371380
value(Left(new ReplError(retCode)))

modules/integration/src/test/scala/scala/cli/integration/ReplTestDefinitions.scala

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ abstract class ReplTestDefinitions(val scalaVersionOpt: Option[String])
4646
}
4747

4848
test("ammonite scalapy") {
49-
TestInputs.empty.fromRoot { root =>
49+
val inputs = TestInputs(
50+
os.rel / "foo" / "something.py" ->
51+
"""messageStart = 'Hello from'
52+
|messageEnd = 'ScalaPy'
53+
|""".stripMargin
54+
)
55+
inputs.fromRoot { root =>
5056
val ammArgs = Seq(
5157
"-c",
5258
"""println("Hello" + " from Scala " + scala.util.Properties.versionNumberString)
53-
|// py.Dynamic.global.print("Hello from", "ScalaPy") // doesn't work
54-
|println(py"'Hello from '" + py"'ScalaPy'")
59+
|val sth = py.module("foo.something")
60+
|py.Dynamic.global.applyDynamicNamed("print")("" -> sth.messageStart, "" -> sth.messageEnd, "flush" -> py.Any.from(true))
5561
|""".stripMargin
5662
)
5763
.map {
@@ -61,6 +67,24 @@ abstract class ReplTestDefinitions(val scalaVersionOpt: Option[String])
6167
identity
6268
}
6369
.flatMap(arg => Seq("--ammonite-arg", arg))
70+
71+
val errorRes = os.proc(
72+
TestUtil.cli,
73+
"repl",
74+
extraOptions,
75+
"--ammonite",
76+
"--python",
77+
ammArgs
78+
).call(
79+
cwd = root,
80+
env = Map("PYTHONSAFEPATH" -> "foo"),
81+
mergeErrIntoOut = true,
82+
check = false
83+
)
84+
expect(errorRes.exitCode != 0)
85+
val errorOutput = errorRes.out.text()
86+
expect(errorOutput.contains("No module named 'foo'"))
87+
6488
val res = os.proc(
6589
TestUtil.cli,
6690
"repl",

0 commit comments

Comments
 (0)