Discussion:
[guice] Misleading error message with child injector (#877)
Laurent Martelli
2014-11-07 15:30:54 UTC
Permalink
If you have a child injector and ask the parent injector a value for a binding defined in the child, you get an "It was already configured on one or more child injectors" error.

It would be better to say "No implementation for ... was bound, although it is configured in a child injector".

Here's a sample code:

```java
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class GuiceBug {

public static void main(String[] args) {
Injector i1 = Guice.createInjector();
Injector i2 = i1.createChildInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(IStuff.class).to(Stuff.class);
}
});
i1.getInstance(IStuff.class);
}

static interface IStuff {}

static class Stuff implements IStuff {}
}
```
```
Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:

1) Unable to create binding for org.enercoop.test.GuiceBug$IStuff. It was already configured on one or more child injectors or private modules
bound at org.enercoop.test.GuiceBug$1.configure(GuiceBug.java:15)
If it was in a PrivateModule, did you forget to expose the binding?
while locating org.enercoop.test.GuiceBug$IStuff

1 error
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at GuiceBug.main(GuiceBug.java:18)
```


---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Sam Berlin
2014-11-07 15:39:20 UTC
Permalink
What makes you say it's misleading? The error is correct: you can't get the binding from the parent because it's bound in the child. Leading with "no implementation was bound" seems more misleading, because one *was* bound.. just in a different place.

---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877#issuecomment-62162625
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Laurent Martelli
2014-11-07 15:48:47 UTC
Permalink
Saying "it was **already** configured" make me think it was configured twice which is not the case. It does not suggest it was configured in another place. If you configure a binding in both parent and child you also get "A binding to ... was **already** configured at ...".

---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877#issuecomment-62164102
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Sam Berlin
2014-11-07 15:59:47 UTC
Permalink
If you have one apple and someone takes that apple, when you try to get it again the result it "You can't, someone **already** took the apple". The apple wasn't taken twice. When you try to get it the second time, you're told it was already taken.

This is the same. You try to create a binding on the parent (by way of getting a binding that doesn't exist on it yet) and the result is you're told you can't create the binding because it was already configured on a child injector.

---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877#issuecomment-62165873
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Laurent Martelli
2014-11-07 16:23:16 UTC
Permalink
I get your point, but I still find this confusing, because "[Elements of the child injector are not visible to its parent](http://google-guice.googlecode.com/git-history/3.0-rc1/javadoc/com/google/inject/Injector.html#createChildInjector%28com.google.inject.Module...%29)". So if I want something from the parent which is not defined in it, it fails just because of that, not because of anything defined or not in the children.

---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877#issuecomment-62169669
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Sam Berlin
2014-11-07 16:43:21 UTC
Permalink
Closed #877.

---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877#event-190020519
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Sam Berlin
2014-11-07 16:43:21 UTC
Permalink
That's incorrect. It fails precisely because it's defined in the child. If it was just not defined in the parent, then the binding would be created at runtime as a just-in-time binding. It can't create the just-in-time binding, though, because the binding already exists in the child, and Guice doesn't allow bindings in both the parent & child.

---
Reply to this email directly or view it on GitHub:
https://github.com/google/guice/issues/877#issuecomment-62172879
--
You received this message because you are subscribed to the Google Groups "google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice-dev+***@googlegroups.com.
To post to this group, send email to google-guice-***@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.
Loading...