Check for .d.ts files with ambient module declarations in the sources of the rule, not just in the sources of the deps.
Also fixes an issue where the query-based analyzer failed when run outside of the workspace root. Fixed by prefixing the the root path to the file path when loading .d.ts files.
PiperOrigin-RevId: 233822781
diff --git a/ts_auto_deps/analyze/analyze.go b/ts_auto_deps/analyze/analyze.go
index e7f2851..d1ca2d5 100644
--- a/ts_auto_deps/analyze/analyze.go
+++ b/ts_auto_deps/analyze/analyze.go
@@ -328,7 +328,7 @@
continue handlingImports
}
}
- d, err := a.findExistingDepProvidingImport(ctx, target.dependencies, imp)
+ d, err := a.findExistingDepProvidingImport(ctx, root, target, imp)
if err != nil {
return err
}
@@ -398,13 +398,13 @@
//
// If the import already has a knownTarget, findRuleProvidingImport will
// return the knownTarget.
-func (a *Analyzer) findExistingDepProvidingImport(ctx context.Context, rules map[string]*appb.Rule, i *ts_auto_depsImport) (string, error) {
+func (a *Analyzer) findExistingDepProvidingImport(ctx context.Context, root string, rt *resolvedTarget, i *ts_auto_depsImport) (string, error) {
if i.knownTarget != "" {
return i.knownTarget, nil
}
// check if any of the existing deps declare a module_name that matches the import
- for _, r := range rules {
+ for _, r := range rt.dependencies {
moduleName := stringAttribute(r, "module_name")
if moduleName == "" {
continue
@@ -440,18 +440,23 @@
}
}
- // check if any of the existing deps have .d.ts sources which have ambient module
- // declarations
- for _, r := range rules {
+ // check if any of the other sources or the souces of any of the deps are .d.ts
+ // files which have ambient module declarations
+ var allRules []*appb.Rule
+ for _, r := range rt.dependencies {
+ allRules = append(allRules, r)
+ }
+ allRules = append(allRules, rt.rule)
+ for _, r := range allRules {
for _, src := range listAttribute(r, "srcs") {
- filepath := labelToPath(src)
- if !strings.HasSuffix(filepath, ".d.ts") {
+ fp := filepath.Join(root, labelToPath(src))
+ if !strings.HasSuffix(fp, ".d.ts") {
continue
}
- contents, err := platform.ReadFile(ctx, filepath)
+ contents, err := platform.ReadFile(ctx, fp)
if err != nil {
- return "", fmt.Errorf("error reading file lookinf for ambient module decls: %s", err)
+ return "", fmt.Errorf("error reading file looking for ambient module decls: %s", err)
}
matches := ambientModuleDeclRE.FindAllStringSubmatch(string(contents), -1)
@@ -464,7 +469,7 @@
// remove all the modules that were imported (ie all the modules that
// were being augmented/re-opened)
- for _, mi := range parseImports(filepath, contents) {
+ for _, mi := range parseImports(fp, contents) {
delete(declaredModules, mi.importPath)
}
diff --git a/ts_auto_deps/analyze/analyze_test.go b/ts_auto_deps/analyze/analyze_test.go
index 3e669ad..9e97220 100644
--- a/ts_auto_deps/analyze/analyze_test.go
+++ b/ts_auto_deps/analyze/analyze_test.go
@@ -156,14 +156,13 @@
}
for _, test := range tests {
r := analyzeTargets(t, []string{"a_lib"}, []*testTarget{
- {"a_lib", `
+ {"a_lib", fmt.Sprintf(`
rule_class: "ts_library"
attribute: <
name: "srcs"
- string_list_value: "//a:b/importer.ts"
- string_list_value: "//a:importer.d.ts"
+ string_list_value: "//a:%s"
type: 5
- >`, nil},
+ >`, test.filepath), nil},
}, []*file{{test.filepath, []string{test.fileContents}}})
if r == nil {
continue