BUG: Relative Paths Fail In Govmomi Finder With Subfolders
Hey guys! Today, we're diving into a tricky bug that's been causing some headaches when working with relative paths and subfolders in govmomi and the vSphere Finder API. Let's get straight to the point and break down what's happening, how to reproduce it, and potential solutions.
Describe the Bug
When you're using govc or the govmomi Finder API, you might notice that relative paths containing subfolders—like ParentFolder/SubFolder—just don't resolve correctly. It's as if they're hitting a dead end! However, when you switch to absolute paths, everything magically works. The root cause? It seems the Finder automatically switches to LIST mode for any path containing a /. The catch is that LIST mode only checks immediate children, completely missing the full path hierarchy we need for nested folders. This can be a real time-killer, especially when you're trying to automate tasks or manage your vSphere environment efficiently.
To Reproduce
Alright, let's get our hands dirty and reproduce this bug. Follow these steps:
- 
Set up your vSphere environment: Make sure you have nested VM folders. For example, /Datacenter/vm/Folder1/Folder2/Folder3/Folder4.
- 
Set the datacenter context: Use govc datacenter.infoto set your datacenter context. This tellsgovcwhere to start looking for your folders.
- 
Try to access a subfolder using a relative path: $ govc folder.info Folder1/Folder2/Folder3/Folder4 govc: folder 'Folder1/Folder2/Folder3/Folder4' not foundSee? It can't find the folder. Super frustrating. 
- 
Try with an absolute path: $ govc folder.info /Datacenter/vm/Folder1/Folder2/Folder3/Folder4 Name: Folder4 Path: /Datacenter/vm/Folder1/Folder2/Folder3/Folder4Now it works! That's because absolute paths bypass the problematic LIST mode. 
- 
Listing children also fails with relative paths: $ govc ls Folder1 (no output) $ govc ls /Datacenter/vm/Folder1 /Datacenter/vm/Folder1/Folder2The first command gives you nothing, while the second correctly lists the child folder. 
Expected Behavior
Ideally, relative paths with subfolders should behave just like absolute paths. They should resolve relative to the datacenter's VM folder. So, Folder1/Folder2/Folder3/Folder4 should resolve to /Datacenter/vm/Folder1/Folder2/Folder3/Folder4 when the datacenter is properly set. The current behavior is inconsistent: single-level relative paths like Folder1 work fine, but multi-level ones like Folder1/Folder2 just fail. This inconsistency makes the system feel unreliable and unpredictable.
Affected Version
This bug affects:
- govmomiversion: main branch (commit 9d1d96b3, 2025-10-31)
- Cluster API Provider vSphere (CAPV), which relies on govmomiFinder.
So, if you're using these versions, you're likely to run into this issue.
Screenshots/Debug Output
The root cause lies in find/finder.go, specifically around line 106. The code checks for a / to determine if something is a path:
isPath := strings.Contains(arg, "/")
When isPath is true, the code switches to LIST mode (lines 147-151), which then calls Lister.ListFolder(). This method uses a TraversalSpec with only "childEntity" (see list/lister.go, lines 165-169). This setup only retrieves immediate children and doesn't recursively traverse the folder hierarchy. This is why it fails to find nested folders using relative paths. The key takeaway is that the presence of / triggers a non-recursive listing, which is not what we want for relative paths.
Additional Context
Let's talk about the impact and potential solutions.
Impact:
- Counter-intuitive UX: Adding specificity to a path causes it to fail. Users expect that providing a more detailed path should increase the chances of finding the resource, but in this case, it does the opposite.
- CAPV users must use absolute paths: For nested folders, CAPV users are forced to use absolute paths with the datacenter prefix. This reduces portability and makes configurations more verbose.
- Workarounds are poorly documented: While there are workarounds like using absolute paths or the ...wildcard, these aren't well-documented, leaving users to discover them through trial and error.
Workarounds:
- Use absolute paths: /Datacenter/vm/Folder1/Folder2/Folder3/Folder4
- Use ...wildcard: For recursive search, you can use the...wildcard (when applicable). This tellsgovcto search recursively, but it's not always the most efficient solution.
Possible Solutions:
- Make LIST mode recursively traverse multi-segment paths: Modify LIST mode to handle multi-segment paths by recursively traversing the folder hierarchy. This would require changing the TraversalSpecto include recursive traversal.
- Only use LIST mode for absolute paths: Limit LIST mode to only be used for paths that start with /. This would ensure that relative paths are always resolved using the full hierarchy traversal, which is the more intuitive behavior.
Conclusion
This bug in govmomi and the Finder API can be a real pain, especially when dealing with nested folders and relative paths. By understanding the root cause and the available workarounds, you can mitigate its impact on your workflow. Hopefully, the govmomi team will implement one of the proposed solutions to make relative paths more reliable and intuitive in the future. Keep an eye on future releases and release notes for updates on this issue. Happy scripting, everyone!