language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Svelte Monorepo auto import broken in svelte files.

Open Autumnlight02 opened this issue 9 months ago • 17 comments

Describe the bug

Basically svelte only import ts and svelte functions once on vscode load and you have to reload vscode to get new imports or changed imports (e.g. you modify types of a function and you need to reload so it's reflected) This neither works with build or no build. No build has an additional issue that you can auto import in ts files, not in svelte files. and that if you import it once in a ts file then you can import it via relative path.

Reproduction

Made a custom repo https://github.com/Autumnlight02/monorepo-issues

Expected behaviour

It just imports them simila rto how ts works

System Info

System:
    OS: Linux 6.12 Fedora Linux 41 (Toolbx Container Image)
    CPU: (16) x64 AMD Ryzen 7 5700X 8-Core Processor
    Memory: 96.57 GB / 125.71 GB
    Container: Yes
    Shell: 5.2.32 - /bin/bash
  Binaries:
    Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
    pnpm: 10.5.2 - ~/.nvm/versions/node/v22.14.0/bin/pnpm
    bun: 1.2.4 - ~/.bun/bin/bun

vscodium: 1.97.2 Extensions: prettier, pretty typescript errors, svetle for vscode, grammarly, indent-rainbow, vue - official, symbols (/ icon themes), mdx support

Which package is the issue about?

No response

Additional Information, eg. Screenshots

Either Svelte for vscode or general language server issues

Autumnlight02 avatar Mar 07 '25 13:03 Autumnlight02

Did you open both projects in separate vscode windows? We currently only watch files matching the include pattern in your tsconfig.json and the directory where you open the editor. If I open one editor, I see the same behaviour in ts/svelte files. If you have imported the file at least once, the update will be reflected. If you haven't imported the file, both ts/svelte don't suggest auto-import from the file.

jasonlyu123 avatar Mar 07 '25 14:03 jasonlyu123

No, I had it open either at broken_svelte-no-import or working_ts-monorepo If my config is the issue could you help me to resolve this? Also the way how I can import any ts file in project ts-export. Would be the same possible with svelte as well (w autoimport?) Ive tried now for months to fix this but came so far to no conclusion

Autumnlight02 avatar Mar 07 '25 23:03 Autumnlight02

I am unsure I understand the problem since I saw the same behaviour in broken_svelte-no-import and working_ts-monorepo. Can you explain in more detail what exactly the difference is? I did see the comment in the files, but a lot of them don't work in the ts file or even working_ts-monorepo either, so I don't know what exactly the difference is.

jasonlyu123 avatar Mar 09 '25 05:03 jasonlyu123

Ah. I know why I can't reproduce the difference. I disabled the typescript.preferences.includePackageJsonAutoImports config probably to test out its behaviour when investigating another issue. The problem has something to do with this package.json auto import. We were using a port of TypeScript's package.json auto import, but TypeScript added a new feature to collect auto-import from the package.json wildcard entry. The port doesn't have this feature yet.

Were you also having problems with existing files in the package not being updated? I am not sure what this comment meant.

//Try to add a new file under /ts-export/src/myFile.ts and export it and import it here without reloading

jasonlyu123 avatar Mar 10 '25 06:03 jasonlyu123

Yeah basically lets say I apply that in package export. I have an issue that when I want to import the file it sometimes is not hot reloaded by ts server, it's flaky.

But if you generally know of any other way on how to do the following I would be grateful: Basically have 2 Repos. In Repo 1 add svelte files, In repo 2 import them without build or restarting vscode

Autumnlight02 avatar Mar 10 '25 23:03 Autumnlight02

Would you like me to record a video of what's happening vs expected?

Autumnlight02 avatar Mar 10 '25 23:03 Autumnlight02

Yeah. Please. There seem to be two problems here, but I don't understand what the second one is. I only understand the problem with the never-imported module, but it shouldn't work even if you restart the editor.

Also, does go-to-definition on the import for the shared libary take you to the source file? Or a file path with node_modules in it?

jasonlyu123 avatar Mar 10 '25 23:03 jasonlyu123

@jasonlyu123 sorry for the delay, was sick in bed.

Working ts example:

https://github.com/user-attachments/assets/a510c99c-f8d4-4c18-865d-bf178e3f5f7b

Broken svelte example:

https://github.com/user-attachments/assets/e7f1e85f-fc9e-4b9d-8c6e-8fcf6acbbc72

My end goal is that I can create a file in repo 1 and just import it in repo 2 without having to build anything / restart vscode (with import support)

Autumnlight02 avatar Mar 13 '25 10:03 Autumnlight02

Ah, and regarding definitions, it appears as if it refferences it directly (the file)

Image

https://github.com/user-attachments/assets/53427069-38d2-4b96-b70f-0a11e1c56800

Another thing is, that I would like to have the option to not only import ts files, but svelte files as well. (basically that the ts-exports repo has svelte components I can import in my svelte vite / svelte kit app

Autumnlight02 avatar Mar 13 '25 10:03 Autumnlight02

I understand this part. What I want to know is this

you modify types of a function and you need to reload so it's reflected

jasonlyu123 avatar Mar 13 '25 13:03 jasonlyu123

Oh, basically when I tried in a different repo a version with build and a single file as exports, it basically only scanned what the file exports on vscode load, and then let's say I rename a function, cahnge the output from number to boolean and rebuild the package, vscode intellense still would see the old package instead of the new one.

Autumnlight02 avatar Mar 13 '25 16:03 Autumnlight02

@jasonlyu123 Do you happen to know of a different way on archieving what I want?

Autumnlight02 avatar Mar 28 '25 08:03 Autumnlight02

One workaround is to add the share package to the include of the tsconfig.app.json of your svelte app and add an alias to the paths that points to the shared package.

{
  "compilerOptions": {
    ..
   "paths": {
      "ts-export/*": ["../ts-export/*"]
    }
  }
  "include": [
      ...
      "../ts-export/src/**/*.ts"
   ]
}

jasonlyu123 avatar Mar 28 '25 08:03 jasonlyu123

@jasonlyu123 Sadly that does not with svelte components cross repos. Example. copy the svelte vite folder, call it something like 2 and try ts files. They Work. App.svelte also seems to work. But I added as a Test a "Gaga.svelte" with a div element as content. And I fialed to import that in svelte-vite even after the ts config + adding it to package.json

Autumnlight02 avatar Mar 30 '25 11:03 Autumnlight02

It only shows up in "svelte-vite" if I import my new Gaga.svelte into App.svelte in "svelte-vite-2"

Autumnlight02 avatar Mar 30 '25 11:03 Autumnlight02

The include config I listed is just an example. If you want Svelte and js files, you can add the corresponding pattern to the include config. Have you added svelte files to the include config? I tried, and it works with new files.

jasonlyu123 avatar Mar 30 '25 12:03 jasonlyu123

Oh, yeah that worked. Thank you <3

Autumnlight02 avatar Apr 01 '25 05:04 Autumnlight02