Skip to content

Commit

Permalink
fixup! 24e4665 Don't use a separate regex for each adjusted option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaden Peterson committed Oct 30, 2024
1 parent 24e4665 commit 3d30e9e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
17 changes: 14 additions & 3 deletions rules/private/phases/phase_semanticdb.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ load(
)

def _semanticdb_directory_from_file(file):
return file.path[:file.path.find("META-INF") - 1]
"""
This is janky, but we're limited in what we can do in this function. From the
[documentation](https://bazel.build/rules/lib/builtins/Args#add_all) on `Args#add_all`:
To avoid unintended retention of large analysis-phase data structures into the execution phase,
the `map_each` function must be declared by a top-level `def` statement; it may not be a
nested function closure by default.
"""

return "{}/{}".format(file.root.path, file.short_path[:file.short_path.find("META-INF") - 1])

#
# PHASE: semanticdb
Expand Down Expand Up @@ -40,18 +49,20 @@ def phase_semanticdb(ctx, g):

if scala_configuration.version.startswith("2"):
arguments.add("--compiler_option=-P:semanticdb:failures:error")
arguments.add("--compiler_option_referencing_path=-P:semanticdb:sourceroot:${workDir}")
arguments.add_all(
[outputs[0]],
format_each = "--compiler_option_referencing_path=-P:semanticdb:targetroot:%s",
format_each = "--compiler_option_referencing_path=-P:semanticdb:targetroot:${path} %s",
map_each = _semanticdb_directory_from_file,
)
else:
arguments.add_all(
[outputs[0]],
format_each = "--compiler_option_referencing_path=-semanticdb-target:%s",
format_each = "--compiler_option_referencing_path=-semanticdb-target:${path} %s",
map_each = _semanticdb_directory_from_file,
)

arguments.add("--compiler_option_referencing_path=-sourceroot:${workDir}")
arguments.add("--compiler_option=-Ysemanticdb")

g.out.providers.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import common.sandbox.SandboxUtil
import net.sourceforge.argparse4j.impl.{Arguments => ArgumentsImpl}
import net.sourceforge.argparse4j.inf.{Argument, ArgumentParser, ArgumentType, Namespace}
import java.util.{Collections, List as JList}
import scala.annotation.nowarn
import scala.collection.mutable.Buffer
import scala.jdk.CollectionConverters.*
import java.io.File
Expand Down Expand Up @@ -61,22 +62,23 @@ object Analysis {
}

object CommonArguments {
private val scala2SemanticDbTargetRootRegex = """-P:semanticdb:targetroot:(.*)""".r
private val scala3SemanticDbTargetRootRegex = """-semanticdb-target:(.*)""".r

private def adjustCompilerOptions(workDir: Path, options: List[String]) = {
def adjustStringPath(path: String) =
SandboxUtil.getSandboxPath(workDir, Paths.get(path)).toString

options.flatMap {
case scala2SemanticDbTargetRootRegex(path) =>
List(s"-P:semanticdb:sourceroot:${workDir.toString}", s"-P:semanticdb:targetroot:${adjustStringPath(path)}")

case scala3SemanticDbTargetRootRegex(path) =>
List(s"-semanticdb-target:${adjustStringPath(path)}", s"-sourceroot:${workDir.toString}")

case option => List(option)
private val pathPlaceholderRegex = """\$\{path\}""".r
private def adjustCompilerOptions(workDir: Path, options: List[String]) = options.map { option =>
val i = option.lastIndexOf(' ')
val withPathReplaced = if (i == -1) {
option
} else {
val template = option.slice(0, i)
val path = option.slice(i + 1, option.length)

template.replace(
"${path}": @nowarn("cat=lint-missing-interpolator"),
SandboxUtil.getSandboxPath(workDir, Paths.get(path)).toString,
)
}

withPathReplaced
.replace("${workDir}": @nowarn("cat=lint-missing-interpolator"), workDir.toString)
}

/**
Expand Down

0 comments on commit 3d30e9e

Please sign in to comment.