Skip to content

pluginOptionsSchema not working when using typescript #37319

Closed

Description

@klase

Preliminary Checks

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

  1. Create plugin in /plugins/test-plugin
  2. Add gatsby-node.ts with above code
  3. 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.3

Config Flags

No response

Activity

  1. added
    type: bugAn issue or pull request relating to a bug in Gatsby
    on Dec 22, 2022
  2. added
    status: triage neededIssue or pull request that need to be triaged and assigned to a reviewer
    on Dec 22, 2022
  3. added
    status: confirmedIssue with steps to reproduce the bug that’s been verified by at least one reviewer.
    and removed
    status: triage neededIssue or pull request that need to be triaged and assigned to a reviewer
    on Jan 6, 2023
  4. marvinjude commented on Jan 6, 2023

    @marvinjude
    Contributor

    Hey @klase,

    Thanks for reporting this. In my reproduction, I found out that we actually compile the typescript file into .cache/compiled but do not fall back to .cache/compiled to load the gatsby-node, hence we are unable to load pluginOptionsSchema and do the actual validation.

    try {
    const resolvedPlugin = resolvePlugin(plugin, rootDir)
    gatsbyNode = require(`${resolvedPlugin.resolve}/gatsby-node`)
    } catch (err) {
    gatsbyNode = {}
    }

    The fix here will be to fall back to .cache/compiled. We have similar logic here

  5. added
    topic: coreRelates to Gatsby's core (e.g. page loading, reporter, state machine)
    on Jan 11, 2023
  6. self-assigned this
    on Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: confirmedIssue 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)type: bugAn issue or pull request relating to a bug in Gatsby

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions