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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown): Fix Markdown images breaking the build #8598

Merged
merged 7 commits into from
Sep 21, 2023

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Sep 19, 2023

Changes

This PR reworks how we handle images in Markdown to workaround a Rollup bug with top level await. This has the side effect of needing us to rework how error handling is done around these parts, which honestly is great because it had some Markdown specific parts that I was able to make more general, improving the overall experience.

Fix #8547
Also fix #8454, by accident

Testing

Tests should all pass! The specific problem here is kinda hard to test because it depends specifically on how the website gets bundled, confusing

Docs

Added two errors and deprecated an old one. One of added one is mostly a copy paste of the deprecated one.

@Princesseuh Princesseuh requested a review from a team as a code owner September 19, 2023 12:48
@changeset-bot
Copy link

changeset-bot bot commented Sep 19, 2023

🦋 Changeset detected

Latest commit: e095d44

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Sep 19, 2023
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Only one small nit I'd consider, otherwise, LGTM!

packages/astro/src/core/errors/errors-data.ts Outdated Show resolved Hide resolved
...images[imagePath].attributes,
})
);
});
}

const html = updateImageReferences(${JSON.stringify(html)});
Copy link
Member

Choose a reason for hiding this comment

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

Since updateImageReferences now returns a promise, wouldn't html become a promise and affect compiledContent and the Content render code?

Seems like we need another TLA here but maybe that is able to side-step the Rollup bug.

Copy link
Member Author

@Princesseuh Princesseuh Sep 19, 2023

Choose a reason for hiding this comment

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

Ah yes, I didn't notice that it made html a promise since it worked in rendering... Unfortunately, putting a TLA anywhere in this file causes the issue. No idea what to do then.

Copy link
Member

Choose a reason for hiding this comment

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

I think the Rollup bug is only caused when dynamically importing a module that causes an import loop, but this PR removed the dynamic import as a top-level import, so theoretically, I think we can keep using TLA?

Unless if you're already tested it, then pardon me 😅 We might have to refactor some modules to break the loop otherwise. Or fix in Rollup directly.

Copy link
Member

Choose a reason for hiding this comment

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

By TLA, I mean directly on this line, like const html = await ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Doing const html = await causes the issue, unfortunately

Copy link
Member

Choose a reason for hiding this comment

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

I guess I could try fixing this in Rollup directly

@bluwy
Copy link
Member

bluwy commented Sep 21, 2023

I think my last commit should work as a workaround for now. The Rollup chunking algorithm is amazing but I can't figure out how to handle TLA deadlock specifically. It already failed to chunk correctly on the initial chunk-by-module-dependants flow.

// https://github.com/rollup/rollup/issues/4708
extendManualChunks(outputOptions, {
before(id) {
if (id.includes('astro/dist/assets/services')) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Will this hardcoding cause an issue for custom services?

Copy link
Member

Choose a reason for hiding this comment

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

Would custom services import things like from astro/assets/utils? Then maybe we need to go one directory up and make it astro/dist/assets instead 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, admittedly, it's fairly unlikely

Copy link
Member

@bluwy bluwy Sep 21, 2023

Choose a reason for hiding this comment

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

I think either ways it should still work fine as we're primarily grouping our Astro code into one chunk. The issue before was that services/sharp.ts uses services/service.ts. But both were in two different chunks. services/sharp.ts was in it's own chunk, but services/service.ts got grouped with the markdown page chunk. And there was an import loop after Rollup chunks it, and the TLA in the markdown page made it worst.

So manually handling where services/service.ts should be chunked fixed it here. I don't mind if we want to go back one directory down though.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm honestly not sure of the impact this can have, so I think whichever works just fine for me as long as it works, ha.

If it turns out to be a problem somehow, we can change it back fairly easily it seems like

Copy link
Member

Choose a reason for hiding this comment

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

Changed it back to astro/dist/assets/services/ for now since it feels a liitle safer that way 👍

@matthewp matthewp merged commit bdd267d into main Sep 21, 2023
13 checks passed
@matthewp matthewp deleted the fix/markdown-images branch September 21, 2023 17:07
@astrobot-houston astrobot-houston mentioned this pull request Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
4 participants