Skip to content

fix: skip sub-directories when copying a plugin directory#582

Open
arpitjain099 wants to merge 1 commit into
notaryproject:mainfrom
arpitjain099:fix/copydir-skip-subdirectories
Open

fix: skip sub-directories when copying a plugin directory#582
arpitjain099 wants to merge 1 commit into
notaryproject:mainfrom
arpitjain099:fix/copydir-skip-subdirectories

Conversation

@arpitjain099

Copy link
Copy Markdown

While reading the plugin install path I noticed that internal/file.CopyDirToDir doesn't behave the way its comment describes. It's meant to copy only the top-level regular files of a directory, but the skip-subdirectories guard never fires.

The WalkDir callback names its path parameter path, so the check d.Name() != filepath.Base(path) compares the current entry's base name against itself and is always false. The walk therefore descends into every sub-directory and copies nested files, flattening them into the destination. parsePluginFromDir in the same package gets this right by comparing against the walk root, so the two disagree on what a plugin directory contains: Install validates the top level but then copies nested files too.

The fix compares the walked path against the source root, matching the existing parsePluginFromDir pattern. I added a regression test that fails before the change (a nested file gets copied) and passes after. Verified with go test ./internal/file/... ./plugin/... on linux/amd64.

Thanks for taking a look.

CopyDirToDir is documented to copy only the top-level regular files of a
directory, but its WalkDir callback names its path parameter "path",
which shadows nothing useful and makes the skip check
d.Name() != filepath.Base(path) always false (both sides resolve to the
current entry's base name). As a result the walk descended into every
sub-directory and copied nested files, flattening them into the
destination.

Compare the walked path against the source root instead, matching the
pattern already used in parsePluginFromDir. Add a regression test.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

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 fixes internal/file.CopyDirToDir so it truly copies only the top-level regular files of a directory by preventing filepath.WalkDir from descending into subdirectories. It also adds a regression test to ensure nested files in subdirectories are not copied during plugin installation.

Changes:

  • Fix subdirectory skipping logic in CopyDirToDir by comparing walked paths against the walk root (src).
  • Add a regression test verifying only top-level regular files are copied and that non-directory sources return ErrNotDirectory.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/file/file.go Corrects the WalkDir callback logic so subdirectories are properly skipped when copying.
internal/file/file_test.go Adds a regression test to prevent nested files from being copied and validates non-directory error behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants