Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-missing-frontmatter-fence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Fixes a panic when parsing files with a closing frontmatter fence (---) but no opening fence. The compiler now returns a helpful diagnostic error instead of crashing.
1 change: 1 addition & 0 deletions internal/loc/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
ERROR_UNMATCHED_IMPORT DiagnosticCode = 1003
ERROR_UNSUPPORTED_SLOT_ATTRIBUTE DiagnosticCode = 1004
ERROR_UNTERMINATED_STRING DiagnosticCode = 1005
ERROR_MISSING_FRONTMATTER_FENCE DiagnosticCode = 1006
WARNING DiagnosticCode = 2000
WARNING_UNTERMINATED_HTML_COMMENT DiagnosticCode = 2001
WARNING_UNCLOSED_HTML_TAG DiagnosticCode = 2002
Expand Down
13 changes: 13 additions & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,19 @@ func copyAttributes(dst *Node, src Token) {
func inBodyIM(p *parser) bool {
switch p.tok.Type {
case FrontmatterFenceToken:
// If originalIM is already set, we have a closing fence without an opening one
if p.originalIM != nil {
p.handler.AppendError(&loc.ErrorWithRange{
Code: loc.ERROR_MISSING_FRONTMATTER_FENCE,
Text: "The closing frontmatter fence (---) is missing an opening fence",
Hint: "Add --- at the beginning of your file before any import statements or code",
Range: loc.Range{
Loc: p.tok.Loc,
Len: 3,
},
Comment on lines +1112 to +1115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have snapshot tests so that we can verify that the range is correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a way to do snapshots of errors, so instead I added an assertion to the test I created here: 9dbe356

})
return true
}
p.setOriginalIM()
p.im = frontmatterIM
return false
Expand Down
1 change: 1 addition & 0 deletions internal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,7 @@ frontmatter_loop:
case FrontmatterInitial:
z.fm = FrontmatterOpen
z.dashCount = 0
z.data.Start = z.raw.End - len("---")
z.data.End = z.raw.End
z.tt = FrontmatterFenceToken
z.openBraceIsExpressionStart = false
Expand Down
44 changes: 44 additions & 0 deletions packages/compiler/test/errors/missing-frontmatter-fence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

// Missing opening frontmatter fence - only has closing ---
const FIXTURE = `import BaseLayout from '@/layouts/BaseLayout.astro';
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');
---
<BaseLayout title="Crash Test">
<h1>{posts.length}</h1>
</BaseLayout>`;

let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE, {
filename: '/src/pages/checkthis.astro',
});
});

test('missing opening frontmatter fence reports error instead of panic', () => {
assert.ok(Array.isArray(result.diagnostics));
assert.is(result.diagnostics.length, 1);
assert.is(result.diagnostics[0].code, 1006);
assert.is(
result.diagnostics[0].text,
'The closing frontmatter fence (---) is missing an opening fence'
);
assert.is(
result.diagnostics[0].hint,
'Add --- at the beginning of your file before any import statements or code'
);
// Verify the error location points to the closing --- fence
const loc = result.diagnostics[0].location;
// The line number should point to the line containing ---
assert.is(FIXTURE.split('\n')[loc.line - 1], '---');
// The column and length should extract exactly the --- characters
assert.is(
FIXTURE.split('\n')[loc.line - 1].slice(loc.column - 1, loc.column - 1 + loc.length),
'---'
);
});

test.run();
Loading