Skip to content

Commit

Permalink
Ensure resource directories passed via a using directive aren't ign…
Browse files Browse the repository at this point in the history
…ored in `--watch` mode
  • Loading branch information
Gedochao committed Oct 18, 2024
1 parent f4dd80d commit b198d43
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
35 changes: 22 additions & 13 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,19 @@ object CrossSources {
)
}).flatten

val resourceDirectoriesFromDirectives = {
val resourceDirsFromCli =
allInputs.elements.flatMap { case rd: ResourceDirectory => Some(rd.path); case _ => None }
val latestResourceDirsFromBuildOptions: Seq[os.Path] =
buildOptions.flatMap(_.value.classPathOptions.resourcesDir).distinct
latestResourceDirsFromBuildOptions
.filter(!resourceDirsFromCli.contains(_))
.map(ResourceDirectory(_))
}
val finalInputs = allInputs.add(resourceDirectoriesFromDirectives)

val defaultMainElemPath = for {
defaultMainElem <- allInputs.defaultMainClassElement
defaultMainElem <- finalInputs.defaultMainClassElement
} yield defaultMainElem.path

val pathsWithDirectivePositions
Expand All @@ -284,7 +295,7 @@ object CrossSources {
val baseReqs0 = baseReqs(d.scopePath)
WithBuildRequirements(
d.requirements.fold(baseReqs0)(_ orElse baseReqs0),
(d.path, d.path.relativeTo(allInputs.workspace))
(d.path, d.path.relativeTo(finalInputs.workspace))
) -> d.directivesPositions
}
val inMemoryWithDirectivePositions
Expand All @@ -309,7 +320,7 @@ object CrossSources {
}

val resourceDirs: Seq[WithBuildRequirements[os.Path]] =
resolveResourceDirs(allInputs, preprocessedSources)
resolveResourceDirs(finalInputs, preprocessedSources)

lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
Expand Down Expand Up @@ -362,17 +373,15 @@ object CrossSources {
val paths = pathsWithDirectivePositions.map(_._1)
val inMemory = inMemoryWithDirectivePositions.map(_._1)
val unwrappedScripts = unwrappedScriptsWithDirectivePositions.map(_._1)
(
CrossSources(
paths,
inMemory,
defaultMainElemPath,
resourceDirs,
buildOptions,
unwrappedScripts
),
allInputs
val crossSources = CrossSources(
paths,
inMemory,
defaultMainElemPath,
resourceDirs,
buildOptions,
unwrappedScripts
)
crossSources -> finalInputs
}

/** @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,34 @@ trait RunWithWatchTestDefinitions { _: RunTestDefinitions =>
}
}
}

for {
useDirective <- Seq(false, true)
// TODO make this pass reliably on Mac CI
if !Properties.isMac || !TestUtil.isCI
directive = if (useDirective) "//> using resourceDirs ./resources" else ""
resourceOptions = if (useDirective) Nil else Seq("--resource-dirs", "./src/proj/resources")
title = if (useDirective) "directive" else "command line"
} test(s"resources via $title with --watch") {
val expectedMessage1 = "Hello"
val expectedMessage2 = "world"
resourcesInputs(directive = directive, resourceContent = expectedMessage1)
.fromRoot { root =>
TestUtil.withProcessWatching(
os.proc(TestUtil.cli, "run", "src", "--watch", resourceOptions, extraOptions)
.spawn(cwd = root, stderr = os.Pipe)
) { (proc, timeout, ec) =>
val output1 = TestUtil.readLine(proc.stdout, ec, timeout)
expect(output1 == expectedMessage1)
proc.printStderrUntilRerun(timeout)(ec)
val Some((resourcePath, newResourceContent)) =
resourcesInputs(directive = directive, resourceContent = expectedMessage2)
.files
.find(_._1.toString.contains("resources"))
os.write.over(root / resourcePath, newResourceContent)
val output2 = TestUtil.readLine(proc.stdout, ec, timeout)
expect(output2 == expectedMessage2)
}
}
}
}

0 comments on commit b198d43

Please sign in to comment.