Search in @types directory for modules too.

Add an example that uses Bazel-managed deps to expose the bug where TypeScript would not find ambient definitions installed into Bazel's external directory.

Fixes #179
Closes #180

PiperOrigin-RevId: 194441277
diff --git a/WORKSPACE b/WORKSPACE
index 921a976..23884f2 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -21,7 +21,14 @@
     sha256 = "17a5515f59777b00cb25dbc710017a14273f825029b2ec60e0969d28914870be",
 )
 
-load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")
+load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")
+
+# Use a bazel-managed npm dependency, allowing us to test resolution to these paths
+yarn_install(
+    name = "build_bazel_rules_typescript_internal_bazel_managed_deps",
+    package_json = "//examples/bazel_managed_deps:package.json",
+    yarn_lock = "//examples/bazel_managed_deps:yarn.lock",
+)
 
 # Install a hermetic version of node.
 # After this is run, these labels will be available:
diff --git a/examples/bazel_managed_deps/BUILD.bazel b/examples/bazel_managed_deps/BUILD.bazel
new file mode 100644
index 0000000..d3084b4
--- /dev/null
+++ b/examples/bazel_managed_deps/BUILD.bazel
@@ -0,0 +1,8 @@
+load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
+
+ts_library(
+    name = "bazel_managed_deps",
+    srcs = ["index.ts"],
+    tsconfig = ":tsconfig.json",
+    node_modules = "@build_bazel_rules_typescript_internal_bazel_managed_deps//:node_modules",
+)
diff --git a/examples/bazel_managed_deps/README.md b/examples/bazel_managed_deps/README.md
new file mode 100644
index 0000000..c0cff30
--- /dev/null
+++ b/examples/bazel_managed_deps/README.md
@@ -0,0 +1,12 @@
+# Bazel-managed deps
+
+The NodeJS rules allow you to skip the install step, and have Bazel run yarn/npm for you.
+
+See the /WORKSPACE file where we declare a workspace called
+build_bazel_rules_typescript_internal_bazel_managed_deps
+that will be installed automatically by Bazel.
+
+We then can build the code in this directory without installing the package.json located here.
+
+Regression test for
+https://github.com/bazelbuild/rules_typescript/issues/179
diff --git a/examples/bazel_managed_deps/index.ts b/examples/bazel_managed_deps/index.ts
new file mode 100644
index 0000000..5b3b16a
--- /dev/null
+++ b/examples/bazel_managed_deps/index.ts
@@ -0,0 +1 @@
+import * as t from 'three';
diff --git a/examples/bazel_managed_deps/package.json b/examples/bazel_managed_deps/package.json
new file mode 100644
index 0000000..4a39fc5
--- /dev/null
+++ b/examples/bazel_managed_deps/package.json
@@ -0,0 +1,8 @@
+{
+  "name": "bazel_managed_deps",
+  "devDependencies": {
+    "@types/three": "^0.91.10",
+    "three": "^0.92.0",
+    "typescript": "^2.8.3"
+  }
+}
diff --git a/examples/bazel_managed_deps/tsconfig.json b/examples/bazel_managed_deps/tsconfig.json
new file mode 100644
index 0000000..aee0ec9
--- /dev/null
+++ b/examples/bazel_managed_deps/tsconfig.json
@@ -0,0 +1,5 @@
+{
+  "compilerOptions": {
+    "strict": true
+  }
+}
diff --git a/examples/bazel_managed_deps/yarn.lock b/examples/bazel_managed_deps/yarn.lock
new file mode 100644
index 0000000..4ba79ac
--- /dev/null
+++ b/examples/bazel_managed_deps/yarn.lock
@@ -0,0 +1,21 @@
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
+# yarn lockfile v1
+
+
+"@types/three@^0.91.10":
+  version "0.91.10"
+  resolved "https://registry.yarnpkg.com/@types/three/-/three-0.91.10.tgz#996b82993902d564daa1c29d0c3d653f2b15243a"
+  dependencies:
+    "@types/webvr-api" "*"
+
+"@types/webvr-api@*":
+  version "0.0.34"
+  resolved "https://registry.yarnpkg.com/@types/webvr-api/-/webvr-api-0.0.34.tgz#8fa49028de925c7b8bce3d559d3374ce2c89ee28"
+
+three@^0.92.0:
+  version "0.92.0"
+  resolved "https://registry.yarnpkg.com/three/-/three-0.92.0.tgz#8d3d1f5af890e62da7f4cb45d20c09fa51057dcd"
+
+typescript@^2.8.3:
+  version "2.8.3"
+  resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170"
diff --git a/internal/common/tsconfig.bzl b/internal/common/tsconfig.bzl
index c45bd75..a0b4476 100644
--- a/internal/common/tsconfig.bzl
+++ b/internal/common/tsconfig.bzl
@@ -75,6 +75,17 @@
           ctx.attr.node_modules.label.package,
           "node_modules",
           "*"] if p]))
+      # TypeScript needs to look up ambient types from a 'node_modules'
+      # directory, but when Bazel manages the dependencies, this directory
+      # isn't in the project so TypeScript won't find it.
+      # We can add it to the path mapping to make this lookup work.
+      # See https://github.com/bazelbuild/rules_typescript/issues/179
+      node_modules_mappings.append("/".join([p for p in [
+          ctx.attr.node_modules.label.workspace_root,
+          ctx.attr.node_modules.label.package,
+          "node_modules",
+          "@types",
+          "*"] if p]))
 
     module_roots = {
         "*": node_modules_mappings,