Skip to content

cli/command/container: copyToContainer: improve error-handling#6254

Merged
thaJeztah merged 2 commits intodocker:masterfrom
thaJeztah:cp_cleanup
Aug 13, 2025
Merged

cli/command/container: copyToContainer: improve error-handling#6254
thaJeztah merged 2 commits intodocker:masterfrom
thaJeztah:cp_cleanup

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

cli/command/container: copyToContainer rename error-return

Make it more clearly identifiable where we're dealing with the
named error-return

cli/command/container: copyToContainer: improve error-handling

The logic used in this function was confusing; some errors were ignored,
but responses handled regardless. The intent here is to try to detect
whether the destination exists inside the container and is of the right
"type" (otherwise produce an error).

Failing to "stat" the path in the container means we can't produce a
nice error for the user, but we'll continue the request, which either
would succeed or produce an error returned by the daemon.

While working on this patch, I noticed that some error-handling on the
daemon side is incorrect. This patch does not fix those cases, but
makes the logic slightly easier to follow (we should consider extracting
the "stat" code to a separate function though).

- Human readable description for the release notes

- A picture of a cute animal (not mandatory but encouraged)

Make it more clearly identifiable where we're dealing with the
named error-return

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah added this to the 29.0.0 milestone Aug 12, 2025
@thaJeztah thaJeztah requested a review from Copilot August 12, 2025 22:13

This comment was marked as outdated.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Aug 12, 2025

Codecov Report

❌ Patch coverage is 38.09524% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cli/command/container/cp.go 38.09% 11 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

This comment was marked as outdated.

This comment was marked as outdated.

The logic used in this function was confusing; some errors were ignored,
but responses handled regardless. The intent here is to try to detect
whether the destination exists inside the container and is of the right
"type" (otherwise produce an error).

Failing to "stat" the path in the container means we can't produce a
nice error for the user, but we'll continue the request, which either
would succeed or produce an error returned by the daemon.

While working on this patch, I noticed that some error-handling on the
daemon side is incorrect. This patch does not fix those cases, but
makes the logic slightly easier to follow (we should consider extracting
the "stat" code to a separate function though).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves error handling and code clarity in the copyToContainer function within the Docker CLI. The changes focus on making error handling more explicit and improving the logic flow when dealing with container path validation.

Key changes:

  • Removes named error return to improve clarity of error handling flow
  • Restructures stat error handling to be more explicit about when errors are intentionally ignored
  • Adds explicit error acknowledgment for intentionally ignored stat operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

return fmt.Errorf(`destination "%s:%s" must be a directory or a regular file: %w`, copyConfig.container, dstPath, err)
}
dstInfo.Path = linkTarget
dstStat, err = apiClient.ContainerStatPath(ctx, copyConfig.container, linkTarget)
Copy link

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

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

The error from the second ContainerStatPath call is not handled. If this call fails, the subsequent code at line 332 will still execute with a potentially invalid dstStat, which could cause issues in ValidateOutputPathFileMode.

Suggested change
dstStat, err = apiClient.ContainerStatPath(ctx, copyConfig.container, linkTarget)
dstStat, err = apiClient.ContainerStatPath(ctx, copyConfig.container, linkTarget)
if err != nil {
// Ignore any error and assume that the parent directory of the destination
// path exists, in which case the copy may still succeed. If there is any
// type of conflict (e.g., non-directory overwriting an existing directory
// or vice versa) the extraction will fail. If the destination simply did
// not exist, but the parent directory does, the extraction will still
// succeed.
_ = err // Intentionally ignore stat errors (see above)
return nil
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

READ THE BLOODY COMMENT @copilot - it's not hard, really, you should try it!

@thaJeztah thaJeztah requested a review from Benehiko August 12, 2025 22:27
@thaJeztah
Copy link
Copy Markdown
Member Author

CoPilot is not entirely wrong; the current logic is still somewhat confusing, and I'm planning to extract it to a separate function, but still want to do some more digging into some parts before moving it 😂

I'll bring this one in, as at least we got rid of the confusing named error-return and my IDE no longer is screaming about unhandled errors.

@thaJeztah thaJeztah merged commit e8876ed into docker:master Aug 13, 2025
87 checks passed
@thaJeztah thaJeztah deleted the cp_cleanup branch August 13, 2025 09:22
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.

4 participants