Patch IntelliJ Bazel plugin to recognize our custom generators

We can get rid of the fuzzy $ASPECT_PATH matching now that Hendrik
has deployed rW, and can remove the bind mount.

Test Plan:
Recreated container, ran tests and full sync. Generated SQL code
was properly recognized.

X-Origin-Diff: phab/D265
GitOrigin-RevId: 81de00d54402107ba217ab28b8812ace772777ac
diff --git a/scripts/bin/bazel b/scripts/bin/bazel
index 0773669..b63e060 100755
--- a/scripts/bin/bazel
+++ b/scripts/bin/bazel
@@ -1,4 +1,11 @@
 #!/bin/bash
 set -euo pipefail
 
-exec podman exec -it nexantic-dev bazel --output_user_root ${HOME}/.cache/bazel-nxt $@
+# Override the intellij_aspect repository to point to our own copy that create_container.sh has applied patches to.
+#
+# The IntelliJ plugin sets the repository using a command line argument, which we need to replace:
+#   --override_repository=intellij_aspect=<...>/plugins/ijwb/aspect
+#
+ARGS=$(echo $@ | sed "s%--override_repository=intellij_aspect=[^ ]*%--override_repository=intellij_aspect=${HOME}/.cache/bazel-nxt/ijwb_aspect%")
+
+exec podman exec -it nexantic-dev bazel --output_user_root ${HOME}/.cache/bazel-nxt $ARGS
diff --git a/scripts/create_container.sh b/scripts/create_container.sh
index a2a5d61..5f179cd 100755
--- a/scripts/create_container.sh
+++ b/scripts/create_container.sh
@@ -33,22 +33,22 @@
 # Mount bazel root to identical paths inside and outside the container.
 # This caches build state even if the container is destroyed, and
 BAZEL_ROOT=${HOME}/.cache/bazel-nxt
-
-# The Bazel plugin injects a Bazel repository into the sync command line.
-# Look up the latest copy of it in either the user config folder
-# or the Jetbrains Toolbox dir (for non-standard setups).
-ASPECT_PATH=$(
-  dirname $(
-    find ~/.IntelliJIdea*/config/plugins/ijwb/* ~/.local/share/JetBrains \
-    -name intellij_info_impl.bzl -printf "%T+\t%p\n" | sort | tail -n 1))
-
 mkdir -p ${BAZEL_ROOT}
 
+# The Bazel plugin injects a Bazel repository into the sync command line,
+# We need to copy the aspect repository and apply a custom patch.
+
+ASPECT_ORIG=${HOME}/.IntelliJIdea2019.2/config/plugins/ijwb/aspect
+
+ASPECT_PATH=${BAZEL_ROOT}/ijwb_aspect
+rm -rf "$ASPECT_PATH"
+cp -r "$ASPECT_ORIG" "$ASPECT_PATH"
+patch -d "$ASPECT_PATH" -p1 < scripts/patches/bazel_intellij_aspect_filter.patch
+
 podman run -it -d \
     -v $(pwd):$(pwd) \
     -w $(pwd) \
     --volume=${BAZEL_ROOT}:${BAZEL_ROOT} \
-    -v ${ASPECT_PATH}:${ASPECT_PATH}:ro \
     --device /dev/kvm \
     --privileged \
     --pod nexantic \
diff --git a/scripts/patches/bazel_intellij_aspect_filter.patch b/scripts/patches/bazel_intellij_aspect_filter.patch
new file mode 100644
index 0000000..a9056f1
--- /dev/null
+++ b/scripts/patches/bazel_intellij_aspect_filter.patch
@@ -0,0 +1,60 @@
+Copyright 2020 The Monogon Project Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+
+# We need to patch the IntelliJ Bazel aspect that is injected via the @intellij_bazel repository
+# to support our custom Go code generators.
+#
+# The plugin maintains a whitelist of rules it deems worthy of syncing.
+#
+# Since we really, *really* do not want to touch the Java code and fork the plugin, we instead monkey-patch
+# the intellij_aspect macro to pretend that our custom generators are, in fact, go_proto_library rules
+# (which behave exactly the same - they output Go sources).
+#
+--- a/intellij_info_impl.bzl.o
++++ b/intellij_info_impl.bzl
+@@ -312,7 +312,7 @@
+         files = target[OutputGroupInfo].go_generated_srcs.to_list()
+     else:
+         return None
+-    return [f for f in files if f.basename.endswith(".pb.go")]
++    return [f for f in files if f.basename.endswith(".go")]
+
+ def collect_go_info(target, ctx, semantics, ide_info, ide_info_file, output_groups):
+     """Updates Go-specific output groups, returns false if not a recognized Go target."""
+@@ -897,10 +897,14 @@
+     file_name = file_name + ".intellij-info.txt"
+     ide_info_file = ctx.actions.declare_file(file_name)
+
++    kind = ctx.rule.kind
++    if kind in ["go_sqlboiler_library", "bindata"]:
++        kind = "go_proto_library"
++
+     target_key = make_target_key(target.label, aspect_ids)
+     ide_info = dict(
+         key = target_key,
+-        kind_string = ctx.rule.kind,
++        kind_string = kind,
+         deps = list(all_deps),
+         build_file_artifact_location = build_file_artifact_location(ctx),
+         tags = tags,
+@@ -937,7 +942,7 @@
+         output_groups = output_groups,
+         intellij_info = struct(
+             target_key = target_key,
+-            kind = ctx.rule.kind,
++            kind = kind,
+             output_groups = output_groups,
+             export_deps = export_deps,
+         ),