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): log config validation errors #23620

Merged
merged 4 commits into from
May 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/gatsby-cli/src/structured-errors/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ const errors = {
level: Level.ERROR,
},
// Config errors
"10122": {
text: (context): string =>
`The site's gatsby-config.js failed validation:\n\n${context.sourceMessage}`,
type: Type.CONFIG,
level: Level.ERROR,
},
"10123": {
text: (context): string =>
`We encountered an error while trying to load your site's ${context.configName}. Please fix the error and try again.`,
Expand Down
6 changes: 2 additions & 4 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getBrowsersList } from "../utils/browserslist"
import { createSchemaCustomization } from "../utils/create-schema-customization"
import { startPluginRunner } from "../redux/plugin-runner"
const { store, emitter } = require(`../redux`)
import { internalActions } from "../redux/actions"
const loadPlugins = require(`./load-plugins`)
const loadThemes = require(`./load-themes`)
const report = require(`gatsby-cli/lib/reporter`)
Expand Down Expand Up @@ -154,10 +155,7 @@ module.exports = async (args: BootstrapArgs) => {
)
}

store.dispatch({
type: `SET_SITE_CONFIG`,
payload: config,
})
store.dispatch(internalActions.setSiteConfig(config))

activity.end()

Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/joi-schemas/joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const gatsbyConfigSchema: Joi.ObjectSchema<IGatsbyConfig> = Joi.object()
.replace(/^\/$/, ``)
)
),
linkPrefix: Joi.forbidden().error(
new Error(`"linkPrefix" should be changed to "pathPrefix"`)
),
Comment on lines +35 to +37
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved from

if (normalizedPayload.linkPrefix) {
console.log(`"linkPrefix" should be changed to "pathPrefix"`)
}
to validation object, so validation is unified

siteMetadata: Joi.object({
siteUrl: stripTrailingSlash(Joi.string()).uri(),
}).unknown(),
Expand Down
88 changes: 88 additions & 0 deletions packages/gatsby/src/redux/actions/__tests__/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { setSiteConfig } from "../internal"
import reporter from "gatsby-cli/lib/reporter"

jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panic: jest.fn(),
}
})

beforeEach(() => {
reporter.panic.mockClear()
})

describe(`setSiteConfig`, () => {
it(`let's you add a config`, () => {
const action = setSiteConfig({
siteMetadata: {
hi: true,
},
})
expect(action).toMatchInlineSnapshot(`
Object {
"payload": Object {
"pathPrefix": "",
"polyfill": true,
"siteMetadata": Object {
"hi": true,
},
},
"type": "SET_SITE_CONFIG",
}
`)
})

it(`handles empty configs`, () => {
const action = setSiteConfig()
expect(action).toMatchInlineSnapshot(`
Object {
"payload": Object {
"pathPrefix": "",
"polyfill": true,
},
"type": "SET_SITE_CONFIG",
}
`)
})

it(`Validates configs with unsupported options`, () => {
setSiteConfig({
someRandomThing: `hi people`,
plugins: [],
})

expect(reporter.panic).toBeCalledWith({
id: `10122`,
context: {
sourceMessage: `"someRandomThing" is not allowed`,
},
})
})

it(`It corrects pathPrefixes without a forward slash at beginning`, () => {
const action = setSiteConfig({
pathPrefix: `prefix`,
})

expect(action.payload.pathPrefix).toBe(`/prefix`)
})

it(`It removes trailing forward slash`, () => {
const action = setSiteConfig({
pathPrefix: `/prefix/`,
})
expect(action.payload.pathPrefix).toBe(`/prefix`)
})

it(`It removes pathPrefixes that are a single forward slash`, () => {
const action = setSiteConfig({
pathPrefix: `/`,
})
expect(action.payload.pathPrefix).toBe(``)
})

it(`It sets the pathPrefix to an empty string if it's not set`, () => {
const action = setSiteConfig({})
expect(action.payload.pathPrefix).toBe(``)
})
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those are tests that previously were reducer tests adjusted to be action creator test (same scenarios)

29 changes: 29 additions & 0 deletions packages/gatsby/src/redux/actions/internal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import reporter from "gatsby-cli/lib/reporter"

import {
IGatsbyConfig,
IGatsbyPlugin,
ProgramStatus,
ICreatePageDependencyAction,
Expand All @@ -12,8 +15,11 @@ import {
ISetProgramStatusAction,
IPageQueryRunAction,
IRemoveStaleJobAction,
ISetSiteConfig,
} from "../types"

import { gatsbyConfigSchema } from "../../joi-schemas/joi"

/**
* Create a dependency between a page and data. Probably for
* internal use only.
Expand Down Expand Up @@ -217,3 +223,26 @@ export const removeStaleJob = (
},
}
}

/**
* Set gatsby config
* @private
*/
export const setSiteConfig = (config?: unknown): ISetSiteConfig => {
const result = gatsbyConfigSchema.validate(config || {})
const normalizedPayload: IGatsbyConfig = result.value

if (result.error) {
reporter.panic({
id: `10122`,
context: {
sourceMessage: result.error.message,
},
})
}

return {
type: `SET_SITE_CONFIG`,
payload: normalizedPayload,
}
}

This file was deleted.

81 changes: 0 additions & 81 deletions packages/gatsby/src/redux/reducers/__tests__/config.js

This file was deleted.

32 changes: 0 additions & 32 deletions packages/gatsby/src/redux/reducers/config.js

This file was deleted.

14 changes: 14 additions & 0 deletions packages/gatsby/src/redux/reducers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IGatsbyConfig, ISetSiteConfig } from "../types"

module.exports = (
state: IGatsbyConfig = {},
action: ISetSiteConfig
): IGatsbyConfig => {
switch (action.type) {
case `SET_SITE_CONFIG`: {
return action.payload
}
default:
return state
}
}
8 changes: 8 additions & 0 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export interface IGatsbyConfig {
title?: string
author?: string
description?: string
sireUrl?: string
Copy link
Contributor

@muescha muescha May 26, 2020

Choose a reason for hiding this comment

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

typo here... siteUrl

--> #24488

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🙈 Thanks!

// siteMetadata is free form
[key: string]: unknown
Copy link
Contributor

Choose a reason for hiding this comment

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

Fail

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean - in context of gatsby core it doesn't matter much, because core doesn't make use of those fields really

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it might matter when we use those types for public index.d.ts declaration instead of hand written one like we do right now

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, doesn't matter at all

Just weird to see those

Copy link
Contributor Author

@pieh pieh May 4, 2020

Choose a reason for hiding this comment

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

I needed it here I think for tests in particular which were setting random thing (really random):

    setSiteConfig({
      someRandomThing: `hi people`,
      plugins: [],
    })

---edit - that was wrong example - see comment below

Copy link
Contributor Author

@pieh pieh May 4, 2020

Choose a reason for hiding this comment

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

above was wrong one - that one was failing - it was for this one:

    const action = setSiteConfig({
      siteMetadata: {
        hi: true,
      },
    })

Copy link
Contributor

Choose a reason for hiding this comment

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

someRandomThing: `hi people`

This is almost as good as your house is on fire

}
// @deprecated
polyfill?: boolean
Expand Down Expand Up @@ -422,3 +425,8 @@ export interface ISetWebpackConfigAction {
type: `SET_WEBPACK_CONFIG`
payload: Partial<IGatsbyState["webpack"]>
}

export interface ISetSiteConfig {
type: `SET_SITE_CONFIG`
payload: IGatsbyState["config"]
}