diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 5f597bf0677efc8f77d1da2d71752268e32173a2..7e57fd7ca2a335bad90f56cfa56809f74194b23b 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -155,6 +155,9 @@ Path canonPath(const Path & path, bool resolveSymlinks)
                     s.clear();  /* restart for symlinks pointing to absolute path */
                 } else {
                     s = dirOf(s);
+                    if (s == "/") {  // we don’t want trailing slashes here, which dirOf only produces if s = /
+                        s.clear();
+                    }
                 }
             }
         }