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

gateway: restore original filename in ReadFile error message #3990

Merged
merged 2 commits into from
Jul 7, 2023

Conversation

jedevc
Copy link
Member

@jedevc jedevc commented Jul 5, 2023

All messages returned by os.Open are guaranteed to return a PathError. However, as these error messages are printed, they include the temporary directory for the mounted reference which is not useful to the caller.

On an error, we can restore the filename in the PathError to the requested filename, as also seen in os.DirFS.

In the following example, we perform a docker build in an empty directory:

Before:

$ docker buildx build .
[+] Building 0.0s (2/2) FINISHED                                                                                       docker:desktop-linux
 => [internal] load .dockerignore                                                                                                      0.0s
 => => transferring context: 2B                                                                                                        0.0s
 => [internal] load build definition from Dockerfile                                                                                   0.0s
 => => transferring dockerfile: 2B                                                                                                     0.0s
ERROR: failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount1333925779/Dockerfile: no such file or directory

After (note the no temp mount in the error message):

$ docker buildx build . --builder=dev
[+] Building 0.0s (1/1) FINISHED                                                                                                 remote:dev
 => [internal] load build definition from Dockerfile                                                                                   0.0s
 => => transferring dockerfile: 2B                                                                                                     0.0s
ERROR: failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory

(cc @nicks, we discussed simplification of docker error messages before)

All messages returned by os.Open are guaranteed to return a PathError.
However, as these error messages are printed, they include the temporary
directory for the mounted reference which is not useful to the caller.

On an error, we can restore the filename in the PathError to the
requested filename, as also seen in os.DirFS.

Signed-off-by: Justin Chadwell <[email protected]>
@jedevc jedevc requested a review from tonistiigi July 5, 2023 10:06
// The filename here is internal to the mount, so we can restore
// the request base path for error reporting.
// See os.DirFS.Open for details.
err.(*os.PathError).Path = req.Filename
Copy link
Member

@tonistiigi tonistiigi Jul 6, 2023

Choose a reason for hiding this comment

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

Although this is documented behavior, I don't like this direct type-cast. It should be type detection or errors.As.

if req.Range == nil {
dt, err = os.ReadFile(fp)
dt, err = io.ReadAll(f)
f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

f.Close() duplication in here could be cleaner with defer.

Technically the io.ReadAll() duplication is not needed as well but not sure if that part is worth optimizing.

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.

3 participants