Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations not parsed when running Sourcery with the same template but different output files #1357

Open
alexdmotoc opened this issue Aug 10, 2024 · 1 comment

Comments

@alexdmotoc
Copy link
Contributor

Hi!

I have a setup where I have one AutoMockable.stencil file that I use as template for 2 configurations. One configuration outputs in a file in the unit tests target, and one in the App target.

I use this to generate mocks for SwiftUI previews and also have them for unit tests.

Another note is, I use 2 // sourcery: AutoMockable annotations:

  • the standard // sourcery: AutoMockable - used to output in the tests target file
  • a new // sourcery: AutoMockableApp - used to output in the App target file

I also have some custom flags configured in the .sourcery.yml to be able to differentiate which configuration is run (the unit tests or app) in order to customize the resulting mocks.

Finally, the super-weird part is that some times it works, but if I run sourcery multiple times consecutively, it breaks and doesn't find the annotations in one of the configurations

My .sourcery.yml looks like this

configurations:
  - sources: 
      - Sources/
    templates:
      - /Stencils/AutoMockable.stencil
    output: Tests/Mocks/GeneratedMocks.swift
    args:
      isTestTargetMocks: true
      autoMockableImports: [
        "Combine",
      ]
      autoMockableTestableImports: [
        "MyApp"
      ]
  - sources: 
      - Sources/
    templates:
      - /Stencils/AutoMockable.stencil
    output: MyApp/Core/Generators/GeneratedBuilders.swift
    args:
      isAppTargetMocks: true
      autoMockableImports: [
        "Combine",
      ]

The AutoMockable.stencil is the same like the template one on this repo, just modified it a bit to not generate CalledCount, etc for the App target mocks.

Mocked protocol looks like this

// sourcery: AutoMockable
// sourcery: AutoMockableApp
protocol ArticlePagedLoaderType: ObservableObject {
    var articles: [Article] { get }
    var isLoading: Bool { get }
}

Attached photos of the output in the App target

Any ideas what could be the problem?

Screenshot 2024-08-10 at 21 39 13
Screenshot 2024-08-10 at 21 39 33

@alexdmotoc alexdmotoc changed the title Annotations not parse when running Sourcery with the same template but different output files Annotations not parsed when running Sourcery with the same template but different output files Aug 10, 2024
@alexdmotoc
Copy link
Contributor Author

I investigated a bit, and it turns out you can reproduce it quite easily. The setup is:

protocol SomeTypeUsingGenericConstraint: AutoMockable {
    associatedtype MyGenericConstraint: SomeTypeToBeUsedAsGenericConstraint
}

protocol SomeTypeToBeUsedAsGenericConstraint: AutoMockable {
    func doStuff()
}

You just need to run sourcery multiple times, one after the other. I tried with --disableCache but it still fails. SomeTypeToBeUsedAsGenericConstraint is not generated anymore, only SomeTypeUsingGenericConstraint

We have sourcery as a pre-build phase (don't ask why, it was there when I found it 😸 ) and since we're working with SwiftUI and Previews, you can imagine sourcery is called quite often when the previews are built.

Would appreciate any insight on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant