Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use UTF8 as the encoding for Kotlin code, regardless of the system/default charset #437

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- Tests that everything works when using an unusual default Charset. -->
<argLine>-Dfile.encoding=UTF-16</argLine>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
19 changes: 10 additions & 9 deletions core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.ParseError
import com.google.googlejavaformat.FormattingError
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.io.PrintStream
import java.nio.charset.StandardCharsets.UTF_8
import java.util.concurrent.atomic.AtomicInteger
import kotlin.system.exitProcess

Expand Down Expand Up @@ -125,7 +129,8 @@ class Main(
private fun format(file: File?): Boolean {
val fileName = file?.toString() ?: parsedArgs.stdinName ?: "<stdin>"
try {
val code = file?.readText() ?: BufferedReader(InputStreamReader(input)).readText()
val bytes = if (file == null) input else FileInputStream(file)
val code = BufferedReader(InputStreamReader(bytes, UTF_8)).readText()
val formattedCode = Formatter.format(parsedArgs.formattingOptions, code)
val alreadyFormatted = code == formattedCode

Expand All @@ -136,7 +141,7 @@ class Main(
out.println(fileName)
}
} else {
out.print(formattedCode)
BufferedWriter(OutputStreamWriter(out, UTF_8)).use { it.write(formattedCode) }
}
return alreadyFormatted
}
Expand All @@ -148,7 +153,7 @@ class Main(
} else {
// TODO(T111284144): Add tests
if (!alreadyFormatted) {
file.writeText(formattedCode)
file.writeText(formattedCode, UTF_8)
}
err.println("Done formatting $fileName")
}
Expand All @@ -158,18 +163,14 @@ class Main(
err.println("Error formatting $fileName: ${e.message}; skipping.")
throw e
} catch (e: ParseError) {
handleParseError(fileName, e)
err.println("$fileName:${e.message}")
throw e
} catch (e: FormattingError) {
for (diagnostic in e.diagnostics()) {
System.err.println("$fileName:$diagnostic")
err.println("$fileName:$diagnostic")
hick209 marked this conversation as resolved.
Show resolved Hide resolved
}
e.printStackTrace(err)
throw e
}
}

private fun handleParseError(fileName: String, e: ParseError) {
err.println("$fileName:${e.message}")
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.FormattingOptions
import java.io.File
import java.io.PrintStream
import java.nio.charset.StandardCharsets.UTF_8

/** ParsedArgs holds the arguments passed to ktfmt on the command-line, after parsing. */
data class ParsedArgs(
Expand All @@ -40,7 +41,7 @@ data class ParsedArgs(

fun processArgs(err: PrintStream, args: Array<String>): ParsedArgs {
if (args.size == 1 && args[0].startsWith("@")) {
return parseOptions(err, File(args[0].substring(1)).readLines().toTypedArray())
return parseOptions(err, File(args[0].substring(1)).readLines(UTF_8).toTypedArray())
hick209 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return parseOptions(err, args)
}
Expand Down
Loading
Loading