diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/FileUtils.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/FileUtils.kt index a371331fbb..a480371f74 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/FileUtils.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/FileUtils.kt @@ -137,8 +137,13 @@ internal fun FileSystem.fileSequence( dirAttr: BasicFileAttributes, ): FileVisitResult = if (Files.isHidden(dirPath)) { - LOGGER.trace { "- Dir: $dirPath: Ignore" } - FileVisitResult.SKIP_SUBTREE + if (dirPath == commonRootDir) { + LOGGER.trace { "- Dir: $dirPath: Traverse started from hidden directory" } + FileVisitResult.CONTINUE + } else { + LOGGER.trace { "- Dir: $dirPath: Ignore traversal of hidden directory" } + FileVisitResult.SKIP_SUBTREE + } } else { LOGGER.trace { "- Dir: $dirPath: Traverse" } FileVisitResult.CONTINUE diff --git a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/internal/FileUtilsTest.kt b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/internal/FileUtilsTest.kt index bb108da1d9..3f9c993ad8 100644 --- a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/internal/FileUtilsTest.kt +++ b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/internal/FileUtilsTest.kt @@ -26,9 +26,9 @@ internal class FileUtilsTest { private val javaFileRootDirectory = "Root.java" private val ktFileRootDirectory = "Root.kt" private val ktsFileRootDirectory = "Root.kts" - private val javaFileInHiddenDirectory = "project1/.git/Ignored.java" - private val ktFileInHiddenDirectory = "project1/.git/Ignored.kt" - private val ktsFileInHiddenDirectory = "project1/.git/Ignored.kts" + private val javaFileInHiddenDirectory = "project1/.hidden/Ignored.java" + private val ktFileInHiddenDirectory = "project1/.hidden/Ignored.kt" + private val ktsFileInHiddenDirectory = "project1/.hidden/Ignored.kts" private val javaFileInProjectRootDirectory = "project1/ProjectRoot.java" private val ktFileInProjectRootDirectory = "project1/ProjectRoot.kt" private val ktsFileInProjectRootDirectory = "project1/ProjectRoot.kts" @@ -106,6 +106,17 @@ internal class FileUtilsTest { ) } + @Test + fun `Given the root directory where scanning starts is hidden, then the patterns should work`() { + val foundFiles = + getFiles( + patterns = listOf("*.kt"), + rootDir = ktlintTestFileSystem.resolve("project1/.hidden"), + ) + + assertThat(foundFiles).containsExactlyInAnyOrder(ktFileInHiddenDirectory) + } + @Test fun `Given some patterns including a negate pattern and no workdir then select all files except files in the negate pattern`() { val foundFiles =