Skip to content

importing markdown and referencing import.meta.env can crash the renderer if done in a certain order #1056

@kaytwo

Description

@kaytwo

Astro Info


Astro                    v5.1.2
Node                     v18.20.3
System                   Linux (x64)
Package Manager          unknown
Output                   static
Adapter                  none
Integrations             none

Describe the Bug

accessing an import.meta.env variable before a markdown loaded import causes inscrutable bugs. See stackblitz below for an example.

TLDR: This order does not error:

import { Content } from "../test.md"
const mode = import.meta.env.MODE;

This order errors out:

const mode = import.meta.env.MODE;
import { Content } from "../test.md"

In addition to [ERROR] Unterminated string literal I've also seen [ERROR] Expected "*/" to terminate multi-line comment.

If I had to guess I think that some of the Vite preprocessing for environment variables is leaking somehow, but the stack trace is all esbuild stuff that I'm not familiar with:

ERROR: Unterminated string literal
    at failureErrorWithLog (/home/projects/aonirmbnzk.github/node_modules/vite/node_modules/esbuild/lib/main.js:1466:15)
    at eval (/home/projects/aonirmbnzk.github/node_modules/vite/node_modules/esbuild/lib/main.js:745:50)
    at responseCallbacks.<computed> (/home/projects/aonirmbnzk.github/node_modules/vite/node_modules/esbuild/lib/main.js:612:9)
    at handleIncomingPacket (/home/projects/aonirmbnzk.github/node_modules/vite/node_modules/esbuild/lib/main.js:667:12)
    at Socket.readFromStdout (/home/projects/aonirmbnzk.github/node_modules/vite/node_modules/esbuild/lib/main.js:590:7)
    at Socket.emit (node:events:30:10899)
    at addChunk (node:internal/streams/readable:225:3685)
    at readableAddChunk (node:internal/streams/readable:225:3393)
    at Readable.push (node:internal/streams/readable:225:4971)
    at _0x50b08f.onStreamRead (node:internal/stream_base_commons:211:2596)

What's the expected result?

You should be able to access env vars and imports in any legal order without crashing the renderer.

I would submit a PR if I knew where to start on this one... the workaround of reordering imports seems fine for now, but because I don't know the root cause I don't know whether that will always work.

Link to Minimal Reproducible Example

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

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions