-
Notifications
You must be signed in to change notification settings - Fork 154
fix: resolve warnings and improve file extraction logic #773
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
base: master
Are you sure you want to change the base?
Conversation
Yay, your first pull request! 👍 A contributor will be by to give feedback soon. In the meantime, you can find updates in the #github-notifications channel in the community Slack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @ShigrafS, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request focuses on enhancing the codebase's stability and maintainability by systematically resolving existing warnings and linting issues. A primary aspect of these changes involves refining resource management, particularly ensuring that file and I/O operations are properly closed with robust error handling. Additionally, it introduces new CI tooling to streamline the debugging of linter checks.
Highlights
- Code Quality & Linting: Addressed various linting issues and warnings across the codebase to improve overall code quality and maintainability, as indicated by the PR title and description.
- Robust Resource Handling: Significantly improved resource handling by ensuring proper error-checked
defer
statements for closing files, readers, and writers across multiple utility and file processing functions, preventing potential resource leaks and improving stability. - Build & CI Improvements: Introduced a new GitHub Actions workflow (
.github/debug-build.yml
) to facilitate debugging linter runs on push events, and updated theMakefile
to allow passing extra arguments togolangci-lint
for more flexible execution.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request resolves warnings and improves file extraction logic by addressing unhandled errors from Close()
methods on various resources. The changes correctly identify where error handling was missing, particularly with defer
statements.
However, in several utility functions within utils/utils.go
, logging the error from Close()
is insufficient. For functions that write data to files or streams (like WriteToFile
, WriteYamlToFile
, Compress
), errors on Close()
can indicate critical failures (e.g., failure to flush buffered data) and should be propagated to the caller instead of being silently logged. My review focuses on these areas, suggesting patterns to correctly return these errors.
Additionally, I've provided a suggestion to optimize the new GitHub Actions workflow to be more resource-efficient.
utils/utils.go
Outdated
if err != nil { | ||
return ErrCreateFile(err, path) | ||
} | ||
defer func() { | ||
if cerr := file.Close(); cerr != nil { | ||
log.Printf("error closing file %s: %v", path, cerr) | ||
} | ||
}() | ||
|
||
_, err = file.WriteString(content) | ||
if err != nil { | ||
return ErrWriteFile(err, path) | ||
} | ||
// Close the file to save the changes. | ||
err = file.Close() | ||
if err != nil { | ||
return ErrWriteFile(err, path) | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The defer
statement logs the error from file.Close()
, but this can hide critical issues like a failed disk write on flush. The original code returned this error, and this behavior should be preserved. A caller of WriteToFile
needs to know if the write was successful, including the file being closed correctly.
To ensure the error is returned, use a named return value for the error and update it in the deferred function. This requires changing the function signature to func WriteToFile(path string, content string) (err error)
and then modifying the defer block to assign to err
.
func WriteToFile(path string, content string) (err error) {
file, err := os.Create(path)
if err != nil {
return ErrCreateFile(err, path)
}
defer func() {
if cerr := file.Close(); cerr != nil && err == nil {
err = cerr
log.Printf("error closing file %s: %v", path, cerr)
}
}()
_, err = file.WriteString(content)
if err != nil {
return ErrWriteFile(err, path)
}
return nil
}
utils/utils.go
Outdated
zr := gzip.NewWriter(buf) | ||
defer zr.Close() | ||
defer func() { | ||
if cerr := zr.Close(); cerr != nil { | ||
log.Printf("error closing gzip writer: %v", cerr) | ||
} | ||
}() | ||
tw := tar.NewWriter(zr) | ||
defer tw.Close() | ||
defer func() { | ||
if cerr := tw.Close(); cerr != nil { | ||
log.Printf("error closing tar writer: %v", cerr) | ||
} | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The errors from zr.Close()
and tw.Close()
are critical because these writers buffer data. A failure on Close
often means a failure to flush data, resulting in a corrupt or incomplete archive. These errors are currently only logged and should be returned by the Compress
function.
Use a named return value for the error and update it in the deferred functions. Since defer
executes in LIFO order, tw.Close()
will be called first, then zr.Close()
. The error handling logic should ensure the first non-nil error is returned.
func Compress(src string, buf io.Writer) (err error) {
zr := gzip.NewWriter(buf)
defer func() {
if cerr := zr.Close(); cerr != nil && err == nil {
err = cerr
log.Printf("error closing gzip writer: %v", cerr)
}
}()
tw := tar.NewWriter(zr)
defer func() {
if cerr := tw.Close(); cerr != nil && err == nil {
err = cerr
log.Printf("error closing tar writer: %v", cerr)
}
}()
return filepath.Walk(src, func(file string, fi os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
header, err := tar.FileInfoHeader(fi, file)
if err != nil {
return err
}
relPath, err := filepath.Rel(src, file)
if err != nil {
return err
}
header.Name = filepath.ToSlash(relPath)
if err := tw.WriteHeader(header); err != nil {
return err
}
if !fi.IsDir() {
data, err := os.Open(file)
if err != nil {
return err
}
defer func() {
if cerr := data.Close(); cerr != nil {
log.Printf("error closing file %s: %v", file, cerr)
}
}()
_, err = io.Copy(tw, data)
if err != nil {
return err
}
}
return nil
})
}
utils/utils.go
Outdated
defer func() { | ||
if cerr := data.Close(); cerr != nil { | ||
log.Printf("error closing file %s: %v", file, cerr) | ||
} | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error from data.Close()
is only logged inside this filepath.Walk
callback. An error during file close should be propagated up by returning it from this anonymous function. This will stop the filepath.Walk
and signal a failure in the compression process.
Consider not using defer
here and instead closing the file explicitly after io.Copy
, checking and returning any errors from both operations.
if data, err := os.Open(file); err == nil {
_, err = io.Copy(tw, data)
if err != nil {
data.Close() // ignore error
return err
}
err = data.Close()
if err != nil {
log.Printf("error closing file %s: %v", file, err)
return err
}
} else {
return err
}
.github/debug-build.yml
Outdated
on: | ||
push: | ||
branches: | ||
- '**' # Run on any branch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To optimize CI usage and reduce resource consumption, consider triggering this workflow on pull_request
events instead of every push to every branch ('**'
). This ensures that linting is performed on changes that are ready for review and merging.
If the goal is to allow developers to manually trigger it for debugging, add a workflow_dispatch
trigger.
on:
pull_request:
workflow_dispatch:
Thank you for your contribution!! |
Hi @vr-varad Please let me know your opinions. |
@ShigrafS I strongly suggest you create several PRs and fix issues 1 by 1 please, this will help reviewers |
209f155
to
77dbf28
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you adress comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShigrafS just made some comments
Signed-off-by: ShigrafS <[email protected]>
Signed-off-by: SHIGRAF SALIK <[email protected]>
Signed-off-by: SHIGRAF SALIK <[email protected]>
Signed-off-by: ShigrafS <[email protected]>
Signed-off-by: ShigrafS <[email protected]>
|
||
// formatComposeFile takes in a pointer to the compose file byte array and formats it | ||
// so that it is compatible with Kompose. It expects a validated Docker Compose file. | ||
func formatComposeFile(yamlManifest *DockerComposeFile) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we evaluate the impact on services calling this function since the signature has changed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatComposeFile
is only called once from within the Convert
function in this same file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, I did not notice it was a local function. Sorry for the noise
@@ -175,7 +182,7 @@ func clonewalk(g *Git) error { | |||
} | |||
|
|||
if !info.IsDir() { | |||
err = g.readFile(info, rootPath) | |||
err = g.readFile(info, rootPath, log) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why adding logger, here ?
@ShigrafS sorry for dealy and thanks for the work done, added small comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes to ensure files are closed properly
type composeFile struct { | ||
Version string `yaml:"version,omitempty"` | ||
// cleanup removes temporary files | ||
func cleanup(tempFilePath, resultFilePath string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the advantage to extract this in a dedicate function since it is bind to those files.
Also it should be done in a defer like previous implementation to ensure the files are closed properly. Here if one of the previous action fail the files will never be closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more used, write ?
Signed-off-by: ShigrafS <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small comments added
// A logger should be used here to warn about the failed cleanup. | ||
// e.g., log.Warnf("failed to remove temporary file %s: %v", tempFilePath, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not log here ?
// e.g., log.Warnf("failed to remove temporary file %s: %v", tempFilePath, err) | ||
} | ||
if err := os.Remove(resultFilePath); err != nil && !os.IsNotExist(err) { | ||
// e.g., log.Warnf("failed to remove result file %s: %v", resultFilePath, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -42,37 +43,55 @@ func Convert(dockerCompose DockerComposeFile) (string, error) { | |||
tempFilePath := filepath.Join(mesheryDir, "temp.data") | |||
resultFilePath := filepath.Join(mesheryDir, "result.yaml") | |||
|
|||
// Defer cleanup to ensure temp files are removed even on error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not util IMO
type composeFile struct { | ||
Version string `yaml:"version,omitempty"` | ||
// cleanup removes temporary files | ||
func cleanup(tempFilePath, resultFilePath string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more used, write ?
|
||
// formatComposeFile takes in a pointer to the compose file byte array and formats it | ||
// so that it is compatible with Kompose. It expects a validated Docker Compose file. | ||
func formatComposeFile(yamlManifest *DockerComposeFile) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, I did not notice it was a local function. Sorry for the noise
Closes: #755
Description
This draft PR addresses and aims to fix all existing errors, warnings, and lint issues throughout the codebase to improve overall code quality, stability, and maintainability. It includes fixes in utilities, sanitization, and other core packages with the following key changes:
defer
statements (e.g., closing files and readers).This PR serves as a foundation for a cleaner, more maintainable codebase by systematically resolving technical debt and improving developer experience.
Notes for Reviewers
[Signed commits](../CONTRIBUTING.md#signing-off-on-commits-developer-certificate-of-origin)