get-convex / convex-svelte

https://convex-svelte.vercel.app
Apache License 2.0
21 stars 5 forks source link

Svelte Language Server Not Picking Up New Schema Tables #8

Open btab opened 1 month ago

btab commented 1 month ago

Ported over from a discussion on the Convex Community Discord.

  1. I create a new model file goats.ts containing...
    
    import { query } from "./_generated/server";

export const get = query({ args: {}, handler: async (ctx) => { return await ctx.db.query("goats").collect(); }, });

This initially gets a red squiggle under "goats" because I guess it's not one of the table names in my schema file.

2. Then I add the schema for that table...

import { defineSchema, defineTable } from "convex/server"; import { v } from "convex/values";

export default defineSchema({ [snip...] goats: defineTable({ name: v.string(), }), [snip...] });


And if I go back to `goats.ts`, the red squiggle has disappeared. So VS Code now understands the set of literal table names correctly.

3. Then I head over to my test svelte route and start typing `api.` and it completes with prior table names but doesn't offer `goats`.

4. If I then restart the Svelte language server, the autocomplete now works as expected. So it's something about the build / transpilation step in Svelte ...maybe?

Note I am runnning both `convex dev` and `npm dev` during this scenario so both convex and Svelte are doing their updating magic.
btab commented 1 month ago

Based on a suggestion from @thomasballinger, I tried playing around with extra saves on the various generated files to see whether one such save would nudge the Svelte server into catching up. The good news is that re-saving / touching api.d.ts seems to do so (the added table springs to life in the autocomplete). There might be something about the way that convex is saving that file that is somehow defeating Svelte's file watching - maybe it's being done as a write-then-rename?

So this means that this problem should exist irrespective of whether schemas are involved right? That generated file just comes from the set of api modules in the convex source directory, if I understand it correctly?

thomasballinger commented 1 month ago

Ah great find! To be clear, of 1 viewing the file in VSCode, 2) saving the file from VSCode, or 3) touching it from the terminal it's 2 and 3 that both fix the problem, making svelte types catch up?

Yep, api.d.ts contains a list of .ts files in the convex/ directory , but doesn't change when the contents of those files change. Without Svelte involved, adding new tables or columns to convex/schema.ts or new export convex functions to other files in the convex/ folder updates types in the browser and autocomplete on things like api.filename.functionName immediately.

btab commented 1 month ago

Yes indeed - anything that would bump the mtime on that generated api TS definition file does the trick.

I also have a secondary (rather weak) signal that I haven't had a chance to investigate. When rapidly building up api files in the convex dir, I found myself quite naturally copying whole existing files (specifically by selecting the file in VS CODE and then copy-pasting it) ... and then modifying the new file accordingly. So foods.ts would be joined by foods copy.ts, which I'd then rename and start hacking on. During this process convex dev is running the whole time.

I have had to force reload the windows a couple of times under these circumstances to get the right code completion even in scripts that have nothing to do with Svelte.

I'm mentioning all this here in case its another mini-clue that the generated files are being instantiated in a somehow LSP-unfriendly way. This problem is proving difficult to reproduce though so I'm not confident in the signal yet.