Repository: pantsbuild/pants

Issue:
quoting of nested options does not work
I'd need to do a 100% static build of a go binary (that is even statically like libc).
```
./pants bundle src/:: --compile-go-build-flags='--ldflags "-extldflags -static"'

17:09:20 00:03       [install 3rdparty/go/k8s.io/apimachinery:vendor/gopkg.in/yaml.v2]
                     invalid value "\"-extldflags" for flag -ldflags: unterminated " string
                     usage: install [build flags] [packages]

./pants bundle src/:: --compile-go-build-flags="--ldflags \"-extldflags \\\"-static\\\"\""

17:11:40 00:03       [install 3rdparty/go/k8s.io/apimachinery:vendor/gopkg.in/yaml.v2]
                     invalid value "\"-extldflags" for flag -ldflags: unterminated " string
                     usage: install [build flags] [packages]
```
```
 ./pants --version
1.3.0.dev16
```


Discussion:
I think this is specific to go compile's build flag handling. It should use `safe_shlex_split` from pants.util.strutil instead of string's split

https://github.com/pantsbuild/pants/blob/master/contrib/go/src/python/pants/contrib/go/tasks/go_compile.py#L61
Hm, rather than lexing, `--compile-go-build-flags` should probably be of type `list`, as that would avoid the need to split them.
@ensonic : If you're interested in making this change, it should be a straightforward one. Would need to add another option with type `list` (with a similar name to the existing option) and deprecate the previous one.

There is an example here, where the `--ignore-patterns` option has been replaced by the `--build-ignore` option: https://github.com/pantsbuild/pants/blob/62997d02f53c112cd215dadef9740b32b2964b61/src/python/pants/option/global_options.py#L171-L174
@stuhood  Not sure how the type=list would help. How do you envision it to be used then? Like this:

    ./pants compile src/:: --compile-go-build-flags="--ldflags" --compile-go-build-flags "-extldflags \"-static\""

Also trying @baroquebobcat suggestion, but need to figure how to run the integrations tests - I am running ./build-support/bin/ci.sh but it fails repeatedly (https://gist.github.com/ensonic/15a416e16b355e1bfe71bf8c169b4218) 

Resolving patch:
diff --git a/contrib/go/src/python/pants/contrib/go/tasks/go_compile.py b/contrib/go/src/python/pants/contrib/go/tasks/go_compile.py
--- a/contrib/go/src/python/pants/contrib/go/tasks/go_compile.py
+++ b/contrib/go/src/python/pants/contrib/go/tasks/go_compile.py
@@ -6,10 +6,12 @@
                         unicode_literals, with_statement)
 
 import os
+import re
 
 from pants.base.exceptions import TaskError
 from pants.base.workunit import WorkUnitLabel
 from pants.util.dirutil import safe_mkdir
+from pants.util.strutil import safe_shlex_split
 
 from pants.contrib.go.targets.go_target import GoTarget
 from pants.contrib.go.tasks.go_workspace_task import GoWorkspaceTask
@@ -58,7 +60,8 @@ def execute(self):
                                                    vt.target.import_path + '.a')
 
   def _go_install(self, target, gopath):
-    args = self.get_options().build_flags.split() + [target.import_path]
+    build_flags = re.sub(r'^"|"$', '', self.get_options().build_flags)
+    args = safe_shlex_split(build_flags) + [target.import_path]
     result, go_cmd = self.go_dist.execute_go_cmd(
       'install', gopath=gopath, args=args,
       workunit_factory=self.context.new_workunit,


Test patch:
diff --git a/contrib/go/tests/python/pants_test/contrib/go/tasks/test_go_compile_integration.py b/contrib/go/tests/python/pants_test/contrib/go/tasks/test_go_compile_integration.py
--- a/contrib/go/tests/python/pants_test/contrib/go/tasks/test_go_compile_integration.py
+++ b/contrib/go/tests/python/pants_test/contrib/go/tasks/test_go_compile_integration.py
@@ -42,3 +42,8 @@ def test_go_compile_with_remote_deps(self):
     args = ['compile', 'contrib/go/examples/src/go/server']
     pants_run = self.run_pants(args)
     self.assert_success(pants_run)
+
+  def test_go_compile_fully_static(self):
+    args = ['compile', 'contrib/go/examples/src/go/server', '--compile-go-build-flags="--ldflags \'-extldflags \"-static\"\'"']
+    pants_run = self.run_pants(args)
+    self.assert_success(pants_run)