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

Avoid stringer-related deadlocks without adding ISGOMOCK #204

Merged
merged 1 commit into from
Sep 18, 2024

Conversation

JacobOaks
Copy link
Contributor

@JacobOaks JacobOaks commented Sep 18, 2024

A deadlock related to controller calling Stringer on mocks themselves
was revealed in #116. #144 solved this deadlock by adding a generated
ISGOMOCK() method to all generated mocks, and then checking for it
before calling .String() on arguments.

This reveals an exported method on each generated mock that:

  • Bloats the generated code
  • Can be taken dependency on in strange ways via Hyrum's Law
  • Technically opens up another route for naming collision.

This PR attempts to clean up this type of check by instead generating
an unexported field in generated mock structs instead, and checks for it using reflect.
This hides this implementation detail of gomock/mockgen better,
and produces less generated bloat.

Note that, importantly, the regression test added in #144 still passes
with this PR.

Ref: #193

@JacobOaks JacobOaks force-pushed the joaks/reflect_gomock_detection branch from 9a164ea to f042bfa Compare September 18, 2024 16:50
A deadlock related to controller calling Stringer on mocks themselves
was revealed in uber-go#116. uber-go#144 solved this deadlock by adding a generated
`ISGOMOCK()` method to all generated mocks, and then checking for it
before calling `.String()` on arguments.

This reveals an exported method on each generated mock that:
* Bloats the generated code
* Can be taken dependency on in strange ways via Hyrum's Law
* Technically opens up another route for naming collision.

This PR attempts to clean up this type of check by instead generating
an unexported field in generated mock structs instead, and checks for it using reflect.
This hides this implementation detail of gomock/mockgen better,
and produces less generated bloat.

Note that, importantly, the regression test added in uber-go#144 still passes
with this PR.

Ref: uber-go#193
@JacobOaks JacobOaks force-pushed the joaks/reflect_gomock_detection branch from f042bfa to 1f58549 Compare September 18, 2024 17:03
@JacobOaks JacobOaks requested a review from r-hang September 18, 2024 17:06
Copy link
Contributor

@r-hang r-hang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@JacobOaks JacobOaks merged commit 33ccc99 into uber-go:main Sep 18, 2024
4 checks passed
@JacobOaks JacobOaks deleted the joaks/reflect_gomock_detection branch September 18, 2024 19:00
apricote pushed a commit to hetznercloud/fleeting-plugin-hetzner that referenced this pull request Oct 18, 2024
…eting-plugin-hetzner!135)

This MR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [go.uber.org/mock](https://github.com/uber/mock) | require | minor | `v0.4.0` -> `v0.5.0` |

---

### Release Notes

<details>
<summary>uber/mock (go.uber.org/mock)</summary>

### [`v0.5.0`](https://github.com/uber-go/mock/releases/tag/v0.5.0)

[Compare Source](uber-go/mock@v0.4.0...v0.5.0)

#### 0.5.0 (15 Oct 2024)

##### Added

-   [#&#8203;153][]: Add `--write_command_comment` flag to specify whether to include
    `Generated by this command` comment.
-   [#&#8203;191][]: Add `--build_constraint` flag to add `//go:build` directives
    to generated mocks
-   [#&#8203;214][]: Add gob mode to support custom package loading techniques in place
    of --exec_only

##### Changed

-   [#&#8203;181][]: Made mockgen faster by changing flags passed to `go list`.
-   [#&#8203;183][]: Made `Cond` matcher generic.
-   [#&#8203;204][]: Removed `ISGOMOCK()` from generated mocks.
-   [#&#8203;207][]: Deprecated reflect mode and replaced it with the new package mode.

##### Fixed

-   [#&#8203;144][]: Fix a deadlock that can happen when mocking an interface that
    matches `fmt.Stringer`.
-   [#&#8203;168][]: Fix an issue where the "generated by" comment was being included
    in the package comment of generated mocks.

[#&#8203;144]: uber-go/mock#144

[#&#8203;153]: uber-go/mock#153

[#&#8203;168]: uber-go/mock#168

[#&#8203;181]: uber-go/mock#181

[#&#8203;183]: uber-go/mock#183

[#&#8203;191]: uber-go/mock#191

[#&#8203;204]: uber-go/mock#204

[#&#8203;207]: uber-go/mock#207

[#&#8203;214]: uber-go/mock#214

Thanks to [@&#8203;tulzke](https://github.com/tulzke) [@&#8203;JacobOaks](https://github.com/JacobOaks) [@&#8203;ARR4N](https://github.com/ARR4N) [@&#8203;sashamelentyev](https://github.com/sashamelentyev) [@&#8203;sywhang](https://github.com/sywhang) [@&#8203;fasmat](https://github.com/fasmat)
[@&#8203;eyasy1217](https://github.com/eyasy1217) [@&#8203;ghouscht](https://github.com/ghouscht) [@&#8203;tie](https://github.com/tie) [@&#8203;Neo2308](https://github.com/Neo2308) [@&#8203;carson-brill](https://github.com/carson-brill) [@&#8203;alexandear](https://github.com/alexandear) [@&#8203;sodul](https://github.com/sodul)
[@&#8203;nbgraham](https://github.com/nbgraham) for their contributions this release.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4xMjcuMCIsInVwZGF0ZWRJblZlciI6IjM4LjEyNy4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
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

Successfully merging this pull request may close these issues.

2 participants