Fix issue with module_root when building external repo

Closes #242

Closes #249

PiperOrigin-RevId: 232897910
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index fef6760..f587cc9 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -19,6 +19,9 @@
     build_targets:
     - "..."
     - "@disable_tsetse_for_external_test//..."
+    # Run some targets again, but addressed as an external repo
+    # TODO(alexeagle): run all of them after fixing https://github.com/bazelbuild/rules_typescript/issues/243
+    - "@build_bazel_rules_typescript//examples/some_library:lib"
     test_flags:
     # TODO(gregmagolan): shared libs needed by chrome & firefox not available on ubuntu1604
     - "--test_tag_filters=-browser:chromium-local,-browser:firefox-local"
diff --git a/internal/common/module_mappings.bzl b/internal/common/module_mappings.bzl
index a28b327..b74e5b8 100644
--- a/internal/common/module_mappings.bzl
+++ b/internal/common/module_mappings.bzl
@@ -90,9 +90,17 @@
 
             else:
                 for s in srcs:
-                    if not s.short_path.startswith(mr):
+                    short_path = s.short_path
+
+                    # Execroot paths for external repositories should start with external/
+                    # But the short_path property of file gives the relative path from our workspace
+                    # instead. We must correct this to compare with the module_root which is an
+                    # execroot path.
+                    if short_path.startswith("../"):
+                        short_path = "external/" + short_path[3:]
+                    if not short_path.startswith(mr):
                         fail(("all sources must be under module root: %s, but found: %s" %
-                              (mr, s.short_path)))
+                              (mr, short_path)))
         if mn in mappings and mappings[mn] != mr:
             fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
                   (label, mn, mappings[mn], mr)), "deps")