script icon indicating copy to clipboard operation
script copied to clipboard

FindFiles exits and returns no files when it can't open a directory

Open jreisinger opened this issue 2 years ago • 8 comments

This program

package main

import (
	"log"
	"os"

	"github.com/bitfield/script"
)

func main() {
	h, err := os.UserHomeDir()
	if err != nil {
		log.Fatal(err)
	}
	_, err = script.FindFiles(h).Stdout()
	if err != nil {
		log.Fatal(err)
	}
}

yields:

$ go build
$ ./script
2022/03/12 11:51:51 open /Users/user/.Trash: operation not permitted

I expected behavior similar to find $HOME -type f, i.e. to return files from directories it can open.

jreisinger avatar Mar 12 '22 11:03 jreisinger

Thanks, @jreisinger! Yes, that seems a reasonable expectation.

bitfield avatar Mar 12 '22 11:03 bitfield

It seems like FindFiles should just ignore any errors encountered by filepath.Walk. I think while we're working on that, it would make sense to update FindFiles to use fs.WalkDir instead of filepath.Walk, wouldn't it? Then it would be easier to test (see Walking with filesystems).

bitfield avatar Mar 15 '22 18:03 bitfield

@bitfield agree that using fs.WalkDir with fs.SkipDir would provide a nice DX here.

Thinking that we'd still want to include the permission errors in the output though (rather than completely swallowing), as this is more aligned with the default behavior of find?

Maybe checking to see if the fs.WalkDirFunc returns fs.ErrPermission, and if so, append <path-to-file>: permission denied to filenames. Otherwise if any other err type is encountered, it should be piped out WithError.

The developer could always just Reject lines containing "permission denied" from the resulting pipe of a successful find.

parkerduckworth avatar Mar 27 '22 05:03 parkerduckworth

@parkerduckworth it's a tricky area, because one of the design goals for script is to just get out of your way so that you can cook up quick hacks, and in the event of errors it should just "do the right thing", without blocking your task.

bitfield avatar Mar 27 '22 09:03 bitfield

Ah, ok. Understood

On Mar 27, 2022, at 4:43 AM, John Arundel @.***> wrote:

 @parkerduckworth it's a tricky area, because one of the design goals for script is to just get out of your way so that you can cook up quick hacks, and in the event of errors it should just "do the right thing", without blocking your task.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

parkerduckworth avatar Mar 27 '22 14:03 parkerduckworth

Happy to accept PRs for this.

bitfield avatar Jun 02 '22 11:06 bitfield

@bitfield great, just submitted one

parkerduckworth avatar Jun 02 '22 21:06 parkerduckworth