Skip to content

cargo sqlx prepare only works after cargo clean #788

Closed
@colmanhumphrey

Description

@colmanhumphrey

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!

Activity

Nehliin

Nehliin commented on Nov 14, 2020

@Nehliin

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

hollg commented on Nov 14, 2020

@hollg

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

colmanhumphrey commented on Nov 18, 2020

@colmanhumphrey
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

Nehliin commented on Nov 21, 2020

@Nehliin

@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

TatriX commented on Feb 4, 2021

@TatriX

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

jplatte commented on Feb 4, 2021

@jplatte
Contributor

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

spigaz

spigaz commented on Apr 18, 2021

@spigaz

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

abonander

abonander commented on Apr 28, 2021

@abonander
Collaborator

Closing as duplicate of #663

oessaid

oessaid commented on Oct 1, 2021

@oessaid

@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

matschaffer-roblox commented on Nov 11, 2022

@matschaffer-roblox

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

DanielJoyce commented on Nov 22, 2022

@DanielJoyce

Yes. Updating cli fixed it for me.

morukele

morukele commented on Jan 9, 2023

@morukele

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

s0lst1ce

s0lst1ce commented on Jan 28, 2023

@s0lst1ce

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

7 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @TatriX@spigaz@DanielJoyce@jplatte@abonander

        Issue actions

          `cargo sqlx prepare` only works after `cargo clean` · Issue #788 · launchbadge/sqlx