Repository navigation
pluginOptionsSchema not working when using typescript #37319
Copy link
Copy link
Closed
Labels
status: confirmedIssue with steps to reproduce the bug that’s been verified by at least one reviewer.Issue with steps to reproduce the bug that’s been verified by at least one reviewer.topic: coreRelates to Gatsby's core (e.g. page loading, reporter, state machine)Relates to Gatsby's core (e.g. page loading, reporter, state machine)type: bugAn issue or pull request relating to a bug in GatsbyAn issue or pull request relating to a bug in Gatsby
Description
Preliminary Checks
- This issue is not a duplicate. Before opening a new issue, please search existing issues: https://github.com/gatsbyjs/gatsby/issues
- This issue is not a question, feature request, RFC, or anything other than a bug report directly related to Gatsby. Please post those things in GitHub Discussions: https://github.com/gatsbyjs/gatsby/discussions
Description
Gatsby is not picking up the default values nor validating the plugin schema in a gatsby-node.ts file
export const pluginOptionsSchema: GatsbyNode["pluginOptionsSchema"] = ({
Joi,
}) => {
return Joi.object({
siteId: Joi.string().required(),
templatePath: Joi.string()
.default("/src/templates")
});
};
export const onPreInit: GatsbyNode["onPreInit"] = (args, options) => {
console.log("options: ", options);
// options does NOT contain default values, nor is validation on required options performed
};but if I change it to plain js (gatsby-node.js) with commonjs exports everything's fine:
exports.pluginOptionsSchema = ({ Joi }) => {
return Joi.object({
siteId: Joi.string().required(),
templatePath: Joi.string()
.default("/src/templates")
});
};
exports.onPreInit = (args, options) => {
console.log("options: ", options);
// options contain default values as expected
};Reproduction Link
see code
Steps to Reproduce
- Create plugin in
/plugins/test-plugin - Add gatsby-node.ts with above code
- Run gatsby develop
...
Expected Result
Validation should be performed on the plugin options schema, and default option values set
Actual Result
No defaults are set, and validation is not performed
Environment
System:
OS: macOS 12.5
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
Yarn: 1.22.5 - ~/.yarn/bin/yarn
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
Browsers:
Chrome: 108.0.5359.124
Firefox: 107.0.1
Safari: 15.6
npmPackages:
gatsby: ^5.3.3 => 5.3.3
gatsby-plugin-google-gtag: ^5.2.0 => 5.2.0
gatsby-plugin-image: ^3.2.0 => 3.2.0
gatsby-plugin-manifest: ^5.2.0 => 5.2.0
gatsby-plugin-root-import: ^2.0.9 => 2.0.9
gatsby-plugin-sharp: ^5.2.0 => 5.2.0
gatsby-plugin-sitemap: ^6.0.0 => 6.2.0
gatsby-plugin-styled-components: ^6.0.0 => 6.2.0
gatsby-source-filesystem: ^5.2.0 => 5.2.0
gatsby-transformer-sharp: ^5.2.0 => 5.2.0
npmGlobalPackages:
gatsby: 5.3.3Config Flags
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
status: confirmedIssue with steps to reproduce the bug that’s been verified by at least one reviewer.Issue with steps to reproduce the bug that’s been verified by at least one reviewer.topic: coreRelates to Gatsby's core (e.g. page loading, reporter, state machine)Relates to Gatsby's core (e.g. page loading, reporter, state machine)type: bugAn issue or pull request relating to a bug in GatsbyAn issue or pull request relating to a bug in Gatsby
Activity
marvinjude commentedon Jan 6, 2023
Hey @klase,
Thanks for reporting this. In my reproduction, I found out that we actually compile the typescript file into
.cache/compiledbut do not fall back to.cache/compiledto load thegatsby-node, hence we are unable to loadpluginOptionsSchemaand do the actual validation.gatsby/packages/gatsby/src/bootstrap/load-plugins/validate.ts
Lines 192 to 197 in 99c5688
The fix here will be to fall back to
.cache/compiled. We have similar logic here