Skip to content

Commit

Permalink
fix: zinc issue with duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyklim committed Jul 15, 2024
1 parent 1369dea commit f22998b
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 41 deletions.
7 changes: 0 additions & 7 deletions .bazelrc_shared
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ common --announce_rc
common --color=yes

build --experimental_strict_action_env
build --extra_toolchains=@local_jdk//:all

build --strategy=ScalaCompile=worker
build --worker_max_instances=4
Expand All @@ -13,9 +12,3 @@ build:rules --disk_cache=.bazel_cache
build:tests --disk_cache=../.bazel_cache

test --test_output=all
test --jvmopt='-Dfile.encoding=UTF-8'

test --test_env='LC_ALL=en_US.UTF-8'
test --test_env='LANG=en_US.UTF-8'
test --jvmopt='-Dsun.jnu.encoding=UTF-8'
test --jvmopt='-Dfile.encoding=UTF-8'
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions scala/workers/common/FileUtil.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package rules_scala
package workers.common

import scala.annotation.tailrec
import java.io.IOException
import java.io.{IOException, File}
import java.nio.channels.{FileChannel, FileLock}
import java.nio.file.{FileAlreadyExistsException, FileVisitResult, Files, OpenOption, Path, SimpleFileVisitor, StandardCopyOption, StandardOpenOption}
import java.nio.file.attribute.BasicFileAttributes
import java.security.SecureRandom
import java.util.zip.{ZipEntry, ZipInputStream, ZipOutputStream}

import scala.annotation.tailrec

class CopyFileVisitor(source: Path, target: Path) extends SimpleFileVisitor[Path]:
override def preVisitDirectory(directory: Path, attributes: BasicFileAttributes) =
try Files.createDirectory(target.resolve(source.relativize(directory)))
Expand Down Expand Up @@ -84,7 +85,7 @@ object FileUtil:
try
val zipStream = ZipInputStream(fileStream)

@tailrec def next(files: List[Path]): List[Path] =
@tailrec def next(files: List[File]): List[File] =
zipStream.getNextEntry match
case null => files
case entry: ZipEntry =>
Expand All @@ -96,7 +97,7 @@ object FileUtil:
Files.createDirectories(file.getParent)
Files.copy(zipStream, file, StandardCopyOption.REPLACE_EXISTING)
zipStream.closeEntry()
next(file :: files)
next(file.toFile :: files)

next(Nil)
finally fileStream.close()
2 changes: 1 addition & 1 deletion scala/workers/zinc/compile/main/Deps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final case class DepAnalysisFiles(apis: Path, relations: Path)
final case class ExternalDep(file: Path, classpath: Path, analysis: DepAnalysisFiles) extends Dep
final case class ExternalCachedDep(cachedPath: Path, file: Path, classpath: Path, analysis: DepAnalysisFiles) extends Dep

object Dep:
object Deps:
def sha1(file: Path): String =
val digest = MessageDigest.getInstance("SHA1")
BigInteger(1, digest.digest(Files.readAllBytes(file))).toString(16)
Expand Down
3 changes: 1 addition & 2 deletions scala/workers/zinc/compile/main/ZincPersistence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package workers.zinc.compile

import workers.common.FileUtil

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.{Files, Path}

trait ZincPersistence:
def load(): Unit
Expand Down
18 changes: 8 additions & 10 deletions scala/workers/zinc/compile/main/ZincRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import xsbti.compile.{
MiniSetup,
PerClasspathEntryLookup,
PreviousResult,
Setup
Setup,
TastyFiles
}

import rules_scala.common.worker.WorkerMain
Expand Down Expand Up @@ -113,7 +114,6 @@ object ZincRunner extends WorkerMain[ZincRunner.Arguments]:
val sourcesDir = workArgs.tmpDir.resolve("src")
val sources: collection.Seq[File] = workArgs.sources ++ workArgs.sourceJars.zipWithIndex
.flatMap((jar, i) => FileUtil.extractZip(jar, sourcesDir.resolve(i.toString)))
.map(_.toFile)

// extract upstream classes
val classesDir = workArgs.tmpDir.resolve("classes")
Expand All @@ -126,7 +126,7 @@ object ZincRunner extends WorkerMain[ZincRunner.Arguments]:
jar -> (classesDir.resolve(labelToPath(arg.label)), DepAnalysisFiles(arg.apis, arg.relations))
.toMap
case false => Map.empty
val deps = Dep.create(workerArgs.depsCache, workArgs.classpath, analyses)
val deps = Deps.create(workerArgs.depsCache, workArgs.classpath, analyses)

// load persisted files
val analysisFiles = AnalysisFiles(
Expand All @@ -139,10 +139,9 @@ object ZincRunner extends WorkerMain[ZincRunner.Arguments]:
val analysesFormat = AnxAnalyses(if workArgs.debug then AnxAnalysisStore.TextFormat else AnxAnalysisStore.BinaryFormat)
val analysisStore = AnxAnalysisStore(analysisFiles, analysesFormat)

val persistence = workerArgs.persistenceDir.fold[ZincPersistence](NullPersistence) { rootDir =>
val persistence = workerArgs.persistenceDir.fold[ZincPersistence](NullPersistence): rootDir =>
val path = labelToPath(workArgs.label)
FilePersistence(rootDir.resolve(path), analysisFiles, workArgs.outputJar)
}

val classesOutputDir = classesDir.resolve(labelToPath(workArgs.label))
Files.createDirectories(classesOutputDir)
Expand Down Expand Up @@ -192,21 +191,20 @@ object ZincRunner extends WorkerMain[ZincRunner.Arguments]:
case ExternalDep(_, classpath, files) => classpath -> files
case ExternalCachedDep(_, _, classpath, files) => classpath -> files
.toMap
val lookup = AnxPerClasspathEntryLookup(file =>
val lookup = AnxPerClasspathEntryLookup: file =>
depMap
.get(file)
.map(files =>
.map: files =>
Analysis.Empty.copy(
apis = analysesFormat.apis.read(files.apis),
relations = analysesFormat.relations.read(files.relations)
)
)
)

val setup =
val incOptions = IncOptions.create()
.withEnabled(true)
.withPipelining(true)
.withAuxiliaryClassFiles(Array(TastyFiles.instance()))
// .withStrictMode(true)
// .withRelationsDebug(true)
// .withApiDebug(true)
Expand Down Expand Up @@ -272,7 +270,7 @@ object ZincRunner extends WorkerMain[ZincRunner.Arguments]:
val analysis = compileResult.analysis.asInstanceOf[Analysis]

val usedDeps =
deps.filter(Dep.used(deps, analysis.relations, lookup)).filter(_.file != scalaInstance.libraryJar.toPath)
deps.filter(Deps.used(deps, analysis.relations, lookup)).filter(_.file != scalaInstance.libraryJar.toPath)
Files.write(workArgs.outputUsed, usedDeps.map(_.file.toString).sorted.asJava)

// create jar
Expand Down
2 changes: 0 additions & 2 deletions tests/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
common --config=tests

import %workspace%/../.bazelrc_shared
4 changes: 0 additions & 4 deletions tests/compile/zinc-inc/A.scala

This file was deleted.

5 changes: 5 additions & 0 deletions tests/compile/zinc-inc/A1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test

sealed trait A
object A:
case object Item extends A
6 changes: 6 additions & 0 deletions tests/compile/zinc-inc/A2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test

opaque type A = Int

object A:
val Item: A = 1
5 changes: 4 additions & 1 deletion tests/compile/zinc-inc/B.scala
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
final class B
package test

def test(): Unit =
println(A.Item)
18 changes: 17 additions & 1 deletion tests/compile/zinc-inc/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
load("@rules_scala3//rules:scala.bzl", "scala_library")


config_setting(
name = "a2",
values = {"define": "a2=true"},
visibility = ["//visibility:public"],
)

scala_library(
name = "a",
srcs = select({
":a2": ["A2.scala"],
"//conditions:default": ["A1.scala"],
}),
)

scala_library(
name = "lib",
srcs = glob(["*.scala"]),
srcs = ["B.scala"],
deps = [":a"],
)
4 changes: 0 additions & 4 deletions tests/compile/zinc-inc/C.scala

This file was deleted.

6 changes: 4 additions & 2 deletions tests/compile/zinc-inc/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env -S bash -e
. "$(dirname "$0")"/../../common.sh

# rm -fr "$(bazel info execution_root)/.bazel-zinc"
bazel build --noworker_sandboxing --worker_extra_flag=ScalaCompile=--persistence_dir=/tmp/.bazel-zinc --worker_extra_flag=ScalaCompile=--extracted_file_cache=/tmp/.bazel-zinc-outputs :lib
rm -rf /tmp/.bazel-zinc/zinc-inc /tmp/.bazel-zinc-outputs/zinc-inc

bazel build --noworker_sandboxing --worker_extra_flag=ScalaCompile=--persistence_dir=/tmp/.bazel-zinc/zinc-inc --worker_extra_flag=ScalaCompile=--extracted_file_cache=/tmp/.bazel-zinc-outputs/zinc-inc :lib
bazel build --sandbox_debug --define=a2=true --noworker_sandboxing --worker_extra_flag=ScalaCompile=--persistence_dir=/tmp/.bazel-zinc/zinc-inc --worker_extra_flag=ScalaCompile=--extracted_file_cache=/tmp/.bazel-zinc-outputs/zinc-inc :lib

0 comments on commit f22998b

Please sign in to comment.