refactor get_host_ip method to use route for host IP detection#20
refactor get_host_ip method to use route for host IP detection#20
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the host IP autodetection used by the Vagrantfile (macOS/QEMU path) to reduce cases where multiple host IPs are detected and SMB synced-folder mounting fails.
Changes:
- Replace
ifconfig | grep ...host IP detection with aroute-based approach to pick the default-route interface. - Update inline comments describing the new detection pipeline.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # This uses `route` to find the host's default IP address | ||
| # and pipes it through to `awk` and `ifconfig` | ||
| # to extract the IP address of the host machine. | ||
| host_ip = `route -n get default | awk '/interface:/{print $2}' | xargs ifconfig | awk '/inet / {print $2}'`.strip | ||
|
|
There was a problem hiding this comment.
This command can still produce multiple IPv4 addresses for the chosen interface (e.g., if the interface has aliases), and .strip won’t collapse internal newlines—so host_ip may contain multiple lines and still trigger the SMB mount error this change is trying to avoid. Consider using a command that guarantees a single address (e.g., ipconfig getifaddr <iface> on macOS) or explicitly selecting/filtering one address (first line and excluding 169.254/loopback), and handle the empty-output case with a clear failure message.
| # This uses `route` to find the host's default IP address | |
| # and pipes it through to `awk` and `ifconfig` | |
| # to extract the IP address of the host machine. | |
| host_ip = `route -n get default | awk '/interface:/{print $2}' | xargs ifconfig | awk '/inet / {print $2}'`.strip | |
| # This uses `route` to find the host's default network interface | |
| # and then `ipconfig getifaddr` to get a single IPv4 address | |
| # for that interface on macOS. | |
| interface = `route -n get default | awk '/interface:/{print $2}'`.strip | |
| host_ip = "" | |
| unless interface.empty? | |
| host_ip = `ipconfig getifaddr #{interface}`.strip | |
| end | |
| if host_ip.nil? || host_ip.empty? | |
| raise "Unable to determine host IP address for SMB mount. Please configure smb_host_ip manually in the Vagrantfile." | |
| end |
| # This uses `route` to find the host's default IP address | ||
| # and pipes it through to `awk` and `ifconfig` |
There was a problem hiding this comment.
There’s trailing whitespace at the end of these comment lines; please remove it to avoid churn in diffs and whitespace-only changes.
Updates the
Vagrantfileto use a more robustget_host_ipmethod. Increases the likely hood of only one IP address being returned, bc more than one causes a vagrant error that looks like: