Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Misleading error caused by FactoryProvider(..., null) #1500

Closed
leonsenft opened this issue Jul 17, 2018 · 0 comments
Closed

Misleading error caused by FactoryProvider(..., null) #1500

leonsenft opened this issue Jul 17, 2018 · 0 comments

Comments

@leonsenft
Copy link
Contributor

When a FactoryProvider is configured with a null factory, the generated code behaves as though a ClassProvider was used instead.

Consider the following example.

const baseUrl = OpaqueToken<String>('baseUrl');

class Service {
  final String baseUrl;
  Service(@baseUrl this.baseUrl);
}

@Component(...)
class AppComponent implements OnInit {
  final Service service;
  AppComponent(this.service);

  @override
  void ngOnInit() {
    service.init();
  }
}

If we configured the following injector:

@GenerateInjector([
  FactoryProvider(Service, null),
])
final InjectorFactory injectorFactory => injectorFactory$Injector;

We see this error:

No provider found for OpaqueToken (Instance of 'OpaqueToken<String>') <String>('baseUrl'):
Service -> OpaqueToken (Instance of 'OpaqueToken<String>') <String>('baseUrl').

This error makes it appear as through a provider for Service was found. Furthermore, the fact that it knew to inject baseUrl implies that a ClassProvider(Service) was provided. I have a suspicious that when the factory of FactoryProvider is null, the compiler treats this condition as though the provider is a ClassProvider.

It's probably beneficial that we throw a build error if a FactoryProvider explicitly provides null, since this would never be desired.

@matanlurey matanlurey self-assigned this Aug 17, 2018
@matanlurey matanlurey added this to the =v5.1.0 milestone Aug 17, 2018
matanlurey added a commit that referenced this issue Aug 17, 2018
Turns a (mysterious) runtime error into a compile-time one. Shouldn't impact users.

Closes #1500.
Closes #1581.

PiperOrigin-RevId: 209173141
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants