Skip to content

Commit 7499b22

Browse files
GatsbyJS Botvladar
GatsbyJS Bot
andauthored
feat(gatsby): display message about unfit flags found in config (#32394) (#32424)
(cherry picked from commit 7df39aa) Co-authored-by: Vladimir Razuvaev <[email protected]>
1 parent 006788d commit 7499b22

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

packages/gatsby/src/bootstrap/load-config-and-plugins.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,19 @@ export async function loadConfigAndPlugins({
4444
// Setup flags
4545
if (config) {
4646
// Get flags
47-
const { enabledConfigFlags, unknownFlagMessage, message } = handleFlags(
48-
availableFlags,
49-
config.flags
50-
)
47+
const {
48+
enabledConfigFlags,
49+
unknownFlagMessage,
50+
unfitFlagMessage,
51+
message,
52+
} = handleFlags(availableFlags, config.flags)
5153

5254
if (unknownFlagMessage !== ``) {
5355
reporter.warn(unknownFlagMessage)
5456
}
55-
57+
if (unfitFlagMessage !== ``) {
58+
reporter.warn(unfitFlagMessage)
59+
}
5660
// set process.env for each flag
5761
enabledConfigFlags.forEach(flag => {
5862
process.env[flag.env] = `true`

packages/gatsby/src/utils/__tests__/__snapshots__/handle-flags.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ There are 2 other flags available that you might be interested in:
5454
- FAST_DEV · Enable all experiments aimed at improving develop server start time
5555
- YET_ANOTHER · (Umbrella Issue (test)) · test
5656
",
57+
"unfitFlagMessage": "",
5758
"unknownFlagMessage": "",
5859
}
5960
`;
@@ -101,6 +102,7 @@ There are 4 other flags available that you might be interested in:
101102
- ALL_COMMANDS · (Umbrella Issue (test)) · test
102103
- YET_ANOTHER · (Umbrella Issue (test)) · test
103104
",
105+
"unfitFlagMessage": "",
104106
"unknownFlagMessage": "",
105107
}
106108
`;
@@ -160,6 +162,7 @@ There are 3 other flags available that you might be interested in:
160162
- DEV_SSR · (Umbrella Issue (https://github.com/gatsbyjs/gatsby/discussions/28138)) · SSR pages on full reloads during develop. Helps you detect SSR bugs and fix them without needing to do full builds.
161163
- YET_ANOTHER · (Umbrella Issue (test)) · test
162164
",
165+
"unfitFlagMessage": "",
163166
"unknownFlagMessage": "The following flag(s) found in your gatsby-config.js are not known:
164167
- FASTLY_DEV (did you mean: FAST_DEV)
165168
- SUPER_COOL_FLAG",
@@ -256,6 +259,7 @@ The following flags were automatically enabled on your site:
256259
There is one other flag available that you might be interested in:
257260
- YET_ANOTHER · (Umbrella Issue (test)) · test
258261
",
262+
"unfitFlagMessage": "",
259263
"unknownFlagMessage": "",
260264
}
261265
`;

packages/gatsby/src/utils/__tests__/handle-flags.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ describe(`handle flags`, () => {
145145
}
146146
},
147147
},
148+
{
149+
name: `LMDB_NODE14_ONLY`,
150+
env: `GATSBY_LMDB`,
151+
command: `all`,
152+
description: `test`,
153+
umbrellaIssue: `test`,
154+
telemetryId: `test`,
155+
experimental: false,
156+
testFitness: (): fitnessEnum => false,
157+
requires: `Requires Node 14.10+`,
158+
},
148159
]
149160

150161
const configFlags = {
@@ -192,6 +203,24 @@ describe(`handle flags`, () => {
192203
expect(unknownConfigFlags).toMatchSnapshot()
193204
})
194205

206+
it(`returns a message about unfit flags in the config`, () => {
207+
const unfitConfigFlags = handleFlags(
208+
activeFlags,
209+
{ LMDB_NODE14_ONLY: true },
210+
`develop`
211+
)
212+
expect(unfitConfigFlags.enabledConfigFlags).not.toContain(
213+
expect.objectContaining({
214+
name: `LMDB_NODE14_ONLY`,
215+
})
216+
)
217+
expect(unfitConfigFlags.unfitFlagMessage).toMatchInlineSnapshot(`
218+
"The following flag(s) found in your gatsby-config.js are not supported in your environment and will have no effect:
219+
- LMDB_NODE14_ONLY: Requires Node 14.10+"
220+
`)
221+
expect(unfitConfigFlags.unknownFlagMessage).toEqual(``)
222+
})
223+
195224
it(`opts in sites to a flag if their site is selected for partial release`, () => {
196225
// Nothing is enabled in their config.
197226
const response = handleFlags(activeFlags, {}, `develop`)
@@ -226,6 +255,8 @@ describe(`handle flags`, () => {
226255
Object {
227256
"enabledConfigFlags": Array [],
228257
"message": "",
258+
"unfitFlagMessage": "The following flag(s) found in your gatsby-config.js are not supported in your environment and will have no effect:
259+
- PARTIAL_RELEASE_ONLY_VERY_OLD_LODASH",
229260
"unknownFlagMessage": "",
230261
}
231262
`)
@@ -377,6 +408,7 @@ describe(`handle flags`, () => {
377408
"message": "The following flags are active:
378409
- SOME_FLAG · (Umbrella Issue (test)) · test
379410
",
411+
"unfitFlagMessage": "",
380412
"unknownFlagMessage": "",
381413
}
382414
`)

packages/gatsby/src/utils/flags.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ export interface IFlag {
6666
* (avoids showing unknown flag message and shows "no longer needed" message).
6767
*/
6868
testFitness: (flag: IFlag) => fitnessEnum
69+
/**
70+
* Human-readable text explaining requirements for this feature to be available
71+
* (e.g. requires Node 14+)
72+
*
73+
* It is shown to users when testFitness() returns `false` but flag is set in gatsby-config.js
74+
*/
75+
requires?: string
6976
includedFlags?: Array<string>
7077
umbrellaIssue?: string
7178
noCI?: boolean
@@ -135,6 +142,7 @@ const activeFlags: Array<IFlag> = [
135142
return false
136143
}
137144
},
145+
requires: `Requires [email protected] or above.`,
138146
},
139147
{
140148
name: `PRESERVE_WEBPACK_CACHE`,
@@ -198,6 +206,7 @@ const activeFlags: Array<IFlag> = [
198206
const [major, minor] = process.versions.node.split(`.`)
199207
return (Number(major) === 14 && Number(minor) >= 10) || Number(major) > 14
200208
},
209+
requires: `Requires Node v14.10 or above.`,
201210
},
202211
{
203212
name: `PARALLEL_QUERY_RUNNING`,
@@ -212,6 +221,7 @@ const activeFlags: Array<IFlag> = [
212221
const [major, minor] = process.versions.node.split(`.`)
213222
return (Number(major) === 14 && Number(minor) >= 10) || Number(major) > 14
214223
},
224+
requires: `Requires Node v14.10 or above.`,
215225
},
216226
]
217227

packages/gatsby/src/utils/handle-flags.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const handleFlags = (
1212
): {
1313
enabledConfigFlags: Array<IFlag>
1414
unknownFlagMessage: string
15+
unfitFlagMessage: string
1516
message: string
1617
} => {
1718
// Prepare config flags.
@@ -23,6 +24,7 @@ const handleFlags = (
2324

2425
// Find unknown flags someone has in their config to warn them about.
2526
const unknownConfigFlags: Array<{ flag: string; didYouMean: string }> = []
27+
const unfitConfigFlags: Array<{ flag: string; requires: string }> = []
2628
for (const flagName in configFlags) {
2729
if (availableFlags.has(flagName)) {
2830
continue
@@ -99,8 +101,23 @@ const handleFlags = (
99101
if (fitness === true || fitness === `OPT_IN`) {
100102
applicableFlags.set(flag.name, flag)
101103
}
104+
105+
if (fitness === false && enabledConfigFlags.includes(flag)) {
106+
unfitConfigFlags.push({ flag: flag.name, requires: flag.requires ?? `` })
107+
}
102108
})
103109

110+
let unfitFlagMessage = ``
111+
if (unfitConfigFlags.length > 0) {
112+
unfitFlagMessage =
113+
`The following flag(s) found in your gatsby-config.js are not supported in your environment and will have no effect:\n` +
114+
unfitConfigFlags
115+
.map(
116+
flag => `- ${flag.flag}${flag.requires ? `: ${flag.requires}` : ``}`
117+
)
118+
.join(`\n`)
119+
}
120+
104121
// Filter enabledConfigFlags against various tests
105122
enabledConfigFlags = enabledConfigFlags.filter(flag => {
106123
if (flag.command !== `all` && flag.command !== executingCommand) {
@@ -239,6 +256,7 @@ The following flags were automatically enabled on your site:`
239256
enabledConfigFlags,
240257
message,
241258
unknownFlagMessage,
259+
unfitFlagMessage,
242260
}
243261
}
244262

0 commit comments

Comments
 (0)