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

windows: write arguments to protoc to a file #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 40 additions & 6 deletions bridge/src/main/scala/protocbridge/ProtocRunner.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package protocbridge

import java.nio.file.Files
import scala.io.Source
import scala.sys.process.Process
import scala.sys.process.ProcessLogger
Expand Down Expand Up @@ -57,6 +58,37 @@ object ProtocRunner {
None
}

def maybeBoxArgsInFile[T](args: Seq[String])(withArgs: Seq[String] => T): T =
detectedOs match {
case "windows" =>
// The default command line length limit is 32767, which we might exceed.
// See also https://devblogs.microsoft.com/oldnewthing/20031210-00/?p=41553
// As of protobuf v3.5.0, you can pass arguments via a file instead with @<filename>.
// Arguments in the file are delimited by a newline and not escaped in any way.
val argumentFile = Files.createTempFile("scalapb-arguments-", ".txt")

try {
val writer = Files.newBufferedWriter(argumentFile)

try {
for (arg <- args) {
writer.write(arg)
writer.write("\n")
}
} finally {
writer.close()
}

val fileArgument = s"@${argumentFile.toString}"
withArgs(Seq(fileArgument))
} finally {
Files.delete(argumentFile)
}
case _ =>
// No special handling: just use the arguments as-is.
withArgs(args)
}

// This version of maybeNixDynamicLinker() finds ld-linux and also uses it
// to verify that the executable is dynamic. Newer version (>=3.23.0) of
// protoc are static, and thus do not load with ld-linux.
Expand All @@ -67,11 +99,13 @@ object ProtocRunner {

def apply(executable: String): ProtocRunner[Int] = ProtocRunner.fromFunction {
case (args, extraEnv) =>
Process(
command =
(maybeNixDynamicLinker(executable).toSeq :+ executable) ++ args,
cwd = None,
extraEnv: _*
).!
maybeBoxArgsInFile(args) { args =>
Process(
command =
(maybeNixDynamicLinker(executable).toSeq :+ executable) ++ args,
cwd = None,
extraEnv: _*
).!
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ object CoursierProtocCache {

val protoc = getProtoc(version).getAbsolutePath()

val cmd =
(ProtocRunner.maybeNixDynamicLinker(protoc).toSeq :+ protoc) ++ args
Process(command = cmd, cwd = None, extraEnv: _*).!
ProtocRunner.maybeBoxArgsInFile(args) { args =>
val cmd =
(ProtocRunner.maybeNixDynamicLinker(protoc).toSeq :+ protoc) ++ args
Process(command = cmd, cwd = None, extraEnv: _*).!
}
}

private[this] def download(tmpDir: File, dep: Dependency): Future[File] = {
Expand Down