Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo sqlx prepare only works after cargo clean #788

Closed
colmanhumphrey opened this issue Nov 3, 2020 · 16 comments
Closed

cargo sqlx prepare only works after cargo clean #788

colmanhumphrey opened this issue Nov 3, 2020 · 16 comments

Comments

@colmanhumphrey
Copy link

This may already be solved, as I'm running on version 0.4.0-beta.1.

Issue

I run cargo sqlx prepare -- --bin <bin name> and get a correctly specified sqlx-data.json file. However, if I run it again, I now get the "empty" file, only specifying what db I'm using:

{
  "db": "PostgreSQL"
}

And along with this, I get warning: no queries found; do you have the `offline` feature enabled, which seems off, since I do have "offline" enabled.

Indeed the same issue arises if I run cargo sqlx prepare --check -- --bin <bin name>

Workaround

If I run cargo clean before the commands, everything works fine. This is both in terms of rerunning the builder, and running --check.

What Else I've Tried

  • From Delete target/sqlx before invoking cargo in cargo sqlx prepare #722, I tried just manually deleting ./target/sqlx instead of full cargo clean but that didn't work - the folder is empty anyway.
  • I tried just deleting target/CACHEDIR.TAG, didn't work; same for target/.rustc_info.json
  • I trashed just ./target/debug and that worked... But this is rather similar to running cargo clean I guess
  • On quick search, didn't find a matching issue here or on Discord

Version

My sqlx setup:

[dependencies.sqlx]
version = "0.4.0-beta.1"
default-features = false
features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate", "offline" ]

Postgres: postgres (PostgreSQL) 13.0 (Debian 13.0-1.pgdg100+1) running on postgres Docker (image 817f2d3d51ec)

Thanks

For the great crate!

@Nehliin
Copy link

Nehliin commented Nov 14, 2020

I ran into the same issue on version 0.4.1 but my problem isn't fixed by running cargo clean or removing the target folder

@hollg
Copy link

hollg commented Nov 14, 2020

I have the same issue, also on 0.4.0-beta.1. I'm not entirely sure whether this is related, but I also get the message

`cargo sqlx prepare` needs to be rerun

When I run cargo sqlx prepare --check -- --bin --<bin_name> immediately after cargo sqlx prepare -- --bin --<bin_name>.

@colmanhumphrey
Copy link
Author

Ah @Nehliin do you mean it works the first time for you, then refuses to work? Or you only ever get an empty sqlx-data.json file?

Also, quick update on my end: the issue persists with sqlx0.4.1, and with sqlx-cli 0.2.0. Possible it's something on my end!

@Nehliin
Copy link

Nehliin commented Nov 21, 2020

@colmanhumphrey It generates an almost empty sqlx-data.json (only contains top level databasename) but rerunning it with the empty json solved it for me.

@TatriX
Copy link

TatriX commented Feb 4, 2021

In my case cargo can't see changes made to sql files (I use sqx::query_files_as!). I think this is the reason why cargo sqlx prepare generates empty file for me. In order to fix both cargo recompiling binary with proper sql files and generating non-empty json I need to touch src/lib.rs to make cargo see that it needs to recompile.

@jplatte
Copy link
Contributor

jplatte commented Feb 4, 2021

The query_file_as! issue is being tracked at #663. The fix should actually be relatively simple.

@spigaz
Copy link

spigaz commented Apr 18, 2021

The cargo clean workaround is still needed in version cargo-sqlx 0.5.1.

@abonander
Copy link
Collaborator

Closing as duplicate of #663

@oessaid
Copy link

oessaid commented Oct 1, 2021

@abonander: Please note that this issue is indeed a duplicate of #663 for the specialized case of query_as_file!, but more generally speaking a workaround is still necessary for the standard query! case as of 0.5.7.

Using cargo clean (as in the original comment) works but is obviously time-consuming.

My own workaround is to prefix both cargo sqlx prepare -- --bin <...> and cargo sqlx prepare --check -- --bin <...> with touch src/lib.rs. I haven't seen it mentioned above and it has been working for me so far.

The exact commands I use are:

## Generate sqlx metadata file
touch ./src/lib.rs && SQLX_OFFLINE=true cargo sqlx prepare -- --bin <my_bin_name>
## Check sqlx metadata file, fail if out of sync (CI)
touch ./src/lib.rs && SQLX_OFFLINE=true cargo sqlx prepare --check -- --bin <my_bin_name>

For context, I have this in my Cargo.toml:

[dependencies.sqlx]
version = "0.5.7"
default-features = false
features = [
  "runtime-actix-rustls",
  "macros",
  "postgres",
  "uuid",
  "chrono",
  "migrate",
  "offline"
]

Are there any plans to improve this experience or mention this as a caveat in the docs? Is there anything I could do that would be helpful?

Thanks (for looking into this but also for the awesome crate).

@matschaffer-roblox
Copy link

I ran into this today and I think it might have been caused by a mismatch between sqlx-cli (0.6.0) and sqlx (0.6.2). Re-running cargo install sqlx-cli seemed to get things working again.

@DanielJoyce
Copy link

Yes. Updating cli fixed it for me.

@morukele
Copy link

morukele commented Jan 9, 2023

I had the same issue and updating the CLI fixed it for me.

@s0lst1ce
Copy link

Same but only for a while. After doing it for a few crates in a workspace, it started failing again.

@zalper
Copy link

zalper commented Aug 6, 2023

I am using "query_as!" and have this issue.

cargo sqlx prepare -- --workspace --all-targets --all-features

gives :No queries found: until cargo clean is issued.

Info

  • SQLx version: 0.7.1
  • SQLx features enabled: [ "runtime-tokio-native-tls", "postgres", "chrono", "uuid" ] }
  • Database server and version: Postgres 15
  • Operating system: Debian GNU/Linux 11
  • rustc --version: 1.73.0-nightly (864bdf784 2023-07-25)

@vszakd
Copy link

vszakd commented Apr 1, 2024

Hello,
not sure if I'm doing something wrong, but with sqlx-cli 0.7.4 and sqlx 0.7.4 I receive "no queries found".
Even after cleaning, the problem persists.

The program builds and runs correctly.

@jdevries3133
Copy link

I found that I experienced this issue with sqlx version 0.6.3 and sqlx-cli version 0.6.0. To fix, I ensured that I installed the exact correct CLI version on my machine;

cargo install --version 0.6.3 sqlx-cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests