-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
TypeScript Error: No element indexed by 0 #424
Comments
I can reproduce the issue in TS mode, but not the one in JS mode - although I'm not testing this on Windows. I'll try that next. |
Yep, I can also see the issue in JS mode when running everything in Windows (and the TS one as well). |
Looks like two separate issues — from the stack trace, the TS thing looks like it might be a bug in Vite's sourcemap handling |
Switching this issue to the TS-specific one. I'll open a separate issue for the JS one later. I dug a bit into this by pointing When it attempts to handle the following: const oldMap = {
version: 3,
file: null,
sources: [ null ],
sourcesContent: [ null ],
names: [],
mappings: 'AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;AAC3D;AACvD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,0CAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/E,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC,CAAC,CAAC;AACH;AACA,8BAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC'
};
const newMap = {
version: 3,
mappings: ';;;;;;;',
names: [],
sources: [],
sourcesContent: []
}; Unfortunately, that means the issue is actually deeper in I am unsure how to produce a minimal reproduction to submit as an issue to |
Okay, I managed to track down the source of the issue: it dies on empty source files (in this case, our empty |
I've gotten some progress on this issue. Vite's current code does not handle blank sourcemaps at all. Problem is, when I tried to look at prior art:
At the moment I am able to make Vite + Svelte generate the unmapped generated JavaScript component. Sourcemaps are always properly generated with <!-- this works -->
Hello {'world'}
<!-- this works -->
Hello world
{''}
<!-- this works -->
<script>
let blah = true;
</script>
Hello world
<!-- this works -->
{#each [1, 2, 3] as num}
{num}
{/each} @dominikg Are static file sourcemaps potentially something that can be supported at the @Rich-Harris Any potential insight here? I'd love to get sourcemaps for static Svelte files, but the empty sourcemaps currently being sent to Vite seems to indicate that this might need support from either the |
You tested Vanilla Svelte with TypeScript. What about Vanilla Svelte without TypeScript? I think that would narrow down whether it's Svelte core or |
Oh, not sure why I didn't think of that. Vanilla Svelte also does the same thing, so I guess it's part of |
Thanks for your PR over in the Vite repo! vitejs/vite#2441 |
fwiw vite-plugin-svelte uses the logic from rollup-plugin-svelte for sourcemaps: https://github.com/svitejs/svite/blob/c5cb875a007ad5fd4dfff1a33c9aaba61eab291c/packages/vite-plugin-svelte/src/utils/compile.ts#L36 and i ran into this gem on vites css transform last night: https://github.com/vitejs/vite/blob/5ec13d8b3fe1632f793c7ad22b21c43a13d71141/packages/vite/src/node/plugins/css.ts#L197 |
I see now in @dominikg's links that let preprocessed
if (options.preprocess) {
preprocessed = await preprocess(code, options.preprocess, { filename })
if (preprocessed.dependencies)
dependencies.push(...preprocessed.dependencies)
if (preprocessed.map) finalCompilerOptions.sourcemap = preprocessed.map
} So I guess I'm taking a look at either |
Okay, I can in fact replicate this on a JS+CSS starter kit template by just adding const sveltePreprocess = require('svelte-preprocess');
module.exports = {
preprocess: sveltePreprocess(),
// ... I also tried building a local copy of let preprocessed
if (options.preprocess) {
preprocessed = await preprocess(code, options.preprocess, { filename })
if (preprocessed.dependencies)
dependencies.push(...preprocessed.dependencies)
// if (preprocessed.map) finalCompilerOptions.sourcemap = preprocessed.map
} Which successfully shows the unprocessed map. I also tried to change I'm starting to delve into |
FWIW, this bug showed up for me when I chose |
fyi there is an option you need to enable in Vite config and its not released, but this could help if the issue is within svelte-preprocess |
I don't think we should side step svelte-preprocess. I'm also quite confused how this happens since when using roll-up you get the correct source maps which you can use to debug your Svelte-with-Typescript code in the Browser. |
@dummdidumm to be clear, I'm not advocating sidestepping |
I think @dummdidumm was talking about my last comment regarding the vite based preprocessor. We should discuss this elsewhere, i was merely offering another option to narrow down the issue. It's very possible the way how vite-plugin-svelte reads the svelte config or how the preprocessors+map are applied in compile is at fault here. (though i took the latter bits from rollup-plugin-svelte) |
I dug into this at well and I'm suspecting the Svelte compiler now and how it handles incoming I noticed that when you run a preprocessor before compiling
EDIT: The reason is the empty |
Dug deeper. The problem is that inside the function combine_sourcemaps mappings go in, and then one mapping comes out without if (!map.sources.length) {
map.sources = [filename];
} It works. This could be a quick fix while we investigate why this even happens. Either the library which we use to combine source mappings is at fault, or we give it faulty input. |
Hm, should I update my Vite PR then? It was largely based on the existing |
I haven't been following this closely, but do we need that Vite PR at all if we change this on the Svelte side? Or do you think that's something their API should be able to handle anyway? |
Not sure. I think the PR over at Vite makes sense either way. To me it looks like the remapping library trips up when the first source map is empty. So if I want to merge this
It produces Digging into the source code of that lib, I think that in this function the combination is done, and the line where I created ampproject/remapping#116 , let's see what comes back. In the meantime we might add the workaround I posted earlier. |
I guess the question on the Svelte side would be whether Svelte should be returning empty source maps when there's no |
When source maps are combined and the leading map is empty, sources is not set. Add the filename to the empty array in this case. Related sveltejs/kit#424
Postmortem analysis: I think the reason why both the change to Svelte core (adding sources) and the Vite PR solve this on their own is that
Ultimately I does not matter what comes out in the end because those source maps are empty either way, so it was just a matter of preventing a crash along the way |
Yeah, I already figured that to fix the crash, all you'd need to do is fix it at one end. That's why later on I focused on "disappearing sourcemaps for non-dynamic files", which I guess should be a separate issue. |
This is caused by Vite's source map handling on
.svelte
files that are not dynamic files.As a temporary workaround, make sure to have
src/routes/$layout.svelte
with at least some "dynamic" content, e.g.Describe the bug
As long as
svelte-preprocess
is used insvelte.config.cjs
, attempting to run dev mode results in internal errors.Logs
With TypeScript, the page is a plaintext error:
To Reproduce
npm init svelte@next # enable typescript npm install npm run dev
Expected behavior
No errors out of the box.
Information about your SvelteKit Installation:
Severity
Unusable dev mode.
The text was updated successfully, but these errors were encountered: