-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
I have found these related issues/pull requests
I have not found any existing issues for this bug. The closest I could find was #3255, which is about a different bug in to_url_lossy()
.
Description
PgConnectOptions
does not always set socket
when a socket is used. Since PgConnectOptions.to_url_lossy
only percent-encodes the socket (and not the host), this can cause it to panic:
BUG: generated un-parseable URL: EmptyHost
An easy way to reproduce this is to parse a URL with the default host, user, etc., such as postgres:///foo
. No host is specified in the URL, so the default one is used, which in my case is the socket at /var/run/postgresql
:
let opts: PgConnectOptions = "postgresql:///foo".parse().unwrap();
assert_eq!(opts.get_host(), "/var/run/postgresql");
assert_eq!(opts.get_socket(), None);
dbg!(opts.to_url_lossy()); // panic: BUG: generated un-parseable URL
I believe this is a bug, as passing a URL without a host seems to be supported by libpq and thus a lot of postgresql software (sqlx seemingly supports it too; connecting works fine even though PgConnectOptions.to_url_lossy
panics). While this may seem like an edge-case, the ability to not specify a host in the URL is very convenient in my experience.
I'm not sure what the best way to fix this is. One option would be to percent-encode the host, while another would be to make sure that socket
is always set when a socket is used, similar to the check in PgConnectOptions.parse_from_url
.
Reproduction steps
The example I mentioned above should be enough to reproduce the issue, as long as one of the candidate sockets used by PgConnectOptions.default_host
is available:
let opts: PgConnectOptions = "postgresql:///foo".parse().unwrap();
assert_eq!(opts.get_host(), "/var/run/postgresql");
assert_eq!(opts.get_socket(), None);
dbg!(opts.to_url_lossy()); // panic: BUG: generated un-parseable URL
SQLx version
0.8.6
Enabled SQLx features
postgres, runtime-tokio
Database server and version
PostgreSQL 17.5
Operating system
Linux 6.15.8 - NixOS 25.11.20250806.c2ae88e (Xantusia)
Rust version
rustc 1.88.0 (6b00bc388 2025-06-23)