Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 BUG: Compiler crashes with out of bound errors when an imported component is top level component. #807

Closed
thisizkp opened this issue Jun 19, 2023 · 6 comments
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@thisizkp
Copy link

thisizkp commented Jun 19, 2023

What version of @astrojs/compiler are you using?

1.5.1

What package manager are you using?

pnpm

What operating system are you using?

Mac

Describe the Bug

Compiler crashes when importing a component. Happens when the imported component is the top level component.

No bug here as the imported component is not top level.

---
import Test from '../components/Test.astro';
---
<h1>This will crash without me</h1>
<Test/>

Bug here as the imported component is top level

---
import Test from '../components/Test.astro';
---
<Test/>

Stacktrace

error   runtime error: index out of range [0] with length 0
  Hint:
    This is almost always a problem with the Astro compiler, not your code. Please open an issue at https://astro.build/issues/compiler.
  File:
    /home/projects/github-jnwbjw/src/pages/bug.astro
  Stacktrace:
goroutine 45 [running]:
runtime/debug.Stack()
        runtime/debug/stack.go:24 +0x6
github.com/withastro/compiler/internal_wasm/utils.ErrorToJSError(0x6e9200, {0xb59b18, 0x6ca7c8})
        github.com/withastro/compiler/internal_wasm/utils/utils.go:68 +0x2
main.Transform.func1.1.1.1()
        ./astro-wasm.go:284 +0x5
panic({0x2dac0, 0x6ca7c8})
        runtime/panic.go:884 +0x29
github.com/withastro/compiler/internal.inBodyIM(0x6e9320)
        github.com/withastro/compiler/internal/parser.go:1157 +0x658
github.com/withastro/compiler/internal.(*parser).parseCurrentToken(0x6e9320)
        github.com/withastro/compiler/internal/parser.go:2966 +0x13
github.com/withastro/compiler/internal.(*parser).parse(0x6e9320)
        github.com/withastro/compiler/internal/parser.go:2993 +0x3
github.com/withastro/compiler/internal.ParseWithOptions({0x7aad8, 0x42dda0}, {0x439b80, 0x1, 0x1})
        github.com/withastro/compiler/internal/parser.go:3057 +0x1c
main.Transform.func1.1.1()
        ./astro-wasm.go:289 +0xc
created by main.Transform.func1.1
        ./astro-wasm.go:280 +0x14

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-jnwbjw?file=src%2Fpages%2Findex.astro

@thisizkp thisizkp changed the title 馃悰 BUG: 馃悰 BUG: Compiler crashes with out of bound errors when an imported component is top level component. Jun 19, 2023
@ematipico
Copy link
Member

You should use a <Fragment> in this case.

@natemoo-re natemoo-re self-assigned this Jun 19, 2023
@thisizkp
Copy link
Author

Okay. Wasn't aware of <Fragment>. But I still think it should give us proper error message of what's happening instead of crashing.

@natemoo-re
Copy link
Member

I wasn't able to reproduce using the example in this issue, but I do see the problem in the reproduction.

If you move the component inside of the html element things should work. Is there a use case for having a component outside of html that I'm missing? Would you expect the component to be moved into the head element, the body, or somewhere else?

---
import Test from '../components/Test.astro';
---
<Test/>

<html lang="en">
	<head>
		<meta charset="utf-8" />
		<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
		<meta name="viewport" content="width=device-width" />
		<meta name="generator" content={Astro.generator} />
		<title>Astro</title>
	</head>
	<body>
		<h1>Astro</h1>
	</body>
</html>

@thisizkp
Copy link
Author

@natemoo-re Yeah, moving the component within the <html> fixed it for me too. But that solution is very specific to this particular example.

This could have happened with components too. May be not so priority as can't think of many common use cases, but still a good error message would help.

@natemoo-re natemoo-re added the - P3: minor bug An edge case that only affects very specific usage (priority) label Jun 27, 2023
@MoustaphaDev
Copy link
Member

MoustaphaDev commented Dec 20, 2023

Hey @thisizkp 馃憢 This was fixed in #891. Note that another issue arises when there is any element before the html tag. See #913

@davemagro
Copy link

came across this issue as well.

Search keywords:
panic: runtime error: index out of range [0] with length 0
Error: TypeError: Cannot read properties of undefined (reading 'map').
panic: runtime error: index out of range [0] with length 0

for me, the issue was that I had html comment tags in my .astro files. Example:
<!---->

I just had to get rid of this and npx astro check the error went away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

5 participants