Specifying “externs” using closurebuilder.py

August 13th, 2012 by exhuma.twn

The last hour or so I have been struggling with an “extern” definition using closurebuilder.py. When running the compiler manually, you specify externs like this:

  java -jar compiler.jar [...] --externs myexterns.js

So, naturally, my command looked like this:

  closurebuilder.py [...] --compiler_flags="--externs myexterns.js"

However, this resulted in the following error:

  "--externs myexterns.js" is not a valid option

My next step was to verify this by running the compiler manually. And it worked. So, without a doubt, it had something to do with closurebuilder.py. Suspecting a bug, I updated to the latest SVN revision. No luck. Still getting the same error.

So I dug into the code of closurebuilder, and the problem was obvious:

Each additional command-line flag --compiler_flags gets appended to a list. This list is then used as-is in the subprocess call. The subprocess API however expects each argument as a new item in the list. And technically --externs myexterns.js are two arguments. The first is --externs, the second is myexterns.js. The usual --compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" works because it is considered as one argument (it does not contain white-space).

So to get it working properly, you will have to modify the above line to the following:

  closurebuilder.py [...] --compiler_flags="--externs" --compiler_flags="myexterns.js"

Whether it makes sense to fix this inside closurebuilder.py is debatable. The arguments need to be passed as a list for security reasons (to be able to do proper shell escaping). Personally I don’t care that I have to specify my compiler flags in this wonky fashion. It’s in a makefile afterall…

Posted in Uncategorized | No Comments »

Pages

Recent Posts

Categories

Links


Archives

Meta