Skip to content

Commit b198d43

Browse files
committed
Ensure resource directories passed via a using directive aren't ignored in --watch mode
1 parent f4dd80d commit b198d43

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

modules/build/src/main/scala/scala/build/CrossSources.scala

+22-13
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,19 @@ object CrossSources {
273273
)
274274
}).flatten
275275

276+
val resourceDirectoriesFromDirectives = {
277+
val resourceDirsFromCli =
278+
allInputs.elements.flatMap { case rd: ResourceDirectory => Some(rd.path); case _ => None }
279+
val latestResourceDirsFromBuildOptions: Seq[os.Path] =
280+
buildOptions.flatMap(_.value.classPathOptions.resourcesDir).distinct
281+
latestResourceDirsFromBuildOptions
282+
.filter(!resourceDirsFromCli.contains(_))
283+
.map(ResourceDirectory(_))
284+
}
285+
val finalInputs = allInputs.add(resourceDirectoriesFromDirectives)
286+
276287
val defaultMainElemPath = for {
277-
defaultMainElem <- allInputs.defaultMainClassElement
288+
defaultMainElem <- finalInputs.defaultMainClassElement
278289
} yield defaultMainElem.path
279290

280291
val pathsWithDirectivePositions
@@ -284,7 +295,7 @@ object CrossSources {
284295
val baseReqs0 = baseReqs(d.scopePath)
285296
WithBuildRequirements(
286297
d.requirements.fold(baseReqs0)(_ orElse baseReqs0),
287-
(d.path, d.path.relativeTo(allInputs.workspace))
298+
(d.path, d.path.relativeTo(finalInputs.workspace))
288299
) -> d.directivesPositions
289300
}
290301
val inMemoryWithDirectivePositions
@@ -309,7 +320,7 @@ object CrossSources {
309320
}
310321

311322
val resourceDirs: Seq[WithBuildRequirements[os.Path]] =
312-
resolveResourceDirs(allInputs, preprocessedSources)
323+
resolveResourceDirs(finalInputs, preprocessedSources)
313324

314325
lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
315326
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
@@ -362,17 +373,15 @@ object CrossSources {
362373
val paths = pathsWithDirectivePositions.map(_._1)
363374
val inMemory = inMemoryWithDirectivePositions.map(_._1)
364375
val unwrappedScripts = unwrappedScriptsWithDirectivePositions.map(_._1)
365-
(
366-
CrossSources(
367-
paths,
368-
inMemory,
369-
defaultMainElemPath,
370-
resourceDirs,
371-
buildOptions,
372-
unwrappedScripts
373-
),
374-
allInputs
376+
val crossSources = CrossSources(
377+
paths,
378+
inMemory,
379+
defaultMainElemPath,
380+
resourceDirs,
381+
buildOptions,
382+
unwrappedScripts
375383
)
384+
crossSources -> finalInputs
376385
}
377386

378387
/** @return

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

+30
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,34 @@ trait RunWithWatchTestDefinitions { _: RunTestDefinitions =>
308308
}
309309
}
310310
}
311+
312+
for {
313+
useDirective <- Seq(false, true)
314+
// TODO make this pass reliably on Mac CI
315+
if !Properties.isMac || !TestUtil.isCI
316+
directive = if (useDirective) "//> using resourceDirs ./resources" else ""
317+
resourceOptions = if (useDirective) Nil else Seq("--resource-dirs", "./src/proj/resources")
318+
title = if (useDirective) "directive" else "command line"
319+
} test(s"resources via $title with --watch") {
320+
val expectedMessage1 = "Hello"
321+
val expectedMessage2 = "world"
322+
resourcesInputs(directive = directive, resourceContent = expectedMessage1)
323+
.fromRoot { root =>
324+
TestUtil.withProcessWatching(
325+
os.proc(TestUtil.cli, "run", "src", "--watch", resourceOptions, extraOptions)
326+
.spawn(cwd = root, stderr = os.Pipe)
327+
) { (proc, timeout, ec) =>
328+
val output1 = TestUtil.readLine(proc.stdout, ec, timeout)
329+
expect(output1 == expectedMessage1)
330+
proc.printStderrUntilRerun(timeout)(ec)
331+
val Some((resourcePath, newResourceContent)) =
332+
resourcesInputs(directive = directive, resourceContent = expectedMessage2)
333+
.files
334+
.find(_._1.toString.contains("resources"))
335+
os.write.over(root / resourcePath, newResourceContent)
336+
val output2 = TestUtil.readLine(proc.stdout, ec, timeout)
337+
expect(output2 == expectedMessage2)
338+
}
339+
}
340+
}
311341
}

0 commit comments

Comments
 (0)