Improve handling of import paths versus file paths.

There isn't a one to one mapping of ts import paths to file paths.  A import path of "google3/foo/bar" could be from "foo/bar.ts" or "foo/bar.d.ts" or foo/bar/index.ts" or several others.  The query-based analyzer has to issue a query looking for all the possible files for an import, then match the files found to the import path.

This cl improves the matching from found files back to import paths by consistently using a single function to generate all the possibilities, and matching against all of them, instead of trying to normalize a file path back into an import path.  Fixes a bug where the query based analyzer couldn't find an import that was both named index and an ngsummary.

PiperOrigin-RevId: 233083622
diff --git a/ts_auto_deps/analyze/loader.go b/ts_auto_deps/analyze/loader.go
index ab82fce..4b509ca 100644
--- a/ts_auto_deps/analyze/loader.go
+++ b/ts_auto_deps/analyze/loader.go
@@ -132,6 +132,25 @@
 	return currentPkg + "^" + label
 }
 
+// possibleFilepaths generates the possible filepaths for the ts import path.
+// e.g. google3/foo/bar could be foo/bar.ts or foo/bar.d.ts or foo/bar/index.ts, etc.
+// Also handles special angular import paths (.ngfactory and .ngsummary).
+func possibleFilepaths(importPath string) []string {
+	// If the path has a suffix of ".ngfactory" or ".ngsummary", it might
+	// be an Angular AOT generated file. We can infer the target as we
+	// infer its corresponding ngmodule target by simply stripping the
+	// ".ngfactory" / ".ngsummary" suffix
+	importPath = strings.TrimSuffix(strings.TrimSuffix(importPath, ".ngsummary"), ".ngfactory")
+	importPath = strings.TrimPrefix(importPath, workspace.Name()+"/")
+
+	var possiblePaths []string
+
+	possiblePaths = append(possiblePaths, pathWithExtensions(importPath)...)
+	possiblePaths = append(possiblePaths, pathWithExtensions(filepath.Join(importPath, "index"))...)
+
+	return possiblePaths
+}
+
 // LoadImportPaths uses Bazel Query to load targets associated with import
 // paths from BUILD files.
 func (q *QueryBasedTargetLoader) LoadImportPaths(ctx context.Context, currentPkg, workspaceRoot string, paths []string) (map[string]*appb.Rule, error) {
@@ -150,15 +169,9 @@
 			if _, ok := addedPaths[path]; !ok {
 				addedPaths[path] = true
 
-				// If the path has a suffix of ".ngfactory" or ".ngsummary", it might
-				// be an Angular AOT generated file. We can infer the target as we
-				// infer its corresponding ngmodule target by simply stripping the
-				// ".ngfactory" / ".ngsummary" suffix
-				path = strings.TrimSuffix(strings.TrimSuffix(path, ".ngsummary"), ".ngfactory")
-				path = strings.TrimPrefix(path, workspace.Name()+"/")
-
-				possiblePaths = append(possiblePaths, pathWithExtensions(path)...)
-				possiblePaths = append(possiblePaths, pathWithExtensions(filepath.Join(path, "index"))...)
+				// there isn't a one to one mapping from ts import paths to file
+				// paths, so look for all the possible file paths
+				possiblePaths = append(possiblePaths, possibleFilepaths(path)...)
 			}
 		}
 	}
@@ -190,7 +203,7 @@
 		}
 	}
 
-	labelToRule := make(map[string]*appb.Rule)
+	filepathToRule := make(map[string]*appb.Rule)
 
 	// load all the rules with file srcs (either literal or generated)
 	sourceLabelToRule, err := q.loadRulesWithSources(workspaceRoot, fileLabels)
@@ -198,7 +211,7 @@
 		return nil, err
 	}
 	for label, rule := range sourceLabelToRule {
-		labelToRule[label] = rule
+		filepathToRule[labelToPath(label)] = rule
 	}
 
 	// load all the rules with generator rule srcs
@@ -208,20 +221,14 @@
 	}
 	for label, rule := range generatorLabelToRule {
 		for _, generated := range generatorsToFiles[label] {
-			labelToRule[generated.GetName()] = rule
+			filepathToRule[labelToPath(generated.GetName())] = rule
 		}
 	}
 
-	for label, rule := range labelToRule {
-		_, pkg, file := edit.ParseLabel(label)
-		// Trim "/index" suffixes that were added to path in the queries above.
-		pathWithoutExtension := strings.TrimSuffix(filepath.Join(pkg, stripTSExtension(file)), string(filepath.Separator)+"index")
-		for _, path := range paths {
-			if pathWithoutExtension == strings.TrimSuffix(path, string(filepath.Separator)+"index") {
-				results[path] = rule
-			} else if pathWithoutExtension == strings.TrimSuffix(path, ".ngsummary") {
-				results[path] = rule
-			} else if pathWithoutExtension == strings.TrimSuffix(path, ".ngfactory") {
+	for _, path := range paths {
+		// check all the possible file paths for the import path
+		for _, fp := range possibleFilepaths(path) {
+			if rule, ok := filepathToRule[fp]; ok {
 				results[path] = rule
 			}
 		}