Skip to content

fix(matryer): honor interface-level skip-ensure imports - #1185

Open
fzlzjerry wants to merge 1 commit into
vektra:v3from
fzlzjerry:fix/1082-matryer-ensure-imports
Open

fzlzjerry wants to merge 1 commit into
vektra:v3from
fzlzjerry:fix/1082-matryer-ensure-imports

Conversation

@fzlzjerry

Copy link
Copy Markdown

Description

Fixes #1082.

Register the Matryer source-package import when at least one interface actually emits an ensure declaration. The import section previously checked file-level TemplateData, while the declarations check each interface's effective TemplateData.

That mismatch can produce either an unused source import (an interface opts out) or an undefined package qualifier (the file opts out and an interface opts back in).

This changes only the ensure-related registration. Configuration inheritance is unchanged; method-type imports remain intact, and explicit add-import entries are still processed before automatic ensure imports so a later interface can supply the source alias.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Version of Go used when building/testing:

  • 1.26.8
  • 1.27.1

How Has This Been Tested?

  • Twelve template/registry regression cases, including both override directions, mixed interfaces, same-package generation, existing type imports, and aliases supplied by later interfaces. Generated code is parsed and checked with go/types, not just formatted.
  • Four end-to-end cases build and invoke the real CLI, compile the generated mocks in separate modules, and call the generated methods/check call histories. Three fail on the original implementation; all four pass with the fix. Four of the twelve template cases also fail before the fix.
  • Eleven additional real CLI/consumer scenarios reproduce six original compiler failures; all eleven compile and run after the change. The five previously working controls retain byte-identical generated code.
  • The repository's complete test.ci flow passes on Go 1.26.8 and 1.27.1: lint, complete mock regeneration, git-state, 217 unit-test events, shell generation checks, and 10 end-to-end test events. The Go 1.26 run was repeated with --force so Task did not reuse lint/tool-building cache decisions.
  • GOFLAGS='-mod=readonly -race' GOWORK=off go test -count=1 -json ./... passes with 229 test events and no failures; the flag is inherited by the CLI/consumer subprocesses too.

Reproduce the focused tests:

GOWORK=off go test ./internal ./e2e/test_matryer_skip_ensure \
  -run '^(TestMatryerEnsureImports|TestInterfaceSkipEnsure)$' -count=1 -v

No generated fixtures, dependency manifests, or template-data merge rules are changed.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

No documentation change is needed: the existing interface-level option now generates compilable code consistently.

Match source-package registration to the ensure declarations actually
emitted for each interface, without changing template-data inheritance
or explicit import alias precedence.

Fixes vektra#1082
Copilot AI lite review requested due to automatic review settings September 10, 2026 11:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment on lines +38 to +42
dir := t.TempDir()
require.NoError(t, os.Mkdir(filepath.Join(dir, "api"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com/skipensure\n\ngo 1.25.5\n"), 0o600))
source := "package api\ntype First interface { Ping() string }; type Second interface { Pong() string }; type Value struct { N int }; type Typed interface { Get() Value }\n"
require.NoError(t, os.WriteFile(filepath.Join(dir, "api", "api.go"), []byte(source), 0o600))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of writing out the test file each iteration, can you make a test fixture with this source file, add the config to the root mockery config file, and then write a test file that asserts the behavior of the mock file? Adding mock generation in each test like this will quickly balloon the test run time. That's why the tests generate all of the mock files once and then assert the result of those outputs.

This branch has not been deployed

No deployments
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.

v3: adds unused import in matryer template with template.skip-ensure

3 participants