Skip to content

Conversation

@rfwatson
Copy link

@rfwatson rfwatson commented Jan 21, 2026

Description

Add goimports local-prefix configuration. Fixes #1098.

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.22
  • 1.23

How Has This Been Tested?

Mostly manual testing. It seems that testing the actual formatter call could be a bit tricky, happy to take a look at adding some if you think it's worth it though.

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

@rfwatson rfwatson force-pushed the feat/goimports-local-prefix branch from 5b14a70 to bc0ef83 Compare January 21, 2026 06:01
@rfwatson rfwatson force-pushed the feat/goimports-local-prefix branch from bc0ef83 to dc7ca2f Compare January 21, 2026 06:01
@rfwatson rfwatson marked this pull request as ready for review January 21, 2026 06:53
Copy link
Member

@LandonTClipp LandonTClipp left a comment

Choose a reason for hiding this comment

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

Looks great, let's just extend this a bit more.

config/config.go Outdated
Recursive *bool `koanf:"recursive" yaml:"recursive,omitempty"`
ForceFileWrite *bool `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
Formatter *string `koanf:"formatter" yaml:"formatter,omitempty"`
FormatterOptions FormatterOptions `koanf:"formatter-options" yaml:"formatter-options,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

This should be *FormatterOptions so that the yaml map can be nil.

Copy link
Author

Choose a reason for hiding this comment

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

Wouldn't we just end up with a zero value FormatterOptions in that case? I was thinking to do it this way to avoid the nil checks later, might be missing something though.

Copy link
Member

Choose a reason for hiding this comment

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

The reason is pretty complicated. It has to do with the migrate command when going from v2 to v3. If you set the default in koanf then mockery will never have nil values for it.

Copy link
Member

Choose a reason for hiding this comment

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

This is good but I think people also want to be able to modify the imports.Options struct. I think we can do a functional option style of thing. I suggest something like this:

type FormatterOptions struct {
	GoImports *GoImports
}

type GoImports struct {
	LocalPrefix *string ...
    TabWidth *int ...
}

func (g GoImports) Options() imports.Options {
    return imports.Options{
      TabWidth: g.TabWidth,
      // etc
    }
}

Then:

func goimports(src []byte, opt GoImports) ([]byte, error) {
    imports.LocalPrefix = opt.LocalPrefix
    formatted, err := imports.Process("/", src, opt.Options())

We'd want to set default values in here: https://github.com/rfwatson/mockery/blob/dc7ca2f8c944d58757e85aef489fee65a5da13fd/config/config.go#L68-L85

@rfwatson rfwatson marked this pull request as draft January 28, 2026 07:51
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.

Feature request: support formatter options like goimports local prefix

2 participants