🐛 BUG: losing intellisense on typescript imports inside .astro files until editor reload
Describe the Bug
MacOS VS Code extension: 2.14.2 Npm: 10.8.3 Node: 22.9.0 VS Code: 1.93.1
Intelisense in frontmatter imports doesn't seem to be working as expected when dealing with new typescript files. Sometimes I can get it to work for the first instance of a file, but then it fails to import any other files. Either way, reloading the editor after creating any new files fixes the issue.
Steps to Reproduce
npm create astro@latest(4.15.9 as of today)- open the project in vscode
- create
anything.tswith the contentsexport const anything = "anything"; - create
another.tswith the contentsexport const another = "another"; - open
index.astroand try to import the exported const. - while typing
import anyt...I would expect the import to be listed at the top of the autocomplete list, however it does not appear there. - reload the vscode window and the imports now appear on the autocomplete list
Link to Minimal Reproducible Example
https://github.com/lee-arnold/astro-issue-example
Everything works as expected for me when following your instructions:
https://github.com/user-attachments/assets/abb57f4e-2d48-46ac-b2f5-28f36d43f34e
I'm not sure how to debug this further.
Hey, thanks for the response. Can I ask what OS and versions you're using? I haven't had a chance to test any others yet.
This is on macOS, 2.14.2 of the Astro extension, VS Code 1.93.1.
I am having this same problem, but on Ubuntu 22.04 with VS Code 1.92.1
I've noticed with further use that it also fails to recognise new exported functions in files that already existed, again resolved by restarting the language server.
My dupe issue above was about autocompletions on components like <MyComp.. but I guess it boils down to same issue. Running VSCode in Windows 11, under WSL2 (debian). Version is 1.96.0
Having the same issue on MacOS with Astro extension v2.15.4.
Any kind of file operation (new/renamed file or new/renamed exports) is not picked up unless I use "restart extension host" or restart VSCode. Until then, imports of these new files/exports are underlined as not found, and are not reported by intellisense either. This also affects Astro components (.astro files), new components can't be imported unless the extension is restarted.
It's a bit like if a file watcher was stuck and never updating after the initial scan.
~~UPDATE: Seems to be resolved for my after the latest VSCode update (1.96.2).~~ UPDATE 2 (a few days later): The issue reappeared.
Just a note that I'm on VSCode 1.96.2 and it still doesn't work. What I see is that any export (either on a TS file or Astro) gets picked up immediately on a TS file, but only gets picked up on an Astro file after reloading.
This is probably my biggest issue with Astro at the date, as I have to reload VSCode a dozen times a day due to this.
I experienced the same issue, but it hasn't occurred yet in Code Insiders (1.96). There could be other factors, such as conflicting extensions, but I haven't been able to reproduce the bug so far.
Without this issue I would not know a restart fixes it :)
Happens to me every time on Debian 12 + NeoVim 0.10.2 + astro-ls 2.15.4.
Can confirm this issue in an empty vscode profile (no extensions except astro-build.astro-vscode installed). This also fails in a code-insiders build in the same way.
Importing a .ts or .astro file fails from inside a .astro file (until reloading the extension host or window) but both work without any reload when importing from inside a .ts file.
The exporting file can either be a new file or a new identifier from an existing file (even one that's imported into the current .astro module). Interestingly, removing an identifier from the exporting module doesn't remove it from the auto-import list, but it fails to import when you tab complete it.
Ubuntu: 22.04.1
Astro vscode extension: 2.15.4
astro@latest: 5.2.5
vscode: 1.96.4
vscode insiders: 1.98.0-insider
(both vscode editions are installed using snap if that makes any difference)
This also happens with Ubuntu 24.04 + NeoVim 0.10.3 + astro-language-server 2.15.4.
For those with NeoVim, a solution that worked for me was this:
-- Add this to the astro lsp config:
on_attach = function (client, bufnr)
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = { "*.js", "*.ts" },
group = vim.api.nvim_create_augroup("astro_ondidchangetsorjsfile",
{ clear = true }),
callback = function (ctx)
client.notify("workspace/didChangeWatchedFiles", {
changes = {
{
uri = ctx.match,
type = 2, -- 1 = Created, 2 = Changed, 3 = Deleted
}
}
})
end
})
end
VS Code and the language server have received many updates to their watching system with time and there hasn't been recent reports of this (that I know of).
For other editors, this language server does not have any sort of fallback watching system, it expects for didChangeWatchedFiles to come through. As far as I know, the server properly asks to watch files, as it works in tests and in VS Code, so I'm not sure why it'd need to be configured manually.
Additionally, this issue is often mistaken for another issue that auto-imports do not always work in certain situations due to compiler bugs (that are reported on the compiler repo).
If this happens again, and you can prove that it is in fact a watching issue, please open another issue!