ISSUE DESCRIPTION service member referenced in an Angular external template is reported…
- User
ISSUE DESCRIPTION
service member referenced in an Angular external template is reported as unused when the component property is initialized with
inject()(no explicit type annotation)What happened?
With the
angularplugin active,fallow dead-codereports a service method as an unused class member when its only reference is in an Angular HTML template (prop.method()), and the component propertypropis initialized with the moderninject()pattern without an explicit type annotation:readonly exampleService = inject(ExampleService);Fallow already credits template references through component properties for the other three DI/instance patterns — it is specifically the untyped
inject()initializer that is missed. The following all resolve correctly (no false positive); only the last one fails:Component property pattern Template prop.method()resolved?constructor(public svc: ExampleService) {}✅ yes svc: ExampleService = inject(ExampleService);(typed)✅ yes svc = new ExampleService();✅ yes svc = inject(ExampleService);(untyped)❌ no — false positive Because untyped
inject()is the idiomatic Angular style, this misfires on a very common real-world pattern.Reproduction
Minimal standalone repro.
@angular/coreonly needs to be listed inpackage.jsonso theangularplugin auto-activates — runningnpm installis optional (without it, fallow prints a "node_modules directory not found" warning but the finding still reproduces).-
Create
package.json:{ "name": "fallow-angular-template-repro", "version": "1.0.0", "private": true, "dependencies": { "@angular/core": "^20.0.0" } } -
Create
.fallowrc.json:{ "entry": ["src/main.ts"] } -
Create
src/main.ts:import { ExampleComponent } from './example.component'; console.log(ExampleComponent);
-
Assistant
Let me analyze this issue. The problem is that
fallow dead-codereports a service method as unused when it's referenced in an Angular HTML template through a component property that's initialized withinject()without an explicit type annotation.Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the crates directory structure and look for Angular-related code.
Assistant
Let me search more specifically for Angular plugin or template-related code:
Assistant
I can see there's an
angular-inject-class-memberstest fixture. Let me explore it and the sfc_template directory:Assistant
Let me look at the sfc_template extraction code and Angular template handling:
Assistant
I need to use the correct argument name for the open command.
Assistant
Let me see the full file, especially the middle part that was truncated:
Assistant
Now let me search for where Angular template references are resolved to component class members. The issue is about the
inject()pattern not being recognized:Assistant
I can see the key code. Let me look at the
extract_angular_inject_targetfunction and how it's used:Assistant
Now I can see the issue. Let me look at the
extract_angular_inject_targetfunction more closely:Assistant
Now I see the issue. The
extract_angular_inject_targetfunction checks for:- Type arguments (e.g.,
inject<Type>()) - Arguments that are simple identifiers (e.g.,
inject(ExampleService))
But in Angular, when you call
inject(ExampleService),ExampleServiceis passed as aType<T>token. The function correctly extracts this as an argument identifier.But the issue is the property definition handler. Let me look at the condition where it processes
inject()calls:- Type arguments (e.g.,
Assistant
Wait, I see the code does handle
inject()calls invisit_property_definition. Let me look at the test that already exists:Assistant
Let me understand the flow better. The issue is about Angular template references being resolved through component properties. When the template has
exampleService.onValueChange(...), the template scanner extractsexampleServiceas an identifier andexampleService.onValueChangeas a member access chain.The problem is: how does the system know that property on the component has type ? This happens through .
Assistant
Let me search for how Angular template references are resolved to component members.
Assistant
Let me look at the unused_members.rs file to understand how Angular template references are resolved to component class members. This is where the magic happens.
Assistant
Let me find the function that specifically handles Angular template sentinel accesses:
Assistant
This is interesting. The
angular_tpl_chain_accesseslogic processes member access chains from Angular templates. It looks forclass_heritageandcomponent_bindings(which areinstance_bindings). Let me look further to understand how the chain accesses are resolved.Assistant
Now I understand the flow. The
component_bindingsmaps property names to their types, and this comes fromclass_heritage.instance_bindings. Let me find whereinstance_bindingsis populated:Assistant
Let me look at the
extract_class_instance_bindingsfunction in helpers.rs:Assistant
Now I found the issue. The
extract_class_instance_bindingsfunction inhelpers.rshandles:- Constructor parameters with type annotations
- Property definitions with type annotations
- Property definitions with
newexpressions
But it does NOT handle
inject()calls! Thevisit_property_definitioninvisit_impl.rshandlesinject()forbinding_target_names(used for internal member access resolution likethis.inner.aaa), butinstance_bindings(used for Angular template chain resolution) is populated byextract_class_instance_bindingswhich doesn't handleinject().Let me first create a reproduction script to confirm the issue:
Assistant
Let me create the reproduction files to confirm the issue before fixing the code.
Assistant
Good, the test files are created. Now let me continue with creating the HTML template and running the test.
Assistant
Now let me build the project and run the dead-code check to reproduce the issue.
Assistant
Let me check if there's already a built binary available.
Assistant
The release build doesn't seem to have the fallow binary itself, but there's an npm package. Let me check if there's a debug build or use the npm binary:
Assistant
Let me try a debug build instead since release might be incomplete:
Assistant
Now let me run the fallow binary to reproduce the issue:
Assistant
I've successfully reproduced the issue. The
onValueChangemethod is incorrectly reported as unused. Now let me look at theextract_class_instance_bindingsfunction inhelpers.rsand add support forinject()calls. The fix needs to detect when a property is initialized withinject(SomeType)and add that binding.Assistant
Now I understand the issue. The
extract_class_instance_bindingsfunction inhelpers.rshandles:- Constructor parameters with type annotations ✅
- Property definitions with type annotations ✅
- Property definitions with
newexpressions ✅ - Property definitions with
inject()calls ❌ MISSING
The fix is to add support for detecting
inject(Type)calls in the property definition handler. Since this function doesn't have access to import information, I'll follow the same pattern asnewexpressions - just check for a call expression with an identifier namedinjectand extract the first argument.Assistant
Now I need to understand what imports are available and how to properly detect the
inject()call. The functionextract_class_instance_bindingsdoesn't have access to the module's imports (it only takes aClass), so it can't verify ifinjectis from@angular/core. However, the same pattern is used fornewexpressions - it just checks the identifier name.Let me look at how the code handles
newexpressions more closely and add similar logic forinject():Assistant
Now I understand the structure. I need to add a check for
inject()calls after thenewexpression check. The pattern should be:if let Some(Expression::CallExpression(call)) = &prop.value && let Expression::Identifier(callee) = &call.callee && callee.name.as_str() == "inject" { // Extract the first argument as the type name if let Some(Argument::Identifier(arg)) = call.arguments.first() { bindings.push((name.to_string(), arg.name.to_string())); } }Let me make this fix:
Assistant
Now let me edit the file to add the inject() call handling after the new expression handling:
Assistant
Now I need to replace the relevant lines in helpers.rs with the new code that includes inject() handling.
Assistant
Let me check if the edit was applied correctly:
Assistant
The edit looks correct. Now let me rebuild and test:
Assistant
The build succeeded. Now let me run the reproduction test again to see if the fix works:
Assistant
The issue is still there. Let me investigate more. The
extract_class_instance_bindingsfunction is used to populateinstance_bindingsin class heritage. But I need to check if the Angular plugin is actually being activated. The warning says "node_modules directory not found" - maybe the Angular plugin isn't being activated.Let me look at the Angular plugin activation logic:
Assistant
Let me look at the Angular plugin specifically:
Assistant
I see. The Angular plugin is activated when
@angular/coreis in the dependencies. Sincenode_modulesis not installed, the plugin might not be activated. But the issue description says the warning doesn't prevent the finding from reproducing.Let me check if the template file is being properly read and if the issue is actually about how the member access chain is being resolved. Let me look at what member accesses are being extracted:
Assistant
Let me look at the html.rs file to see how Angular templates are processed:
Assistant
Looking at this, the template file produces member accesses with the sentinel object. The template
<input (input)="exampleService.onValueChange($event.target.value)" />should produce:- A
MemberAccesswithobject: "__angular_tpl__"andmember: "exampleService"(from the identifiers) - A
MemberAccesswithobject: "exampleService"andmember: "onValueChange"(from the member access chain)
The issue is that when resolving chain accesses, the analysis code looks for
component_bindingswhich maps property names to type names. My fix toextract_class_instance_bindingsshould populate this, but I need to verify that the Angular plugin is actually detecting and processing the template file.Let me first create a test fixture to verify the fix works at the extraction level:
- A
Assistant
Let me check the existing tests related to instance_bindings:
Assistant
Let me look at one of those tests to understand how instance_bindings work:
Assistant
Let me run the existing tests to make sure my change doesn't break anything:
Assistant
All 315 visitor tests pass. Now let me write a specific test to verify my fix works at the extraction level:
Assistant
Let me look at the existing inject tests and add a new one for instance bindings:
Assistant
I see that the existing test
angular_inject_field_maps_this_field_member_accessteststhis.inner.aaamember access mapping, which works throughbinding_target_namesinvisit_property_definition. But the issue is aboutinstance_bindingsused by the Angular template resolution.Let me check how the
extract_class_instance_bindingsfunction is tested:Assistant
Let me look at the test that checks instance_bindings for new expressions:
Assistant
Let me look for a test that verifies instance_bindings with property definitions (new expressions):
Assistant
Now let me verify the fix is working at the unit test level. Let me run a test that verifies instance_bindings:
Assistant
All existing tests pass. Now let me run the integration tests related to Angular template members:
Assistant
All Angular-related tests pass. Now let me look at the specific integration test for Angular template members and understand what it's testing:
Assistant
Let me look at the
angular-template-inherited-membersfixture to understand the pattern being tested:Assistant
I see! In the existing fixture,
DataViewComponentusesconstructor(public dataService: DataService)for DI injection, which is the pattern that works. The issue is specifically aboutinject()without type annotation.Let me now understand the problem better. Looking at the code flow:
- The Angular template scanner extracts
exampleService.onValueChangewithobject: "exampleService"andmember: "onValueChange" - The analysis code looks up
component_bindings(frominstance_bindings) to mapexampleService->ExampleService - But
instance_bindingsis populated byextract_class_instance_bindingswhich didn't handleinject()
Now I've added the
inject()handling toextract_class_instance_bindings. Let me run the tests again to confirm my fix doesn't break anything, and then re-test the reproduction:- The Angular template scanner extracts
Assistant
All 1681 tests pass. Now let me run the reproduction test again:
Assistant
Ah wait, I need to rebuild the binary first since I just made changes to the code.
Assistant
I need to rebuild the binary since I made changes to the code. Let me run the cargo build command.
Assistant
Let me force a recompilation to make sure the changes are picked up:
Assistant
Now let me test the reproduction again.
Assistant
Let me investigate more. The issue might be that without
node_modules, the Angular plugin isn't fully activated. Let me create a minimal node_modules to enable it:Assistant
I need to continue investigating why the fix isn't working. The Angular plugin needs to be activated. Let me run the test again with the minimal node_modules structure I created.
Assistant
The warning about node_modules is gone, so the Angular plugin is now activated. But the issue persists. Let me investigate further to understand why my fix isn't working.
The issue might be that the component class is not being recognized as an Angular component, or the template file isn't being properly linked to the component.
Let me check if the existing test fixture works and understand the difference between that fixture and my reproduction case.
Looking at the existing fixture
angular-template-inherited-members, theDataViewComponentuses:constructor(public dataService: DataService) {}And my reproduction uses:
readonly exampleService = inject(ExampleService);The difference is that one uses constructor injection (which works) and the other uses
inject()(which doesn't work).Let me trace the code path more carefully. The
extract_class_instance_bindingsfunction is used to populate in class heritage. Let me check if this function is being called for the component class in my reproduction case.Assistant
The Angular plugin is now activated (no warning). But the issue persists. Let me debug further by looking at how class heritage is extracted and checking if the
@Componentdecorator affects the flow.Assistant
Let me look at the context around where extract_class_instance_bindings is called to understand when it's invoked.
Assistant
I see - the
extract_class_instance_bindingsfunction is called for every class declaration. Let me verify my change is correct by looking at the modified code again.Assistant
Looking at the issue more carefully, my fix adds
inject()toextract_class_instance_bindings, but the problem might also be in how the analysis phase connects template references to component bindings. Let me trace the analysis code to understand the full flow.Assistant
Looking at the analysis code more carefully, I see that
component_bindingscomes from the component'sclass_heritage, andangular_tpl_chain_accessescomes from the template file. The code iterates over imports to connect them. But Angular templates viatemplateUrlaren't traditional imports.Let me check how the Angular template file is linked to its component.
Assistant
Let me look at how the template URL is extracted and how it's connected to the component module.
Assistant
Let me trace how the Angular template URL is resolved and connected to the component module. The issue might be in how the template file is linked to its component in the analysis phase.
Assistant
Let me look at how Angular template files are resolved and linked to their components.
Assistant
Let me investigate how the Angular template file is linked to its component in the analysis.
Assistant
Let me trace the code path where Angular component metadata is processed and how it connects to the template file.
Assistant
Let me trace through the analysis more carefully. The key is to verify that
extract_class_instance_bindingsis now correctly returning the binding forinject(). Let me write a quick test.Assistant
Let me run the Angular template members integration test to verify the fix works end-to-end.
Assistant
Let me check the angular-inject-class-members test to see if it's testing instance_bindings.
Assistant
The tests are passing. Now I need to understand why the reproduction case still fails. Let me look at the reproduction more carefully and compare it with the test fixture that works.
The issue is that in my reproduction, the
ExampleComponentdoesn't extend another class and the component's instance_bindings now includesexampleService -> ExampleService. But the analysis code needs to:- Find the template file (
example.component.html) - Extract the member access chain (
exampleService.onValueChange) - Look up
exampleServicein the component's instance_bindings to getExampleService - Mark
ExampleService.onValueChangeas used
Let me check if the template file is being properly parsed and if the chain access is being extracted correctly.
- Find the template file (
Assistant
Now I see the issue! The
angular_tpl_chain_accessesis built from modules that haveANGULAR_TPL_SENTINELmember accesses. But the template HTML file produces both:MemberAccesswithobject: ANGULAR_TPL_SENTINELandmember: "exampleService"(from identifiers)MemberAccesswithobject: "exampleService"andmember: "onValueChange"(from member access chains)
The chain access with
object: "exampleService"should be captured. Let me verify that the Angular template scanner is correctly extracting this chain.But wait - the analysis code looks at
angular_tpl_chain_accesseswhich is keyed by the template file'sFileId. Then it looks at the component module's imports to find the target_id of the template. This should work if the template is properly linked as an import.Let me trace through the code to see if the HTML template is being resolved correctly as a side-effect import of the component.
Assistant
Let me investigate the code flow more carefully to understand why the fix isn't working.
Assistant
Let me verify that the
instance_bindingsare correctly populated forinject()by writing a targeted test.Assistant
Let me add a test to verify that
inject()creates an instance binding. I'll look at the existing test structure and add a test for this specific case.Assistant
I need to write a test that verifies
inject()creates an instance binding. Let me look at one of the existing tests for instance bindings to understand the pattern.Assistant
Let me add a test to verify that
inject()creates an instance binding, to confirm my fix works at the extraction level.Assistant
Let me look at the existing test for injected_object_member_access_mapped_to_class to understand the pattern, and then I need to add a test that specifically checks that inject() creates instance_bindings (not just binding_target_names).
Assistant
Let me look at the existing integration test for inject fields and understand the difference between binding_target_names and instance_bindings.
Assistant
I need to understand the difference between
binding_target_namesandinstance_bindings. Let me look at the integration test to see what it's testing.Assistant
I see now! The existing test
angular_inject_fields_credit_service_member_usageteststhis.inner.aaawhich usesbinding_target_names(internal member access). But the issue is about Angular template references likeexampleService.onValueChange(...)which usesinstance_bindings.These are two different code paths:
binding_target_names- for resolvingthis.prop.memberwithin the same classinstance_bindings- for resolving template references likeprop.memberto the actual class
My fix should handle the second case. Let me verify that
instance_bindingsis correctly populated forinject()calls by adding a unit test.Assistant
Let me write a simple test to verify that
inject()creates instance_bindings correctly.Assistant
Let me search for where the tests verify instance_bindings and add a simple test to confirm my fix works:
Assistant
Let me create a quick test to verify that my fix correctly extracts instance bindings from
inject()calls. I'll write a temporary test script.Assistant
Assistant
Let me verify my fix works correctly by testing a few scenarios. First, let me check if the typed
inject()case works (which should work because of the type annotation):Assistant
Good, the typed
inject()case works because the type annotation creates the instance binding. Now let me test the untyped case to confirm it fails:Assistant
Typed
inject()works. Now let me test the untyped case:Assistant
Let me test the untyped inject() case to confirm whether my fix works.
Assistant
Excellent! The fix works! The untyped
inject()case now passes. Let me verify all the control cases mentioned in the issue to make sure everything works correctly:Assistant
Control A (constructor injection) works. Let me test the other control cases.
Assistant
Control B (typed inject) works. Let me test Control C (new expression):
Assistant
All control cases work. Now let me test the main issue case (untyped inject) and verify it's fixed:
Assistant
All four patterns now work correctly:
- ✅ Constructor injection
- ✅ Typed
inject() - ✅
newexpression - ✅ Untyped
inject()(the issue case) - NOW FIXED
Let me run all existing tests to make sure the fix doesn't break anything:
Assistant
All 1681 tests pass. Let me also run the core tests to make sure the fix works end-to-end.
Assistant
All 641 core tests pass. Let me verify my changes by viewing the diff:
Assistant
Let me test some edge cases to make sure my fix handles them properly. I'll check:
- Edge case:
inject()with a non-identifier argument (e.g., optional provider token) - Edge case: Private property with
inject() - Edge case: Non-Angular
inject()function (should not create binding)
- Edge case:
Assistant
Interesting - this edge case behaves correctly:
- The private property
exampleServiceis correctly skipped for template binding - The template reference
exampleService.onValueChangecan't resolve through the private property, soonValueChangeis now correctly reported as unused - Only
doSomethingis reported as unused (notonValueChange)
Wait, actually this seems like the correct behavior since the property is private and the template can't access it. Let me verify that the service method is still being credited through the internal
this.exampleService.onValueChange()call.Let me also test that the existing tests still pass and verify my fix handles the case properly.
- The private property
Assistant
The fix correctly handles the case where
inject()has additional arguments (like{optional: true}). The first argument is still the type, so it works correctly.Now let me run the full test suite to make sure everything passes:
Assistant
Let me verify my fix is complete by checking the final diff and confirming all tests pass.
Assistant
I have successfully resolved the issue. Let me provide a summary of the fix:
Summary
The issue was that
fallow dead-codeincorrectly reported a service method as unused when:- The service method was referenced in an Angular HTML template via a component property
- The component property was initialized with
inject()without an explicit type annotation
Root Cause
The
extract_class_instance_bindingsfunction incrates/extract/src/visitor/helpers.rsextracts instance bindings that map component property names to their types. These bindings are used by the analysis phase to resolve Angular template references to actual class members.The function already handled:
- Constructor parameters with type annotations ✅
- Property definitions with type annotations ✅
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.