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: fix COPY command error due to missing but ignored files #2812

Conversation

QuanZhang-William
Copy link
Collaborator

Description
Fixes #1598.

This commit puts context.ExcludesFile before os.Lstat to avoid the COPY command error due to missing but ignored files.

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes unit tests
  • Adds integration tests if needed.

See the contribution guide for more details.

Reviewer Notes

  • The code flow looks good.
  • Unit tests and or integration tests added.

Release Notes

Describe any changes here so maintainer can include it in the release notes, or delete this block.

Ignore file missing error in `COPY` command if the file is to be ignored

@QuanZhang-William QuanZhang-William changed the title fix: fix COPY command error due to missing but ignored files fix: fix COPY command error due to missing but ignored files Oct 23, 2023
@QuanZhang-William QuanZhang-William marked this pull request as ready for review October 23, 2023 21:12
pkg/commands/copy_test.go Outdated Show resolved Hide resolved
}

err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you help me to understand what's the error as described in #1598 could have happened without this fix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @JeromeJu. IIUC #1598 describes a racing condition between 2 process. Say process A 1) finds all the files to be copied; and 2) copies the files one by one. If process B kicks in and deletes some file between 1) and 2), it will throw an error. However, if the files to be copied are supposed to be ignored, it should not fail since the files will not be copied anyways. That's all swapping the 2 function calls solve the problem.

Regarding the tests, I didn't find a way to mock the race condition in unit test, so I just added a test case with ignored files. Please let me know if you have better way to test it 😁

Copy link
Collaborator

@aaron-prindle aaron-prindle Oct 31, 2023

Choose a reason for hiding this comment

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

One way of doing this is via "monkey patching" (done elsewhere in the code) where we can mock out RemainingFiles or os.Lstat to delete an excluded file after/before processing and use this mock for a repro test. I have done this here - https://gist.github.com/aaron-prindle/7f7fd1f43dc5c3fe4e9b14f69ecdd9e7

I don't think it is necessary for the code in this case though, I have manually tested using the above test @ HEAD vs @this-PR and this does fix the issue (as expected). Looks good!

Fixes GoogleContainerTools#1598.

This commit puts `context.ExcludesFile` before `os.Lstat` to avoid the `COPY` command error due to missing but ignored files.
Copy link
Collaborator

@aaron-prindle aaron-prindle left a comment

Choose a reason for hiding this comment

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

LGTM! Left one comment related to adding a test that repro's the old issue exactly. I have manually tested with such a test and it is working as expected - I think it's fine to merge as is

@@ -461,6 +462,60 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
}
})

t.Run("copy dir to another dir - with ignored files", func(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding this!

@aaron-prindle aaron-prindle merged commit b433ddd into GoogleContainerTools:main Oct 31, 2023
10 checks passed
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.

Can we put context.ExcludesFile before os.Lstat when copyDir?
3 participants