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(gatsby-plugin-mdx): pass the correct options to setParserPlugins #18243

Merged
merged 4 commits into from
Oct 8, 2019
Merged

fix(gatsby-plugin-mdx): pass the correct options to setParserPlugins #18243

merged 4 commits into from
Oct 8, 2019

Conversation

d4rekanguok
Copy link
Contributor

@d4rekanguok d4rekanguok commented Oct 7, 2019

Description

Hey folks!

In gatsby-plugin-mdx, when setParserPlugins is called, it was passed in the wrong options source:

for (let plugin of options.gatsbyRemarkPlugins) {
  ...
    for (let parserPlugin of requiredPlugin.setParserPlugins(
        plugin.pluginOptions   // <--- undefined
      )) { ... }

Instead of plugin.pluginOptions, it should have been plugin.options.

Reproduction of bug

https://github.com/d4rekanguok/gatsby-bug-repro-18243

@pieh
Copy link
Contributor

pieh commented Oct 7, 2019

Seems like this is pretty much copy & paste from remark plugin -

for (let plugin of pluginOptions.plugins) {
const requiredPlugin = require(plugin.resolve)
if (_.isFunction(requiredPlugin.setParserPlugins)) {
for (let parserPlugin of requiredPlugin.setParserPlugins(
plugin.pluginOptions
)) {
if (_.isArray(parserPlugin)) {
const [parser, options] = parserPlugin
remark = remark.use(parser, options)
} else {
remark = remark.use(parserPlugin)
}
}
}
}

Which means if that's a bug we have it in both mdx and remark.

Do you have any reproduction so we could verify this change quickly?

@d4rekanguok
Copy link
Contributor Author

Hi @pieh! I actually discovered this while debugging a remark plugin that worked with gatsby-transformer-remark but not gatsby-plugin-mdx, so I think the issue only exists with mdx.

I can quickly make & share a reproduction repo in an hour or so

@d4rekanguok
Copy link
Contributor Author

d4rekanguok commented Oct 7, 2019

@pieh I've just put this repro together:

https://github.com/d4rekanguok/gatsby-bug-repro-18243

The step to reproduce is detailed in the readme.

I think it might have something to do with the fact that remark plugins for mdx are passed in via options as gatsbyRemarkPlugins, while with remark it's plugins. I'm guessing Gatsby might have a recursive system that store plugin options as pluginOptions, but only recognize plugins set via the plugins property?

@pieh
Copy link
Contributor

pieh commented Oct 7, 2019

think it might have something to do with the fact that remark plugins for mdx are passed in via options as gatsbyRemarkPlugins, while with remark it's plugins. I'm guessing Gatsby might have a recursive system that store plugin options as pluginOptions, but only recognize plugins set via the plugins attribute?

Oh yeah, that would explain the difference.

You are correct - plugins field in options does have special handling, and we do some transformation, so if gatsbyRemarkPlugins is used, this transformation won't be done.

@d4rekanguok
Copy link
Contributor Author

Oh in that case, instead of fixing this like I did in this PR, maybe it's better to also handle gatsbyRemarkPlugins specially to keep the plugin chain going?

This might be a really edge case however, since gatsby-plugin-mdx has been released for a long time & no one has yet filed a bug on this

@pieh pieh added the topic: MDX label Oct 7, 2019
@pieh
Copy link
Contributor

pieh commented Oct 7, 2019

I think this is fine. Other subplugin API use options instead of pluginOptions in mdx, so I think this is fine for now.

mdx (using plugin.options):

debug(`userPlugins: constructing remark plugin for `, plugin)
const requiredPlugin = require(plugin.resolve)
const wrappedPlugin = () =>
async function transformer(markdownAST) {
await requiredPlugin(
{
markdownAST,
markdownNode: mdxNode,
getNode,
files: fileNodes,
pathPrefix,
reporter,
cache,
},
plugin.options || {}
)
return markdownAST
}
return [wrappedPlugin, {}]

remark (using plugin.pluginOptions):

const requiredPlugin = require(plugin.resolve)
if (_.isFunction(requiredPlugin)) {
return requiredPlugin(
{
markdownAST,
markdownNode,
getNode,
files: fileNodes,
basePath,
reporter,
cache: getCache(plugin.name),
getCache,
compiler: {
parseString: remark.parse.bind(remark),
generateHTML: getHTML,
},
...rest,
},
plugin.pluginOptions
)

d4rekanguok and others added 2 commits October 8, 2019 00:18
Co-Authored-By: Michal Piechowiak <misiek.piechowiak@gmail.com>
@d4rekanguok
Copy link
Contributor Author

A few of my PRs is failing this unit_tests_node12:

FAIL peril/tests/validate-yaml/validate-yaml-starters.test.ts
● a new PR › Doesn't allow non GitHub repos

I'm not submitting new starters though, so I'm not sure why it's failing... any ideas? @pieh

@pieh
Copy link
Contributor

pieh commented Oct 8, 2019

I'm not submitting new starters though, so I'm not sure why it's failing... any ideas? @pieh

Oh yeah, this has something with recent Node 12 release (I think) - we "fixed" this (or rather workarounded it) in #18247, so I'll just merge master in this PR and that should fix that failing test

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Verified fix. Works like a charm!

Huge thanks for reproduction making it much faster for us to merge this in!

@pieh pieh merged commit 8224af5 into gatsbyjs:master Oct 8, 2019
@pieh
Copy link
Contributor

pieh commented Oct 8, 2019

Published gatsby-plugin-mdx@1.0.49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants