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): show theme that has faulty config #27708

Merged
merged 4 commits into from Nov 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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