Windows fixes for examples tests
Update to use @bazel_tools//tools/bash/runfiles
Update to bazel 0.14.0 in CircleCI
Limit Bazel JVM memory usage in CircleCI

Closes #161

PiperOrigin-RevId: 203538677
diff --git a/.circleci/bazel.rc b/.circleci/bazel.rc
index c99021e..db2bc43 100644
--- a/.circleci/bazel.rc
+++ b/.circleci/bazel.rc
@@ -26,4 +26,6 @@
 # Workaround https://github.com/bazelbuild/bazel/issues/3645
 # Bazel doesn't calculate the memory ceiling correctly when running under Docker.
 # Limit Bazel to consuming 3072M of RAM
-build --local_resources=3072,2.0,1.0
\ No newline at end of file
+build --local_resources=3072,2.0,1.0
+# Also limit Bazel's own JVM heap to stay within our 4G container limit
+startup --host_jvm_args=-Xmx2G
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 701c4a1..767d2c6 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -9,8 +9,8 @@
 
 ## IMPORTANT
 # If you change the `docker_image` version, also change the `cache_key` suffix
-var_1: &docker_image angular/ngcontainer:0.3.0
-var_2: &cache_key rules_typescript-{{ checksum "yarn.lock" }}-0.3.0
+var_1: &docker_image angular/ngcontainer:0.3.1
+var_2: &cache_key rules_typescript-{{ checksum "yarn.lock" }}-0.3.1
 var_3: &setup-bazel-remote-cache
   run:
     name: Start up bazel remote cache proxy
diff --git a/WORKSPACE b/WORKSPACE
index 01388e1..1d9e13f 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -17,9 +17,9 @@
 
 http_archive(
     name = "build_bazel_rules_nodejs",
-    urls = ["https://github.com/bazelbuild/rules_nodejs/archive/0.9.1.zip"],
-    strip_prefix = "rules_nodejs-0.9.1",
-    sha256 = "6139762b62b37c1fd171d7f22aa39566cb7dc2916f0f801d505a9aaf118c117f",
+    urls = ["https://github.com/bazelbuild/rules_nodejs/archive/0.10.0.zip"],
+    strip_prefix = "rules_nodejs-0.10.0",
+    sha256 = "2f77623311da8b5009b1c7eade12de8e15fa3cd2adf9dfcc9f87cb2082b2211f",
 )
 
 load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")
diff --git a/examples/es5_output/BUILD.bazel b/examples/es5_output/BUILD.bazel
index cf45e89..c0aa28a 100644
--- a/examples/es5_output/BUILD.bazel
+++ b/examples/es5_output/BUILD.bazel
@@ -8,5 +8,8 @@
 sh_test(
     name = "es5_output_test",
     srcs = ["es5_output_test.sh"],
-    data = [":es5_output"],
+    data = [
+      ":es5_output",
+      "@bazel_tools//tools/bash/runfiles",
+    ],
 )
diff --git a/examples/es5_output/es5_output_test.sh b/examples/es5_output/es5_output_test.sh
index b7452ab..ab58ce5 100755
--- a/examples/es5_output/es5_output_test.sh
+++ b/examples/es5_output/es5_output_test.sh
@@ -1,16 +1,51 @@
 #!/bin/bash
 set -e
 
+# --- begin runfiles.bash initialization ---
+# Source the runfiles library:
+# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash
+# The runfiles library defines rlocation, which is a platform independent function
+# used to lookup the runfiles locations. This code snippet is needed at the top
+# of scripts that use rlocation to lookup the location of runfiles.bash and source it
+if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+    if [[ -f "$0.runfiles_manifest" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
+    elif [[ -f "$0.runfiles/MANIFEST" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
+    elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+      export RUNFILES_DIR="$0.runfiles"
+    fi
+fi
+if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+else
+  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+  exit 1
+fi
+# --- end runfiles.bash initialization ---
+
+readonly LIBRARY_JS=$(cat $(rlocation "build_bazel_rules_typescript/examples/some_library/library.js"))
+readonly BAR_JS=$(cat $(rlocation "build_bazel_rules_typescript/examples/bar.js"))
+readonly FOO_JS=$(cat $(rlocation "build_bazel_rules_typescript/examples/foo.js"))
+
 # should produce named UMD modules
-readonly LIBRARY_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/some_library/library.js)
 if [[ "$LIBRARY_JS" != *"define(\"some-lib\""* ]]; then
   echo "Expected library.js to declare module named some-lib, but was"
   echo "$LIBRARY_JS"
   exit 1
 fi
 
+# should produce named UMD modules
+if [[ "$BAR_JS" != *"define(\"build_bazel_rules_typescript/examples/bar\""* ]]; then
+  echo "Expected bar.js to declare named module, but was"
+  echo "$BAR_JS"
+  exit 1
+fi
+
 # should give a name to required modules
-readonly BAR_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/bar.js)
 if [[ "$BAR_JS" != *"require(\"build_bazel_rules_typescript/examples/foo\")"* ]]; then
   echo "Expected bar.js to require named module foo, but was"
   echo "$BAR_JS"
@@ -18,23 +53,29 @@
 fi
 
 # should give a name to required modules from other compilation unit
-readonly FOO_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/bar.js)
-if [[ "$FOO_JS" != *"require(\"some-lib\")"* ]]; then
+if [[ "$BAR_JS" != *"require(\"some-lib\")"* ]]; then
   echo "Expected bar.js to require named module library, but was"
-  echo "$FOO_JS"
+  echo "$BAR_JS"
   exit 1
 fi
 
 # should give a name to required generated modules without bazel-bin
-if [[ "$FOO_JS" != *"require(\"build_bazel_rules_typescript/examples/generated_ts/foo\")"* ]]; then
-  echo "Expected foo.js to require generated named module foo, but was"
-  echo "$FOO_JS"
+if [[ "$BAR_JS" != *"require(\"build_bazel_rules_typescript/examples/generated_ts/foo\")"* ]]; then
+  echo "Expected bar.js to require generated named module foo, but was"
+  echo "$BAR_JS"
   exit 1
 fi
 
 # should not give a module name to external modules
-if [[ "$FOO_JS" != *"require(\"typescript\")"* ]]; then
-  echo "Expected foo.js to require typescript by its original name, but was"
+if [[ "$BAR_JS" != *"require(\"typescript\")"* ]]; then
+  echo "Expected bar.js to require typescript by its original name, but was"
+  echo "$BAR_JS"
+  exit 1
+fi
+
+# should produce named UMD modules
+if [[ "$FOO_JS" != *"define(\"build_bazel_rules_typescript/examples/foo\""* ]]; then
+  echo "Expected foo.js to declare named module, but was"
   echo "$FOO_JS"
   exit 1
 fi
diff --git a/examples/es6_output/BUILD.bazel b/examples/es6_output/BUILD.bazel
index 4e52d1e..4bacb97 100644
--- a/examples/es6_output/BUILD.bazel
+++ b/examples/es6_output/BUILD.bazel
@@ -24,5 +24,8 @@
 sh_test(
     name = "es6_output_test",
     srcs = ["es6_output_test.sh"],
-    data = [":es6_output"],
+    data = [
+      ":es6_output",
+      "@bazel_tools//tools/bash/runfiles",
+    ],
 )
diff --git a/examples/es6_output/es6_output_test.sh b/examples/es6_output/es6_output_test.sh
index b251133..81ba1f1 100755
--- a/examples/es6_output/es6_output_test.sh
+++ b/examples/es6_output/es6_output_test.sh
@@ -1,8 +1,37 @@
 #!/bin/bash
 set -e
 
+# --- begin runfiles.bash initialization ---
+# Source the runfiles library:
+# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash
+# The runfiles library defines rlocation, which is a platform independent function
+# used to lookup the runfiles locations. This code snippet is needed at the top
+# of scripts that use rlocation to lookup the location of runfiles.bash and source it
+if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+    if [[ -f "$0.runfiles_manifest" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
+    elif [[ -f "$0.runfiles/MANIFEST" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
+    elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+      export RUNFILES_DIR="$0.runfiles"
+    fi
+fi
+if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+else
+  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+  exit 1
+fi
+# --- end runfiles.bash initialization ---
+
+readonly FOO_JS=$(cat $(rlocation "build_bazel_rules_typescript/examples/es6_output/es6_output.es6/examples/foo.js"))
+readonly BAR_JS=$(cat $(rlocation "build_bazel_rules_typescript/examples/es6_output/es6_output.es6/examples/bar.js"))
+readonly LIBRARY_JS=$(cat $(rlocation "build_bazel_rules_typescript/examples/es6_output/es6_output.es6/examples/some_library/library.js"))
+
 # should not down-level ES2015 syntax, eg. `class`
-readonly FOO_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/es6_output/es6_output.es6/examples/foo.js)
 if [[ "$FOO_JS" != *"class Greeter"* ]]; then
   echo "Expected foo.js to contain 'class Greeter' but was"
   echo "$FOO_JS"
@@ -10,7 +39,6 @@
 fi
 
 # should not down-level ES Modules
-readonly LIBRARY_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/es6_output/es6_output.es6/examples/some_library/library.js)
 if [[ "$LIBRARY_JS" != *"export const cool = 1;"* ]]; then
   echo "Expected library.js to contain 'export const cool = 1;' but was"
   echo "$LIBRARY_JS"
@@ -18,7 +46,6 @@
 fi
 
 # should not down-level dynamic import
-readonly BAR_JS=$(cat $TEST_SRCDIR/build_bazel_rules_typescript/examples/es6_output/es6_output.es6/examples/bar.js)
 if [[ "$BAR_JS" != *"import('./foo')"* ]]; then
   echo "Expected bar.js to contain 'import('./foo')' but was"
   echo "$BAR_JS"
diff --git a/examples/some_module/BUILD.bazel b/examples/some_module/BUILD.bazel
index 066f4fe..d913b96 100644
--- a/examples/some_module/BUILD.bazel
+++ b/examples/some_module/BUILD.bazel
@@ -45,5 +45,8 @@
 sh_test(
     name = "module_load_test",
     srcs = ["module_load_test.sh"],
-    data = [":bin"],
+    data = [
+      ":bin",
+      "@bazel_tools//tools/bash/runfiles",
+    ],
 )
diff --git a/examples/some_module/module_load_test.sh b/examples/some_module/module_load_test.sh
index a0658f2..68cd275 100755
--- a/examples/some_module/module_load_test.sh
+++ b/examples/some_module/module_load_test.sh
@@ -1,7 +1,34 @@
 #!/bin/bash
 set -e
 
-readonly OUT=$($TEST_SRCDIR/build_bazel_rules_typescript/examples/some_module/bin)
+# --- begin runfiles.bash initialization ---
+# Source the runfiles library:
+# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash
+# The runfiles library defines rlocation, which is a platform independent function
+# used to lookup the runfiles locations. This code snippet is needed at the top
+# of scripts that use rlocation to lookup the location of runfiles.bash and source it
+if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+    if [[ -f "$0.runfiles_manifest" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
+    elif [[ -f "$0.runfiles/MANIFEST" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
+    elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+      export RUNFILES_DIR="$0.runfiles"
+    fi
+fi
+if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+else
+  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+  exit 1
+fi
+# --- end runfiles.bash initialization ---
+
+readonly OUT=$($(rlocation "build_bazel_rules_typescript/examples/some_module/bin"))
+
 if [ "$OUT" != "hello world" ]; then
   echo "Expected output 'hello world' but was $OUT"
   exit 1
diff --git a/internal/ts_repositories.bzl b/internal/ts_repositories.bzl
index 2e81feb..d4248c9 100644
--- a/internal/ts_repositories.bzl
+++ b/internal/ts_repositories.bzl
@@ -14,7 +14,7 @@
 
 "Install toolchain dependencies"
 
-load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")
+load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version", "yarn_install")
 
 def ts_setup_workspace():
   """This repository rule should be called from your WORKSPACE file.
@@ -22,6 +22,9 @@
   It creates some additional Bazel external repositories that are used internally
   by the TypeScript rules.
   """
+  # @bazel_tools//tools/bash/runfiles is required
+  check_bazel_version("0.14.0")
+
   yarn_install(
       name = "build_bazel_rules_typescript_tsc_wrapped_deps",
       package_json = "@build_bazel_rules_typescript//internal:tsc_wrapped/package.json",