-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix prepare
race condition in workspaces
#2069
Fix prepare
race condition in workspaces
#2069
Conversation
…repare-race-condition
There is a related problem with a single fixed location of sql-data.json per crate. A crate could have multiple targets. Let's imagine a use case when the But there are also multiple targets that contain integration tests with own test-only sql queries. If a developer prefers offline mode than an IDE with rust-analyzer shows lint warnings for queries in integration test modules because the generated for the |
prepare
race condition in workspaces
Edit: this comment is irrelevant now, see the workaround in #2069 (comment) . Is that issue only with test-gated queries or are there problems with other cases too? I imagine for We could separate the files for test targets based on E.g. output Is there a more elegant solution by appending a Maybe a simpler solution for that problem would be for It might be better to have a followup PR or issue either way, since it depends on the approach? |
Since I've definitely felt the pain of not being able to have multiple targets in |
The better approach may be to revive #1770. It's still something we want, it just didn't make it into 0.6.0. |
@cycraig , regarding |
@DXist I think generating Edit: opened #2071 so multiple targets can work with or without |
@cycraig, indeed. I added Thank you! |
This PR will be obviated by #1770 but I overestimated from the discussions how complex this solution was so I'm happy to accept it for the time being. |
Description of change
Small PR to change the target directory for offline query caches from
target/sqlx
totarget/sqlx/<CRATE_NAME>
, which fixes the behaviour seen in #2049. CC @djcThe problem is essentially a race condition: there is a period between
cargo sqlx prepare
deleting thetarget/sqlx
directory and reading the queries from it, during which time another process could build/check one of the other crates in the workspace and populate it.BEFORE, queries for all crates in a workspace would share the
target/sqlx
directory:AFTER this PR, queries will be placed under their respective crate names:
You can validate the current and corrected behaviour in the following example workspace https://github.com/cycraig/sqlx_prepare_bug , which can (sometimes) reproduce the non-deterministic behaviour of pulling different queries into
sqlx-data.json
when multiple crates are being compiled duringcargo sqlx prepare
usingsqlx 0.6.1
(it doesn't happen often for me, depends a lot on timing).Note that crate names should be fine to create directories from,
cargo
does it all the time in thetarget
directory. I'm not aware of any platforms where this would be an issue.I believeEdit: failed to account forresolve.root
returned bycargo metadata
should always be populated using any recentcargo
version; the convenience.root_package()
function I used has been around for two years at least but I did have to update the test fixture to include it (wasnull
). Only theprepare
command usesMetadata
as far as I know, and it requires a non-virtual manifest, so it would error anyway ifresolve
is missing, e.g. when run in workspace roots.cargo sqlx prepare --merged
initially, updated PR.Edit: I kept the behaviour of
prepare
nuking the entiretarget/sqlx
directory just in case.Deleting only the relevant sub-crate directory would only be a minor optimisation and it's unclear to me what else it might affect.The entire directory should be deleted forcargo sqlx prepare --merged
anyway.Edit2: one alternative to this approach might be to pass an environment variable to the
cargo check
invocation incargo sqlx prepare
to use a different sub-directory during query caching, but I don't see any advantage to that.Related Issue
Resolves #2049.
Type of change
Not sure if this qualifies as a breaking change, does anyone depend on the location of the query caches in
target/sqlx
? Otherwise it's just a bug fix.How the change has been tested
cd sqlx-cli && cargo test
and manually running the example in https://github.com/cycraig/sqlx_prepare_bug