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

fix(cli codegen): fixed missing imports #3046

Merged
merged 1 commit into from
Jan 7, 2024

Conversation

fredbi
Copy link
Contributor

@fredbi fredbi commented Jan 5, 2024

This PR addresses an issue with goimports not being able
to find another generated package.

When generating files in 2 packages simultaneously (i.e. cli imports models
and files are being generated model per model in both packages),
there are situations when goimports fails to properly resolve the imported
package (i.e. the imported package is not completed and may have errors
while generating).

This fix proceeds in 2 steps: first generate all models, then proceed with
a final generation step ("PostModels") that iterates again over all
models. It is a bit slower, but imports are now safe.

Other additions with this PR:

  • updated dependency to golang.org/x/tools
  • added go-openapi/strfmt to default imports (was missing and added by
    goimports, but it is best to make it explicit and avoid unnecessary
    import fixes)
  • in templates, added standard library imports
  • relinted code generated by the CLI templates (check for errors, unused
    args, shadowed variables, prefer // over block comments, blank lines...)
  • updated cli.gotmpl to use os.UserConfigDir() and os.HomeDir() rather
    than importing a third party package
  • added guard for multiline .Description in calls to PersistentFlags()

Tests:

Signed-off-by: Frederic BIDON fredbi@yahoo.com

@fredbi fredbi marked this pull request as ready for review January 5, 2024 10:55
@fredbi fredbi marked this pull request as draft January 5, 2024 10:56
@fredbi fredbi force-pushed the fix/2969-cli branch 2 times, most recently from c1df1a9 to 76cace6 Compare January 5, 2024 19:58
@fredbi fredbi changed the title Fix/2969 cli fix(cli codegen): fixed missing imports Jan 5, 2024
@fredbi
Copy link
Contributor Author

fredbi commented Jan 5, 2024

Built on top of #3045


// look for default config (OS-specific, e.g. ".config" on linux)
configDir, err = os.UserConfigDir()
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

nit: indent

* fixes go-swagger#2969

This PR addresses an issue with goimports not being able
to find another generated package.

When generating files in 2 packages simultaneously (i.e. cli imports models
and files are being generated model per model in both packages),
there are situations when goimports fails to properly resolve the imported
package (i.e. the imported package is not completed and may have errors
while generating).

This fix proceeds in 2 steps: first generate all models, then proceed with
a final generation step ("PostModels") that iterates again over all
models. It is a bit slower, but imports are now safe.

Other additions with this PR:
* updated dependency to golang.org/x/tools
* added go-openapi/strfmt to default imports (was missing and added by
  goimports, but it is best to make it explicit and avoid unnecessary
  import fixes)
* in templates, added standard library imports
* relinted code generated by the CLI templates (check for errors, unused
  args, shadowed variables, prefer // over block comments, blank lines...)
* updated cli.gotmpl to use os.UserConfigDir() and os.HomeDir() rather
  than importing a third party package
* added guard for multiline .Description in calls to PersistentFlags()

Tests:
* added CLI codegen tests for go-swagger#2969, go-swagger#2650 and the latest docker spec
  (used to generate go-swagger/dockerctl)

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

reindented cli.gotmpl

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Copy link

codecov bot commented Jan 7, 2024

Codecov Report

Attention: 9 lines in your changes are missing coverage. Please review.

Comparison is base (36e5a26) 82.67% compared to head (be68d7d) 82.64%.
Report is 3 commits behind head on master.

Files Patch % Lines
generator/template_repo.go 71.42% 4 Missing and 2 partials ⚠️
generator/shared.go 84.21% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3046      +/-   ##
==========================================
- Coverage   82.67%   82.64%   -0.03%     
==========================================
  Files          62       62              
  Lines       12818    12856      +38     
==========================================
+ Hits        10597    10625      +28     
- Misses       1685     1692       +7     
- Partials      536      539       +3     
Flag Coverage Δ
codegen-oldstable-canary-fixtures 32.55% <7.31%> (-0.09%) ⬇️
codegen-stable-canary-fixtures 32.55% <7.31%> (-0.09%) ⬇️
unit-oldstable 82.79% <78.04%> (-0.03%) ⬇️
unit-stable 82.79% <78.04%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fredbi fredbi merged commit 1e29a3d into go-swagger:master Jan 7, 2024
27 of 29 checks passed
@fredbi fredbi deleted the fix/2969-cli branch January 7, 2024 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

generating a command line : undefined: cli
3 participants