diff --git a/wrappers/desktop.go b/wrappers/desktop.go index 79fafa041a0..bbf446300d6 100644 --- a/wrappers/desktop.go +++ b/wrappers/desktop.go @@ -303,9 +303,13 @@ func forAllDesktopFiles(cb func(base, instanceName string) error) error { } de, err := desktopentry.Read(desktopFile) - if err != nil || de.SnapInstanceName == "" { + if err != nil { // cannot read instance name from desktop file, ignore - logger.Noticef("cannot read instance name: %s", err) + logger.Noticef("cannot read instance name from %q: %v", desktopFile, err) + continue + } + if de.SnapInstanceName == "" { + logger.Noticef("cannot find X-SnapInstanceName entry in %q", desktopFile) continue } diff --git a/wrappers/desktop_test.go b/wrappers/desktop_test.go index 97ab3dd9bfb..c083996d77d 100644 --- a/wrappers/desktop_test.go +++ b/wrappers/desktop_test.go @@ -20,6 +20,7 @@ package wrappers_test import ( + "errors" "fmt" "os" "path/filepath" @@ -882,3 +883,24 @@ X-SnapInstanceName=foo`) c.Check(found["foo_foo.desktop"], Equals, "foo") c.Check(found["foo_bar.desktop"], Equals, "foo") } + +func (s *desktopSuite) TestForAllDesktopFilesSkipsBadDesktopFiles(c *C) { + c.Assert(os.MkdirAll(dirs.SnapDesktopFilesDir, 0755), IsNil) + + var mockEmpty []byte + var mockNoInstanceName = []byte(` +[Desktop Entry] +Name=foo`) + var mockInvalid = []byte(` +[Desktop Entry] +[Desktop Entry]`) + + c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, "empty.desktop"), mockEmpty, 0644), IsNil) + c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, "no-instance-name.desktop"), mockNoInstanceName, 0644), IsNil) + c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, "invalid.desktop"), mockInvalid, 0644), IsNil) + + err := wrappers.ForAllDesktopFiles(func(base, instanceName string) error { + return errors.New("unexpected call") + }) + c.Assert(err, IsNil) +}