ts_auto_deps: Fix load location for TypeScript and Angular rules.

Add a simple e2e test for ts_auto_deps.

PiperOrigin-RevId: 210399767
diff --git a/internal/e2e/ts_auto_deps/simple/file.ts b/internal/e2e/ts_auto_deps/simple/file.ts
new file mode 100644
index 0000000..bb2056c
--- /dev/null
+++ b/internal/e2e/ts_auto_deps/simple/file.ts
@@ -0,0 +1,2 @@
+// Test that a BUILD file is generated in this directory
+export const a = 1;
\ No newline at end of file
diff --git a/package.json b/package.json
index 518d664..ed23f3e 100644
--- a/package.json
+++ b/package.json
@@ -32,12 +32,13 @@
     },
     "scripts": {
         "pree2e": "webdriver-manager update $CHROMEDRIVER_VERSION_ARG && bazel build examples/app:e2e && bazel build examples/protocol_buffers:e2e",
-        "e2e": "yarn e2e-bazel-external && yarn e2e-examples-app-devserver && yarn e2e-examples-app-prodserver && yarn e2e-examples-protobuf-devserver && yarn e2e-examples-protobuf-prodserver",
+        "e2e": "yarn e2e-bazel-external && yarn e2e-examples-app-devserver && yarn e2e-examples-app-prodserver && yarn e2e-examples-protobuf-devserver && yarn e2e-examples-protobuf-prodserver && yarn e2e-ts_auto_deps",
         "e2e-bazel-external": "jasmine internal/e2e/default_tsconfig_test.js",
         "e2e-examples-app-devserver": "concurrently \"bazel run examples/app:devserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite app\" --kill-others --success first",
         "e2e-examples-app-prodserver": "concurrently \"bazel run examples/app:prodserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite app\" --kill-others --success first",
         "e2e-examples-protobuf-devserver": "concurrently \"bazel run examples/protocol_buffers:devserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite protocol_buffers\" --kill-others --success first",
         "e2e-examples-protobuf-prodserver": "concurrently \"bazel run examples/protocol_buffers:prodserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite protocol_buffers\" --kill-others --success first",
+        "e2e-ts_auto_deps": "cd internal/e2e/ts_auto_deps; bazel run //ts_auto_deps -- -recursive && bazel build simple",
         "preskylint": "bazel build --noshow_progress @io_bazel//src/tools/skylark/java/com/google/devtools/skylark/skylint:Skylint",
         "skylint": "find . -type f -name \"*.bzl\" ! -path \"*/node_modules/*\" | xargs $(bazel info bazel-bin)/external/io_bazel/src/tools/skylark/java/com/google/devtools/skylark/skylint/Skylint",
         "skydoc": "bazel build //docs && unzip -o -d docs/api bazel-bin/docs/docs-skydoc.zip",
diff --git a/ts_auto_deps/main.go b/ts_auto_deps/main.go
index 553afbc..81542dc 100644
--- a/ts_auto_deps/main.go
+++ b/ts_auto_deps/main.go
@@ -39,6 +39,16 @@
 }
 
 func main() {
+	// When executed under `bazel run`, we want to run in the users workspace, not
+	// the runfiles directory of the go_binary.
+	// See https://github.com/bazelbuild/bazel/issues/3325
+	if wd := os.Getenv("BUILD_WORKING_DIRECTORY"); len(wd) > 0 {
+		err := os.Chdir(wd)
+		if err != nil {
+			platform.Error(err)
+		}
+	}
+
 	flag.Usage = usage
 	flag.Parse()
 
diff --git a/ts_auto_deps/updater/updater.go b/ts_auto_deps/updater/updater.go
index 2fa08ad..cf2115e 100644
--- a/ts_auto_deps/updater/updater.go
+++ b/ts_auto_deps/updater/updater.go
@@ -675,6 +675,11 @@
 	}
 }
 
+const (
+	tsSkylarkLabel = "@build_bazel_rules_typescript//:defs.bzl"
+	ngSkylarkLabel = "@angular//:index.bzl"
+)
+
 func removeUnusedLoad(bld *build.File, kind string) {
 	if len(bld.Rules(kind)) > 0 {
 		return // kind is still used somewhere.
@@ -733,9 +738,9 @@
 		}
 	}
 	if changed {
-		bld.Stmt = edit.InsertLoad(bld.Stmt, "//javascript/angular2:build_defs.bzl",
+		bld.Stmt = edit.InsertLoad(bld.Stmt, ngSkylarkLabel,
 			[]string{"ng_module"}, []string{"ng_module"})
-		bld.Stmt = edit.InsertLoad(bld.Stmt, "//javascript/typescript:build_defs.bzl",
+		bld.Stmt = edit.InsertLoad(bld.Stmt, tsSkylarkLabel,
 			[]string{"ts_library"}, []string{"ts_library"})
 		removeUnusedLoad(bld, "ts_library")
 		removeUnusedLoad(bld, "ng_module")
@@ -838,7 +843,7 @@
 	}
 
 	loadArgs := []string{ruleKind}
-	bld.Stmt = edit.InsertLoad(bld.Stmt, "//javascript/typescript:build_defs.bzl", loadArgs, loadArgs)
+	bld.Stmt = edit.InsertLoad(bld.Stmt, tsSkylarkLabel, loadArgs, loadArgs)
 
 	r := &build.Rule{&build.CallExpr{X: &build.Ident{Name: ruleKind}}, ""}
 	// Rename to *_ts if there's a name collision. This leaves open a collision with another rule