Skip to content

Commit 026e507

Browse files
authored
Set workspace dir to os.tmp for virtual sources (#1771)
1 parent 6628baa commit 026e507

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

modules/build/src/main/scala/scala/build/input/Inputs.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ object Inputs {
157157
)
158158
(f.path / os.up, true, WorkspaceOrigin.SourcePaths)
159159
}
160+
}.orElse {
161+
validElems.collectFirst {
162+
case _: Virtual =>
163+
(os.temp.dir(), true, WorkspaceOrigin.VirtualForced)
164+
}
160165
}.getOrElse((os.pwd, true, WorkspaceOrigin.Forced))
161166
}
162167

modules/build/src/main/scala/scala/build/input/WorkspaceOrigin.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ object WorkspaceOrigin {
99

1010
case object ResourcePaths extends WorkspaceOrigin
1111

12-
case object HomeDir extends WorkspaceOrigin
12+
case object HomeDir extends WorkspaceOrigin
13+
case object VirtualForced extends WorkspaceOrigin
1314
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,28 @@ trait RunSnippetTestDefinitions { _: RunTestDefinitions =>
144144
expect(res.out.trim() == expectedOutput)
145145
}
146146
}
147+
test("running a script snippet should not create the workspace dir in cwd") {
148+
emptyInputs.fromRoot { root =>
149+
val quotation = TestUtil.argQuotationMark
150+
val msg = "Hello World from snippet"
151+
os.proc(TestUtil.cli, "-e", s"println($quotation$msg$quotation)", extraOptions)
152+
.call(cwd = root)
153+
.out.trim()
154+
expect(!os.exists(root / Constants.workspaceDirName))
155+
}
156+
}
157+
test("running a script snippet with one source file should create the workspace dir in cwd") {
158+
val inputs = TestInputs(
159+
os.rel / "Hello.scala" ->
160+
s"""object Hello {
161+
| val hello = "Hello World"
162+
|}""".stripMargin
163+
)
164+
inputs.fromRoot { root =>
165+
os.proc(TestUtil.cli, "-e", s"println(Hello.hello)", ".", extraOptions)
166+
.call(cwd = root)
167+
.out.trim()
168+
expect(os.exists(root / Constants.workspaceDirName))
169+
}
170+
}
147171
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
176176
passArgumentsScala3()
177177
}
178178

179-
test("setting root dir with no inputs") {
179+
test("setting root dir with virtual input") {
180180
val url = "https://gist.github.com/alexarchambault/7b4ec20c4033690dd750ffd601e540ec"
181181
emptyInputs.fromRoot { root =>
182182
os.proc(TestUtil.cli, extraOptions, escapedUrls(url)).call(cwd = root)
183-
expect(os.exists(root / ".scala-build"))
183+
expect(
184+
!os.exists(root / ".scala-build")
185+
) // virtual source should not create workspace dir in cwd
184186
}
185187
}
186188

0 commit comments

Comments
 (0)