Skip to content

Conversation

@JacobOaks
Copy link
Contributor

v0.5.0 included #207, which replaced reflect mode with package mode. One issue with package mode that came up (ref: #216) was that generated mocks for interfaces that referred to alias types were referring to the aliases' underlying names instead.

e.g.,
some package:

package somgpkg

import "somepkg/internal/apicodec"
...
type Codec = apicodec.Codec

mockgen input:

type Foo interface{
	Bar() somepkg.Codec
}

mock:

func (m *MockFoo) Bar() apicodec.Codec { // This is a problem, since apicodec is an internal package.
    // ...
}

While technically this problem is solved in Go 1.23 with explicit alias types representation, (indeed, if you run mockgen on the example in the linked issue with GODEBUG=gotypesalias=1, you get the expected behavior) since we support the last two versions, we can't bump go.mod to 1.23 yet. This leaves us with the old behavior, where go/types does not track alias types. You can tell if an object is an alias, but not a type itself, and there is no way to retrieve the object of interest at the point where we are recursively parsing method types.

This PR works around this issue (temporarily) by using syntax information to find all references to aliases in the source package. When we find one, we record it in a mapping of underlying type -> alias name. Later, while we parse the type tree, we replace any underlying types in the mapping with their alias names.

The unexpected side effect of this is that all references to the underlying type in the generated mocks will be replaced with the alias, even if the source used the underlying name. This is fine because:

  • If the alias is in the mapping, it was used at least once, which means its accessible.
  • From a type-checking perspective, aliases and their underlying types are equivalent.

The nice exception to the side effect is when we explicitly request mock generation for an alias type, since at that point we are dealing with the object, not the type.

With this PR, the mocks get generated correctly now:

func (m *MockFoo) Bar() Codec {
    // ...
}

Once we can bump go.mod to 1.23, we should definitely remove this, since the new type alias type nodes solve this problem automatically.

v0.5.0 included uber-go#207, which replaced reflect mode with package mode.
One issue with package mode that came up (ref: uber-go#216) was that
generated mocks for interfaces that referred to alias types
were referring to the aliases' underlying names instead.

e.g.,
source:
```go
import "github.com/tikv/client-go/v2/internal/apicodec"
...
type Codec = apicodec.Codec

type Foo interface{
	Bar() Codec
}
```
mock:
```go
func (m *MockFoo) Bar() apicodec.Codec { // This is a problem, since apicodec is an internal package.
    // ...
}
```

While technically this problem is solved in Go 1.23 with explicit alias types representation,
(indeed, if you run mockgen on the example in the linked issue with
`GODEBUG=gotypesalias=1`, you get the expected behavior)
since we support the last two versions, we can't bump `go.mod` to 1.23 yet.
This leaves us with the old behavior, where `go/types` does not track alias types.
You can tell if an object is an alias, but not a type itself,
and there is no way to retrieve the object of interest
at the point where we are recursively parsing method types.

This PR works around this issue (temporarily) by using syntax information
to find all references to aliases in the source package.
When we find one, we record it in a mapping of underlying type -> alias name.
Later, while we parse the type tree, we replace any underlying types
in the mapping with their alias names.

The unexpected side effect of this is that _all_ references to the underlying type
in the generated mocks will be replaced with the alias, even if
the source used the underlying name. This is fine because:
* If the alias is in the mapping, it was used at least once, which means its accessible.
* From a type-checking perspective, aliases and their underlying types are equivalent.

With this PR, the mocks get generated correctly now:
```go
func (m *MockFoo) Bar() Codec {
    // ...
}
```

Once we can bump `go.mod` to 1.23, we should definitely remove this,
since the new type alias type nodes solve this problem automatically.
@JacobOaks JacobOaks marked this pull request as ready for review October 24, 2024 14:59
@JacobOaks JacobOaks requested a review from sywhang October 24, 2024 14:59
@JacobOaks JacobOaks merged commit c205527 into uber-go:main Oct 28, 2024
3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Apr 7, 2025
apricote pushed a commit to hetznercloud/fleeting-plugin-hetzner that referenced this pull request Apr 8, 2025
…eting-plugin-hetzner!238)

This MR contains the following updates:

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

---

### Release Notes

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

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

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

#### 0.5.1 (7 Apr 2025)

##### Fixed

-   [#&#8203;220][]: Package mode will now generate code that uses aliases of types
    when they are used in the source.
-   [#&#8203;219][]: Fixed a collision between function argument names and package names
    in generated code.
-   [#&#8203;165][]: Fixed an issue where aliases specified by `-imports` were not being
    respected in generated code.

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

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

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

Thanks to [@&#8203;mtoader](https://github.com/mtoader) and [@&#8203;bstncartwright](https://github.com/bstncartwright) for their contributions to this release.

</details>

---

### Configuration

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

🚦 **Automerge**: Enabled.

♻ **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:eyJjcmVhdGVkSW5WZXIiOiIzOS4yMzUuMiIsInVwZGF0ZWRJblZlciI6IjM5LjIzNS4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
DennisRasey pushed a commit to DennisRasey/forgejo that referenced this pull request Apr 8, 2025
This PR contains the following updates:

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

---

### Release Notes

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

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

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

#### 0.5.1 (7 Apr 2025)

##### Fixed

-   [#&#8203;220][]: Package mode will now generate code that uses aliases of types
    when they are used in the source.
-   [#&#8203;219][]: Fixed a collision between function argument names and package names
    in generated code.
-   [#&#8203;165][]: Fixed an issue where aliases specified by `-imports` were not being
    respected in generated code.

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

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

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

Thanks to [@&#8203;mtoader](https://github.com/mtoader) and [@&#8203;bstncartwright](https://github.com/bstncartwright) for their contributions to this release.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "* 0-3 * * *" (UTC), Automerge - "* 0-3 * * *" (UTC).

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

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

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

---

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

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yMzMuNSIsInVwZGF0ZWRJblZlciI6IjM5LjIzMy41IiwidGFyZ2V0QnJhbmNoIjoiZm9yZ2VqbyIsImxhYmVscyI6WyJkZXBlbmRlbmN5LXVwZ3JhZGUiLCJ0ZXN0L25vdC1uZWVkZWQiXX0=-->

Co-authored-by: Gusted <[email protected]>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7488
Reviewed-by: Earl Warren <[email protected]>
Co-authored-by: Renovate Bot <[email protected]>
Co-committed-by: Renovate Bot <[email protected]>
JacobOaks added a commit to JacobOaks/mock that referenced this pull request Apr 21, 2025
Alias replacements derived from syntax were introduced in uber-go#220
as a way to ensure the aliases used in source code were also used.
This helped ensure packages mode worked on go1.22, which didn't have
explicit alias node support in the `go/types` package.

Alias replacements have a couple issues:
* They flat out replace any would-be references to an underlying type
  with an alias type.
* They don't properly handle aliases to generic type instantiations (ref: uber-go#243)

Now that go1.24 is released, we can bump `go.mod` to go1.23,
which means we can ensure `go/types` has an explicit `types.Alias` node
for type aliases, and we can remove the alias replacement logic.
JacobOaks added a commit to JacobOaks/mock that referenced this pull request Apr 21, 2025
Alias replacements derived from syntax were introduced in uber-go#220
as a way to ensure the aliases used in source code were also used.
This helped ensure packages mode worked on go1.22, which didn't have
explicit alias node support in the `go/types` package.

Alias replacements have a couple issues:
* They flat out replace any would-be references to an underlying type
  with an alias type.
* They don't properly handle aliases to generic type instantiations (ref: uber-go#243)

Now that go1.24 is released, we can bump `go.mod` to go1.23,
which means we can ensure `go/types` has an explicit `types.Alias` node
for type aliases, and we can remove the alias replacement logic.
JacobOaks added a commit to JacobOaks/mock that referenced this pull request Apr 21, 2025
Alias replacements derived from syntax were introduced in uber-go#220
as a way to ensure the aliases used in source code were also used.
This helped ensure packages mode worked on go1.22, which didn't have
explicit alias node support in the `go/types` package.

Alias replacements have a couple issues:
* They flat out replace any would-be references to an underlying type
  with an alias type.
* They don't properly handle aliases to generic type instantiations (ref: uber-go#243)

Now that go1.24 is released, we can bump `go.mod` to go1.23,
which means we can ensure `go/types` has an explicit `types.Alias` node
for type aliases, and we can remove the alias replacement logic.
JacobOaks added a commit to JacobOaks/mock that referenced this pull request Apr 21, 2025
Alias replacements derived from syntax were introduced in uber-go#220
as a way to ensure the aliases used in source code were also used.
This helped ensure packages mode worked on go1.22, which didn't have
explicit alias node support in the `go/types` package.

Alias replacements have a couple issues:
* They flat out replace any would-be references to an underlying type
  with an alias type.
* They don't properly handle aliases to generic type instantiations (ref: uber-go#243)

Now that go1.24 is released, we can bump `go.mod` to go1.23,
which means we can ensure `go/types` has an explicit `types.Alias` node
for type aliases, and we can remove the alias replacement logic.
JacobOaks added a commit to JacobOaks/mock that referenced this pull request Apr 21, 2025
Alias replacements derived from syntax were introduced in uber-go#220
as a way to ensure the aliases used in source code were also used.
This helped ensure packages mode worked on go1.22, which didn't have
explicit alias node support in the `go/types` package.

Alias replacements have a couple issues:
* They flat out replace any would-be references to an underlying type
  with an alias type.
* They don't properly handle aliases to generic type instantiations (ref: uber-go#243)

Now that go1.24 is released, we can bump `go.mod` to go1.23,
which means we can ensure `go/types` has an explicit `types.Alias` node
for type aliases, and we can remove the alias replacement logic.
JacobOaks added a commit that referenced this pull request Apr 28, 2025
Alias replacements derived from syntax were introduced in #220 as a way
to ensure the aliases used in source code were also used. This helped
ensure packages mode worked on go1.22, which didn't have explicit alias
node support in the `go/types` package.

Alias replacements have a couple issues:
* They flat out replace any would-be references to an underlying type
with an alias type.
* They don't properly handle aliases to generic type instantiations
(ref: #243)

Now that go1.24 is released, we can bump `go.mod` to go1.23, which means
we can ensure `go/types` has an explicit `types.Alias` node for type
aliases, and we can remove the alias replacement logic.
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.

3 participants