Skip to content

Commit 0a3f20d

Browse files
committed
Fix 'is a directory' error in copyright holder migration
- Add isDirectory helper function to check for directories and symlinks to directories - Use helper in updateLicenseHolder and wouldUpdateLicenseHolder functions - Resolves issue where filepath.Walk's fi.IsDir() misses symlinks to directories - Fixes error: 'read path: is a directory' reported in PR feedback This prevents crashes when processing repositories with symbolic links that point to directories, such as version directories in test fixtures.
1 parent 19c688d commit 0a3f20d

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

addlicense/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,29 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data Li
363363
return true, os.WriteFile(path, b, fmode)
364364
}
365365

366+
// isDirectory checks if the given path points to a directory (including through symlinks)
367+
func isDirectory(path string) (bool, error) {
368+
fi, err := os.Stat(path)
369+
if err != nil {
370+
return false, err
371+
}
372+
return fi.IsDir(), nil
373+
}
374+
366375
// updateLicenseHolder checks if a file contains old copyright holders
367376
// (like "HashiCorp, Inc.") and updates them to the new holder while
368377
// preserving years and other header information.
369378
// Returns true if the file was updated.
370379
func updateLicenseHolder(path string, fmode os.FileMode, newData LicenseData) (bool, error) {
380+
// Skip directories and symlinks to directories
381+
isDir, err := isDirectory(path)
382+
if err != nil {
383+
return false, err
384+
}
385+
if isDir {
386+
return false, nil
387+
}
388+
371389
b, err := os.ReadFile(path)
372390
if err != nil {
373391
return false, err
@@ -435,6 +453,15 @@ func updateLicenseHolder(path string, fmode os.FileMode, newData LicenseData) (b
435453
// wouldUpdateLicenseHolder checks if a file would need copyright holder updates
436454
// without actually modifying the file. Used for plan/dry-run mode.
437455
func wouldUpdateLicenseHolder(path string, newData LicenseData) (bool, error) {
456+
// Skip directories and symlinks to directories
457+
isDir, err := isDirectory(path)
458+
if err != nil {
459+
return false, err
460+
}
461+
if isDir {
462+
return false, nil
463+
}
464+
438465
b, err := os.ReadFile(path)
439466
if err != nil {
440467
return false, err

0 commit comments

Comments
 (0)