softwaremill / softwaremill/macwire

Macwire does not consider outer trait members to be candidates for wiring

Open
#120 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.3k
Forks
77
Avg merge
9m
Merged PRs (30d)
4

Description

If I have a trait with an inner trait like this:

trait FooComponents {
  lazy val fooService: FooService = wire[FooService]

  trait FooRequestScopedComponents {
    def request: RequestHeader
    def fooController: FooController = wire[FooController]
  }
}

Macwire (2.3.0) is unable to see the fooService in the outer scope and will report an error like FooComponents.scala:20: Cannot find a value of type: [services.FooService]. Writing the code manually works fine.

My motivation here is to try to create a set of "request components", which are instantiated per-request and can have a dependency on the request. I prefer not to use a traditional "request scope" that uses thread locals because that can be hard to manage across thread boundaries in async frameworks like Play, and it seems far less intuitive than normal dependency injection.

I have a basic example Play app showing the approach here: https://github.com/gmethvin/play-scala-macwire-di-example/blob/50f4771/app/GreetingApplicationLoader.scala. I'd like to be able to replace many of those manual constructor calls with wire calls though.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the nested-trait example in the issue and the linked Play example at app/GreetingApplicationLoader.scala, then trace Macwire's wiring-resolution entry point for visible members. Done means the inner FooController wiring can use the outer fooService without the reported missing-value error, with the example behavior verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.