From 8bbbbd707204f58f4e8b78caba63845a99f27192 Mon Sep 17 00:00:00 2001 From: KhemrajSingh Rathore Date: Wed, 6 Nov 2024 19:36:10 +0530 Subject: [PATCH] Node option fix (#136) * Revert "don't pass node-options explicitly" This reverts commit 537b9f3d77254a75efdf9239c33d7a0cb95dee54. * add a new flag in js --- .../src/main/scala/io/joern/jssrc2cpg/Main.scala | 12 +++++++++++- .../io/joern/jssrc2cpg/utils/AstGenRunner.scala | 11 ++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/Main.scala b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/Main.scala index d277dfe2fee0..e80774e85156 100644 --- a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/Main.scala +++ b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/Main.scala @@ -8,12 +8,18 @@ import scopt.OParser import java.nio.file.Paths -final case class Config(tsTypes: Boolean = true) extends X2CpgConfig[Config] with TypeRecoveryParserConfig[Config] { +final case class Config(tsTypes: Boolean = true, nodeOptions: String = "") + extends X2CpgConfig[Config] + with TypeRecoveryParserConfig[Config] { def withTsTypes(value: Boolean): Config = { copy(tsTypes = value).withInheritedFields(this) } + def withNodeOptions(value: String): Config = { + copy(nodeOptions = value).withInheritedFields(this) + } + } object Frontend { @@ -28,6 +34,10 @@ object Frontend { .hidden() .action((_, c) => c.withTsTypes(false)) .text("disable generation of types via Typescript"), + opt[String]("node-options") + .hidden() + .action((nodeOptions, c) => c.withNodeOptions(nodeOptions)) + .text("node options to be used with astGen"), XTypeRecovery.parserOptions ) } diff --git a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/utils/AstGenRunner.scala b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/utils/AstGenRunner.scala index 4972bbfdca34..b68ba34431a7 100644 --- a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/utils/AstGenRunner.scala +++ b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/utils/AstGenRunner.scala @@ -23,8 +23,6 @@ object AstGenRunner { private val LineLengthThreshold: Int = 10000 - // private val NODE_OPTIONS: Map[String, String] = Map("NODE_OPTIONS" -> "--max-old-space-size=8192") - private val TypeDefinitionFileExtensions = List(".t.ts", ".d.ts") private val MinifiedPathRegex: Regex = ".*([.-]min\\..*js|bundle\\.js)".r @@ -177,6 +175,9 @@ class AstGenRunner(config: Config) { import io.joern.jssrc2cpg.utils.AstGenRunner._ private val executableArgs = if (!config.tsTypes) " --no-tsTypes" else "" + private val nodeOptionsFromConfig = + if config.nodeOptions.nonEmpty then config.nodeOptions else "--max-old-space-size=8192" + private val NODE_OPTIONS: Map[String, String] = Map("NODE_OPTIONS" -> nodeOptionsFromConfig) private def skippedFiles(astGenOut: List[String]): List[String] = { val skipped = astGenOut.collect { @@ -300,7 +301,7 @@ class AstGenRunner(config: Config) { } val result = - ExternalCommand.run(s"$astGenCommand$executableArgs -t ts -o $out", out.toString()) + ExternalCommand.run(s"$astGenCommand$executableArgs -t ts -o $out", out.toString(), extraEnv = NODE_OPTIONS) val jsons = SourceFiles.determine(out.toString(), Set(".json")) jsons.foreach { jsonPath => @@ -327,12 +328,12 @@ class AstGenRunner(config: Config) { private def vueFiles(in: File, out: File): Try[Seq[String]] = { val files = SourceFiles.determine(in.pathAsString, Set(".vue")) if (files.nonEmpty) - ExternalCommand.run(s"$astGenCommand$executableArgs -t vue -o $out", in.toString()) + ExternalCommand.run(s"$astGenCommand$executableArgs -t vue -o $out", in.toString(), extraEnv = NODE_OPTIONS) else Success(Seq.empty) } private def jsFiles(in: File, out: File): Try[Seq[String]] = - ExternalCommand.run(s"$astGenCommand$executableArgs -t ts -o $out", in.toString()) + ExternalCommand.run(s"$astGenCommand$executableArgs -t ts -o $out", in.toString(), extraEnv = NODE_OPTIONS) private def runAstGenNative(in: File, out: File): Try[Seq[String]] = for { ejsResult <- ejsFiles(in, out)