sqlx
sqlx copied to clipboard
sqlx::query_file_as! macro hashing broken on Ubuntu 22.04.1 LTS
Bug Description
A clear and concise description of what the bug is. When a .sql file is saved from a linux machine, the sqlx::query_file_as! macro stops working and throws
'failed to find data for query...'
[..]
'note: this error originates in the macro $crate::sqlx_macros::expand_query
(in Nightly builds, run with -Z macro-backtrace for more info)'
line 122: https://github.com/launchbadge/sqlx/blob/d7277f41ebcaab1072da74efc163b40b47ce63be/sqlx-macros/src/query/data.rs
Minimal Reproduction
If I pull from a branch where a MacOs user has saved the file, it will compile just fine. As soon as I save the .sql file from a linux machine (Nano, Vim, Nvim, Code), even without making an actual change to the sql, the hashing will break and the code will no longer compile.
Info
- SQLx version: 0.6.1
- SQLx features enabled: ["offline", "postgres", "runtime-tokio-rustls", "chrono", "json", "uuid"]
- Database server and version: Postgres 13.4
- Operating system: Ubuntu 22.04.1 LTS
-
rustc --version
: 1.62.1
Likely a similar issue to #1494 and I described a workaround there: https://github.com/launchbadge/sqlx/issues/1494#issuecomment-943667930
@abonander I've seen that thread but we're not hashing using git. For me in place of the hash I would just see the contents of the sql itself in the error message.
Should I enable git hashing?
The ultimate problem is likely that something is changing the line endings in the .sql
file, it may be Git or the editor you're using.
@abonander I added a .gitattributes file following the instructions of #1494 but unfortunately doesn't seem to fix the issue. Do I need extra steps once the file is added? I've tried prepare (won't work because fails) I've tried git add --renormalize . (nothing happens)
Try using a tool to compare the line endings of the file on Mac vs Linux.
@abonander
The line endings are the same "i/lf w/lf " And they are actually the same on the problematic file for both the compiling branch and the non-compiling branch. As such is it possible that eol is not the issue?
Are they the exact same byte for byte?
@abonander
Using the od -c -b
Here is an example of a line in bytes. The pattern being \n.
0000000 w i t h t s a s ( \n s
167 151 164 150 040 164 163 040 141 163 040 050 012 040 040 163
0000020 e l e c t \n ( u n n e s
145 154 145 143 164 012 040 040 040 040 050 165 156 156 145 163
edit: also same on mac
@abonander
here is the error I get if I run it without the Offline mode:
error[E0308]: mismatched types
--> [...]
|
27 | let payment_details = sqlx::query_file_as!(
| ___________________________^
28 | | PaymentDetailResponse,
29 | | "sql/admin/payment_details.sql",
30 | | store_id,
31 | | p
32 | | )
| |_____^ expected enum `std::option::Option`, found struct `std::string::String`
|
= note: expected enum `std::option::Option<std::string::String>`
found struct `std::string::String`
= note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in `deliveries::_::_serde::__private::Some`
|
605| deliveries::_::_serde::__private::Some($crate::sqlx_macros::expand_query!(record = $out_struct, source_file = $path, args = [$($args)*]))
| +++++++++++++++++++++++++++++++++++++++ +
error[E0308]: mismatched types
--> [route...]
|
27 | let payment_details = sqlx::query_file_as!(
| ___________________________^
28 | | PaymentDetailResponse,
29 | | "sql/admin/payment_details.sql",
30 | | store_id,
31 | | p
32 | | )
| |_____^ expected enum `std::option::Option`, found `f64`
|
= note: expected enum `std::option::Option<f64>`
found type `f64`
= note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in `deliveries::_::_serde::__private::Some`
|
605| deliveries::_::_serde::__private::Some($crate::sqlx_macros::expand_query!(record = $out_struct, source_file = $path, args = [$($args)*]))
| +++++++++++++++++++++++++++++++++++++++
We ended up just removing the file queries and sticking with query_as().
I fixed this error by upgrading sqlx-cli
to 0.6.3 (through cargo install sqlx-cli
). Might be related to https://github.com/launchbadge/sqlx/issues/2101