Skip to content

Commit

Permalink
fix(gatsby): show theme that has faulty config (#27708)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxstbr committed Nov 2, 2020
1 parent f43a6bd commit d7d1b97
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/gatsby-cli/src/structured-errors/error-map.ts
Expand Up @@ -468,7 +468,9 @@ const errors = {
text: (context): string =>
[
stripIndent(`
Invalid plugin options for "${context.pluginName}":
Invalid plugin options for "${context.pluginName}"${
context.configDir ? `, configured by ${context.configDir}` : ``
}:
`),
]
.concat([``])
Expand Down
Expand Up @@ -225,6 +225,7 @@ describe(`Load plugins`, () => {
Array [
Object {
"context": Object {
"configDir": null,
"pluginName": "gatsby-plugin-google-analytics",
"validationErrors": Array [
Object {
Expand Down Expand Up @@ -262,6 +263,7 @@ describe(`Load plugins`, () => {
Array [
Object {
"context": Object {
"configDir": null,
"pluginName": "gatsby-plugin-google-analytics",
"validationErrors": Array [
Object {
Expand Down Expand Up @@ -339,6 +341,7 @@ describe(`Load plugins`, () => {
Array [
Object {
"context": Object {
"configDir": null,
"pluginName": "gatsby-remark-autolink-headers",
"validationErrors": Array [
Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/load-plugins/index.ts
Expand Up @@ -89,7 +89,7 @@ export async function loadPlugins(

// Show errors for invalid plugin configuration
if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
await validateConfigPluginsOptions(config)
await validateConfigPluginsOptions(config, rootDir)
}

const currentAPIs = getAPI({
Expand Down
23 changes: 19 additions & 4 deletions packages/gatsby/src/bootstrap/load-plugins/validate.ts
@@ -1,4 +1,5 @@
import _ from "lodash"
import path from "path"
import * as semver from "semver"
import * as stringSimilarity from "string-similarity"
import { version as gatsbyVersion } from "gatsby/package.json"
Expand Down Expand Up @@ -174,7 +175,8 @@ export async function handleBadExports({
}

async function validatePluginsOptions(
plugins: Array<IPluginRefObject>
plugins: Array<IPluginRefObject>,
rootDir: string | null
): Promise<{
errors: number
plugins: Array<IPluginRefObject>
Expand Down Expand Up @@ -227,16 +229,25 @@ async function validatePluginsOptions(
errors: subErrors,
plugins: subPlugins,
} = await validatePluginsOptions(
plugin.options.plugins as Array<IPluginRefObject>
plugin.options.plugins as Array<IPluginRefObject>,
rootDir
)
plugin.options.plugins = subPlugins
errors += subErrors
}
} catch (error) {
if (error instanceof Joi.ValidationError) {
// If rootDir and plugin.parentDir are the same, i.e. if this is a plugin a user configured in their gatsby-config.js (and not a sub-theme that added it), this will be ""
// Otherwise, this will contain (and show) the relative path
const configDir =
(plugin.parentDir &&
rootDir &&
path.relative(rootDir, plugin.parentDir)) ||
null
reporter.error({
id: `11331`,
context: {
configDir,
validationErrors: error.details,
pluginName: plugin.resolve,
},
Expand All @@ -256,11 +267,15 @@ async function validatePluginsOptions(
}

export async function validateConfigPluginsOptions(
config: ISiteConfig = {}
config: ISiteConfig = {},
rootDir: string | null
): Promise<void> {
if (!config.plugins) return

const { errors, plugins } = await validatePluginsOptions(config.plugins)
const { errors, plugins } = await validatePluginsOptions(
config.plugins,
rootDir
)

config.plugins = plugins

Expand Down

0 comments on commit d7d1b97

Please sign in to comment.