Plugins: Fix loading of dist folders (#80015)

* end to end

* tidy

* fix whitespace

* remove unused code

* fix linter

* fix gosec + add sort

* fix test

* apply cr feedback
This commit is contained in:
Will Browne
2024-01-08 11:45:03 +01:00
committed by GitHub
parent 0440b29ebf
commit 78ae795e06
6 changed files with 114 additions and 93 deletions

View File

@@ -27,7 +27,7 @@ func newWalker(rootDir string) *walker {
// it calls the walkFn passed.
//
// It is similar to filepath.Walk, except that it supports symbolic links and
// can detect infinite loops while following sym links.
// can detect infinite loops while following symlinks.
// It solves the issue where your WalkFunc needs a path relative to the symbolic link
// (resolving links within walkfunc loses the path to the symbolic link for each traversal).
func Walk(path string, followSymlinks bool, detectSymlinkInfiniteLoop bool, followDistFolder bool, walkFn WalkFunc) error {
@@ -112,20 +112,22 @@ func (w *walker) walk(path string, info os.FileInfo, resolvedPath string, symlin
subFiles = append(subFiles, subFile{path: path2, resolvedPath: resolvedPath2, fileInfo: fileInfo})
}
// If we have found a dist directory in a subdirectory (IE not at root path), and followDistFolder is true,
// then we want to follow only the dist directory and ignore all other subdirectories.
atRootDir := w.rootDir == path
if followDistFolder && w.containsDistFolder(subFiles) && !atRootDir {
return w.walk(filepath.Join(path, "dist"), info, filepath.Join(resolvedPath, "dist"), symlinkPathsFollowed,
followDistFolder, walkFn)
if followDistFolder && w.containsDistFolder(subFiles) {
err := w.walk(
filepath.Join(path, "dist"),
info,
filepath.Join(resolvedPath, "dist"),
symlinkPathsFollowed,
followDistFolder,
walkFn)
if err != nil {
return err
}
} else {
// Follow all subdirectories, with special handling for dist directories.
for _, p := range subFiles {
// We only want to skip a dist directory if it is not in the root directory, and followDistFolder is false.
if p.isDistDir() && !atRootDir && !followDistFolder {
if p.isDistDir() && !followDistFolder {
continue
}
err = w.walk(p.path, p.fileInfo, p.resolvedPath, symlinkPathsFollowed, followDistFolder, walkFn)
if err != nil {
return err