hugo icon indicating copy to clipboard operation
hugo copied to clipboard

readDir/readFile should return nil when not found

Open bep opened this issue 2 years ago • 8 comments

Or to be more specific: nil for readFile and a []os.FileInfo for readDir (so it can be ranged over even if empty).

This is in line with resources.Get etc. and what all would expect.

bep avatar Mar 07 '22 12:03 bep

is the code located in @bep here?

https://github.com/gohugoio/hugo/blob/673cde1eb122888509cca32e322662af08187ff1/tpl/os/os.go#L71

and

https://github.com/gohugoio/hugo/blob/673cde1eb122888509cca32e322662af08187ff1/tpl/os/os.go#L102

can I jump in to help?

rubiagatra avatar Mar 10 '22 04:03 rubiagatra

Yes, that's the code, and please create a PR. Looking at the code, readFile returns a string, so an empty string when not found would be the medicine.

It would also be great if you could update the relevant snippets in /docs (in this repo) with a note about this new behaviour.

bep avatar Mar 10 '22 07:03 bep

Thinking about this, could you wait with this until we have merged #9613

bep avatar Mar 10 '22 08:03 bep

sure no problem!

rubiagatra avatar Mar 10 '22 12:03 rubiagatra

have you been able to address this @rubiagatra ? can I help?

dibrinsofor avatar Mar 13 '22 18:03 dibrinsofor

still waiting mentioned PR to merge @dibrinsofor

rubiagatra avatar Mar 14 '22 01:03 rubiagatra

Has this been fixed? Can I jump in to help?

stackkrocket avatar May 26 '22 00:05 stackkrocket

so an empty string when not found should be the medicine

It looks like it is like that, or maybe I am missing something. Can we possibly have some test that fails with current implementation?

func readFile(fs afero.Fs, filename string) (string, error) {
	filename = filepath.Clean(filename)
	if filename == "" || filename == "." || filename == string(_os.PathSeparator) {
		return "", errors.New("invalid filename")
	}

	b, err := afero.ReadFile(fs, filename)
	if err != nil {
		return "", err
	}

	return string(b), nil
}

The other method is already fixed in #9613 so I think you can close that one as fixed once #9613 gets merged.

smoorg avatar Aug 22 '22 10:08 smoorg

@bep Can I pick this one if not already done?

sandyydk avatar Oct 03 '22 09:10 sandyydk

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 26 '23 02:01 github-actions[bot]