You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a FormatOnly field to the imports.Options type. Setting FormatOnly to true will bypass the "fix" functionality so that no imports will be inserted or deleted. Default would be false and backward compatible.
Thanks for your consideration.
Rationale
I'm working on a Go code generator and would like for the generated import stanza to be formatted according to the standard goimports style. Some projects enforce goimports formatting such that improperly formatted files will cause a CI build failure.
However, we don't want the import-insertion logic of the imports package to execute. If our generator creates code with missing imports, we consider that a bug in the generator and don't want to mask the bugs by using imports.Process. We've also seen goimports insert the wrong package and want to avoid that scenario, too.
This proposal would also disable the deletion of unused imports, but that functionality is easily replicated with astutil.UsesImport. In our project, we already delete unused imports manually and would continue doing so prior to using imports.Process.
Alternatives
Add an option to go/format
An option could to be added to go/format to enable goimports formatting, but I'm doubtful that the Go team would care to alter go/format for this use. go/format does one thing (with no options): canonical gofmt formatting. Unless we're willing to change the canon (blasphemy!), this is a dead end.
Copy code
We looked at copying the necessary code out of x/tools/imports, but we didn't like that option. It ended up being more code than we cared to copy and added a lot of complexity that we felt was unnecessary.
Proof of Concept
diff --git a/imports/imports.go b/imports/imports.go
index e30946b..5342801 100644
--- a/imports/imports.go+++ b/imports/imports.go@@ -31,6 +31,8 @@ type Options struct {
Comments bool // Print comments (true if nil *Options provided)
TabIndent bool // Use tabs for indent (true if nil *Options provided)
TabWidth int // Tab width (8 if nil *Options provided)
++ FormatOnly bool // Disable the insertion and deletion of imports
}
// Process formats and adjusts imports for the provided file.
@@ -46,9 +48,11 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) {
return nil, err
}
- _, err = fixImports(fileSet, file)- if err != nil {- return nil, err+ if !opt.FormatOnly {+ _, err = fixImports(fileSet, file)+ if err != nil {+ return nil, err+ }
}
sortImports(fileSet, file)
The text was updated successfully, but these errors were encountered:
I created this mess (goimports having an opinion on import blocks, where gofmt doesn't) so I'll take this bug. No promises, but I promise to at least think about it.
Proposal
Add a
FormatOnly
field to theimports.Options
type. SettingFormatOnly
totrue
will bypass the "fix" functionality so that no imports will be inserted or deleted. Default would befalse
and backward compatible.Thanks for your consideration.
Rationale
I'm working on a Go code generator and would like for the generated import stanza to be formatted according to the standard
goimports
style. Some projects enforcegoimports
formatting such that improperly formatted files will cause a CI build failure.However, we don't want the import-insertion logic of the
imports
package to execute. If our generator creates code with missing imports, we consider that a bug in the generator and don't want to mask the bugs by usingimports.Process
. We've also seengoimports
insert the wrong package and want to avoid that scenario, too.This proposal would also disable the deletion of unused imports, but that functionality is easily replicated with
astutil.UsesImport
. In our project, we already delete unused imports manually and would continue doing so prior to usingimports.Process
.Alternatives
Add an option to
go/format
An option could to be added to
go/format
to enablegoimports
formatting, but I'm doubtful that the Go team would care to altergo/format
for this use.go/format
does one thing (with no options): canonicalgofmt
formatting. Unless we're willing to change the canon (blasphemy!), this is a dead end.Copy code
We looked at copying the necessary code out of
x/tools/imports
, but we didn't like that option. It ended up being more code than we cared to copy and added a lot of complexity that we felt was unnecessary.Proof of Concept
The text was updated successfully, but these errors were encountered: