Skip to content

Commit 9e4671e

Browse files
authored
Merge pull request scalacenter#1776 from oyvindberg/truncate-file-timestamp
Truncate file timestamp in `AttributedPath` to milliseconds
2 parents a0ca82d + e443755 commit 9e4671e

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

frontend/src/main/scala/bloop/data/Origin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import bloop.util.CacheHashCode
88

99
case class Origin(path: AbsolutePath, lastModifiedtime: FileTime, size: Long, hash: Int)
1010
extends CacheHashCode {
11-
def toAttributedPath: AttributedPath = AttributedPath(path, lastModifiedtime, size)
11+
def toAttributedPath: AttributedPath = AttributedPath.of(path, lastModifiedtime, size)
1212
}
1313

1414
object Origin {

frontend/src/test/scala/bloop/testing/BaseSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
142142
val osInsensitivePath = ap.path.syntax.replace(prefixPath, "").replace(File.separator, "/")
143143
val maskedRelativePath = AbsolutePath(osInsensitivePath)
144144
if (!maskedRelativePath.syntax.startsWith("/classes-")) {
145-
ap.copy(path = maskedRelativePath)
145+
ap.withPath(maskedRelativePath)
146146
} else {
147147
// Remove '/classes-*' from path
148148
val newPath = maskedRelativePath.syntax.split(File.separatorChar).tail.tail.mkString("/")
149-
ap.copy(path = AbsolutePath("/" + newPath))
149+
ap.withPath(AbsolutePath("/" + newPath))
150150
}
151151
}
152152

shared/src/main/scala/bloop/io/Paths.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import scala.collection.mutable
1919

2020
import dev.dirs.ProjectDirectories
2121

22+
import java.time.temporal.ChronoUnit
23+
2224
object Paths {
2325
private val projectDirectories = ProjectDirectories.from("", "", "bloop")
2426
private def createDirFor(filepath: String): AbsolutePath =
@@ -80,7 +82,24 @@ object Paths {
8082
out.toList
8183
}
8284

83-
case class AttributedPath(path: AbsolutePath, lastModifiedTime: FileTime, size: Long)
85+
// sealed abstract is an abomination for scala 2.12 to get private which actually works
86+
sealed abstract case class AttributedPath(
87+
path: AbsolutePath,
88+
lastModifiedTime: FileTime,
89+
size: Long
90+
) {
91+
def withPath(newPath: AbsolutePath): AttributedPath =
92+
new AttributedPath(newPath, lastModifiedTime, size) {}
93+
}
94+
95+
object AttributedPath {
96+
def of(path: AbsolutePath, lastModifiedTime: FileTime, size: Long): AttributedPath = {
97+
// this logic exists to maintain consistency between old and new JVMs. Older JVMs does not provide more than millisecond precision for `Instant`s by default
98+
val truncatedFileTime =
99+
FileTime.from(lastModifiedTime.toInstant.truncatedTo(ChronoUnit.MILLIS))
100+
new AttributedPath(path, truncatedFileTime, size) {}
101+
}
102+
}
84103

85104
/**
86105
* Get all files under `base` that match the pattern `pattern` up to depth `maxDepth`.
@@ -108,7 +127,7 @@ object Paths {
108127
val visitor = new FileVisitor[Path] {
109128
def visitFile(file: Path, attributes: BasicFileAttributes): FileVisitResult = {
110129
if (matcher.matches(file)) {
111-
out += AttributedPath(
130+
out += AttributedPath.of(
112131
AbsolutePath(file),
113132
attributes.lastModifiedTime(),
114133
attributes.size()

0 commit comments

Comments
 (0)