From 9acb79174114a572fed66eb51ca3e4880b1f5cd7 Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:09:26 +0200 Subject: [PATCH 1/8] fix(gatsby): Support non-serializable SDK options --- packages/gatsby/gatsby-browser.js | 44 ++++++++++++++++++++++++++++++- packages/gatsby/gatsby-node.js | 39 ++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/gatsby-browser.js b/packages/gatsby/gatsby-browser.js index e0ab9d5199da..365fecd5c5a8 100644 --- a/packages/gatsby/gatsby-browser.js +++ b/packages/gatsby/gatsby-browser.js @@ -1,7 +1,36 @@ const Sentry = require('@sentry/gatsby'); +// To avoid confusion, you must set the Sentry configuration in one +// place: either in `gatsby-config.js` (without non-serializable +// option support) or in `sentry.config.js` (supporting them). +// Defining them in `sentry.config.js` makes the SDK to initialize +// before this script is run, so we want to make sure to disable +// the SDK if both places contain options and warn the user about +// it. If the SDK hasn't been initialized at this point, we know +// there aren't any options set in `sentry.config.js`, so it's safe +// to initialize it here. + exports.onClientEntry = function(_, pluginParams) { - if (pluginParams === undefined) { + const isIntialized = isSentryInitialized(); + if (!areSentryOptionsDefined(pluginParams)) { + if (!isIntialized) { + // eslint-disable-next-line no-console + console.error( + 'Sentry Logger [Error]: No config for the Gatsby SDK was found. Learn how to configure it on\n' + + 'https://docs.sentry.io/platforms/javascript/guides/gatsby/', + ); + } + return; + } + + if (isIntialized) { + // eslint-disable-next-line no-console + console.error( + 'Sentry Logger [Error]: The SDK has been disabled because your Sentry config must live in one place.\n' + + 'Consider moving it all to your `sentry.config.js`.', + ); + // TODO: link to the docs where the new approach is documented + window.__SENTRY__.hub.getClient().getOptions().enabled = false; return; } @@ -15,3 +44,16 @@ exports.onClientEntry = function(_, pluginParams) { window.Sentry = Sentry; }; + +function isSentryInitialized() { + return !!(window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.getClient()); +} + +function areSentryOptionsDefined(params) { + if (params == undefined) return false; + // Even if there aren't any options, there's a `plugins` property defined as an empty array + if (Object.keys(params).length == 1 && Array.isArray(params.plugins) && params.plugins.length == 0) { + return false; + } + return true; +} diff --git a/packages/gatsby/gatsby-node.js b/packages/gatsby/gatsby-node.js index f8a6a74ad885..3acb60fb7e91 100644 --- a/packages/gatsby/gatsby-node.js +++ b/packages/gatsby/gatsby-node.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + const sentryRelease = JSON.stringify( // Always read first as Sentry takes this as precedence process.env.SENTRY_RELEASE || @@ -15,8 +17,9 @@ const sentryRelease = JSON.stringify( ); const sentryDsn = JSON.stringify(process.env.SENTRY_DSN || ''); +const SENTRY_USER_CONFIG = './sentry.config.js'; -exports.onCreateWebpackConfig = ({ plugins, actions }) => { +exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { actions.setWebpackConfig({ plugins: [ plugins.define({ @@ -25,4 +28,38 @@ exports.onCreateWebpackConfig = ({ plugins, actions }) => { }), ], }); + + // To configure the SDK `sentry.config.js` is prioritized over `gatsby-config.js`, + // since it isn't possible to set non-serializable parameters in the latter. + if (!fs.existsSync(SENTRY_USER_CONFIG)) { + // We don't want to warn users here, yet they may have their config in `gatsby-config.js`. + return; + } + // `setWebpackConfig` merges the Webpack config, ignoring some props like `entry`. See + // https://www.gatsbyjs.com/docs/reference/config-files/actions/#setWebpackConfig + // So it's not possible to inject the Sentry properties with that method. Instead, we + // can replace the whole config with the modifications we need. + const finalConfig = injectSentryConfig(getConfig()); + actions.replaceWebpackConfig(finalConfig); }; + +function injectSentryConfig(config) { + const injectedEntries = {}; + Object.keys(config.entry).map(prop => { + const value = config.entry[prop]; + let injectedValue = value; + if (typeof value === 'string') { + injectedValue = [SENTRY_USER_CONFIG, value]; + } else if (Array.isArray(value)) { + injectedValue = [SENTRY_USER_CONFIG, ...value]; + } else { + // eslint-disable-next-line no-console + console.error( + `Sentry Logger [Error]: Could not inject SDK initialization code into ${prop}, unexpected format: `, + typeof value, + ); + } + injectedEntries[prop] = injectedValue; + }); + return { ...config, entry: injectedEntries }; +} From cf31a7253871ea6ac0ddf5dafb8002487ea67a9b Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:20:57 +0200 Subject: [PATCH 2/8] Add comments and TODOs --- packages/gatsby/gatsby-browser.js | 3 +++ packages/gatsby/gatsby-node.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/packages/gatsby/gatsby-browser.js b/packages/gatsby/gatsby-browser.js index 365fecd5c5a8..9eeba8b5a011 100644 --- a/packages/gatsby/gatsby-browser.js +++ b/packages/gatsby/gatsby-browser.js @@ -46,6 +46,9 @@ exports.onClientEntry = function(_, pluginParams) { }; function isSentryInitialized() { + // Although `window` should exist because we're in the browser (where this script + // is run), and `__SENTRY__.hub` is created when importing the Gatsby SDK, double + // check that in case something weird happens. return !!(window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.getClient()); } diff --git a/packages/gatsby/gatsby-node.js b/packages/gatsby/gatsby-node.js index 3acb60fb7e91..4a45056af26d 100644 --- a/packages/gatsby/gatsby-node.js +++ b/packages/gatsby/gatsby-node.js @@ -31,6 +31,7 @@ exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { // To configure the SDK `sentry.config.js` is prioritized over `gatsby-config.js`, // since it isn't possible to set non-serializable parameters in the latter. + // Prioritization here means what `init` is being run first. if (!fs.existsSync(SENTRY_USER_CONFIG)) { // We don't want to warn users here, yet they may have their config in `gatsby-config.js`. return; @@ -45,6 +46,8 @@ exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { function injectSentryConfig(config) { const injectedEntries = {}; + // TODO: investigate what entries need the Sentry config injected. + // We may want to skip some. Object.keys(config.entry).map(prop => { const value = config.entry[prop]; let injectedValue = value; From 01573c5c5e207a82aac36e39681d43663d1c4825 Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 13 Oct 2021 15:46:08 +0200 Subject: [PATCH 3/8] fix browser tests --- packages/gatsby/gatsby-browser.js | 23 ++++--- packages/gatsby/test/gatsby-browser.test.ts | 74 ++++++++++++++++++++- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/packages/gatsby/gatsby-browser.js b/packages/gatsby/gatsby-browser.js index 9eeba8b5a011..dc9d250c8156 100644 --- a/packages/gatsby/gatsby-browser.js +++ b/packages/gatsby/gatsby-browser.js @@ -13,7 +13,9 @@ const Sentry = require('@sentry/gatsby'); exports.onClientEntry = function(_, pluginParams) { const isIntialized = isSentryInitialized(); if (!areSentryOptionsDefined(pluginParams)) { - if (!isIntialized) { + if (isIntialized) { + window.Sentry = Sentry; // For backwards compatibility + } else { // eslint-disable-next-line no-console console.error( 'Sentry Logger [Error]: No config for the Gatsby SDK was found. Learn how to configure it on\n' + @@ -31,18 +33,17 @@ exports.onClientEntry = function(_, pluginParams) { ); // TODO: link to the docs where the new approach is documented window.__SENTRY__.hub.getClient().getOptions().enabled = false; - return; + } else { + Sentry.init({ + // eslint-disable-next-line no-undef + release: __SENTRY_RELEASE__, + // eslint-disable-next-line no-undef + dsn: __SENTRY_DSN__, + ...pluginParams, + }); } - Sentry.init({ - // eslint-disable-next-line no-undef - release: __SENTRY_RELEASE__, - // eslint-disable-next-line no-undef - dsn: __SENTRY_DSN__, - ...pluginParams, - }); - - window.Sentry = Sentry; + window.Sentry = Sentry; // For backwards compatibility }; function isSentryInitialized() { diff --git a/packages/gatsby/test/gatsby-browser.test.ts b/packages/gatsby/test/gatsby-browser.test.ts index e39926e77b37..e848784c0362 100644 --- a/packages/gatsby/test/gatsby-browser.test.ts +++ b/packages/gatsby/test/gatsby-browser.test.ts @@ -16,6 +16,7 @@ jest.mock('@sentry/gatsby', () => { }, }; }); +global.console.error = jest.fn(); let tracingAddExtensionMethods = jest.fn(); jest.mock('@sentry/tracing', () => { @@ -50,9 +51,76 @@ describe('onClientEntry', () => { } }); - it('sets window.Sentry', () => { - onClientEntry(undefined, {}); - expect((window as any).Sentry).not.toBeUndefined(); + describe('inits Sentry once', () => { + afterEach(() => { + delete (window as any).Sentry; + delete (window as any).__SENTRY__; + (global.console.error as jest.Mock).mockClear(); + }); + + function setMockedSentryInWindow() { + const sdkOptions = { + enabled: true, + }; + (window as any).__SENTRY__ = { + hub: { + getClient: () => ({ + getOptions: () => sdkOptions, + }), + }, + }; + } + + it('initialized in injected config, without pluginParams', () => { + setMockedSentryInWindow(); + onClientEntry(undefined, { plugins: [] }); + // eslint-disable-next-line no-console + expect(console.error).not.toHaveBeenCalled(); + expect(sentryInit).not.toHaveBeenCalled(); + expect((window as any).Sentry).toBeDefined(); + }); + + it('initialized in injected config, with pluginParams', () => { + setMockedSentryInWindow(); + onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' }); + // eslint-disable-next-line no-console + expect((console.error as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Sentry Logger [Error]: The SDK has been disabled because your Sentry config must live in one place. + Consider moving it all to your \`sentry.config.js\`.", + ] + `); + expect(sentryInit).not.toHaveBeenCalled(); + expect((window as any).__SENTRY__.hub.getClient().getOptions().enabled).toBe(false); + expect((window as any).Sentry).toBeDefined(); + }); + + it('not initialized in injected config, without pluginParams', () => { + onClientEntry(undefined, { plugins: [] }); + // eslint-disable-next-line no-console + expect((console.error as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Sentry Logger [Error]: No config for the Gatsby SDK was found. Learn how to configure it on + https://docs.sentry.io/platforms/javascript/guides/gatsby/", + ] + `); + expect((window as any).Sentry).not.toBeDefined(); + }); + + it('not initialized in injected config, with pluginParams', () => { + onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' }); + // eslint-disable-next-line no-console + expect(console.error).not.toHaveBeenCalled(); + expect(sentryInit).toHaveBeenCalledTimes(1); + expect(sentryInit.mock.calls[0][0]).toMatchInlineSnapshot(` + Object { + "dsn": "dsn", + "plugins": Array [], + "release": "release", + } + `); + expect((window as any).Sentry).toBeDefined(); + }); }); it('sets a tracesSampleRate if defined as option', () => { From 41c41db21efd49f5aeac62933e863e486c6d1738 Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Fri, 15 Oct 2021 10:38:56 +0200 Subject: [PATCH 4/8] Support TS config files --- packages/gatsby/gatsby-node.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/gatsby/gatsby-node.js b/packages/gatsby/gatsby-node.js index 4a45056af26d..4158add8280d 100644 --- a/packages/gatsby/gatsby-node.js +++ b/packages/gatsby/gatsby-node.js @@ -17,7 +17,7 @@ const sentryRelease = JSON.stringify( ); const sentryDsn = JSON.stringify(process.env.SENTRY_DSN || ''); -const SENTRY_USER_CONFIG = './sentry.config.js'; +const SENTRY_USER_CONFIG = ['./sentry.config.js', './sentry.config.ts']; exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { actions.setWebpackConfig({ @@ -29,22 +29,22 @@ exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { ], }); - // To configure the SDK `sentry.config.js` is prioritized over `gatsby-config.js`, + // To configure the SDK, SENTRY_USER_CONFIG is prioritized over `gatsby-config.js`, // since it isn't possible to set non-serializable parameters in the latter. // Prioritization here means what `init` is being run first. - if (!fs.existsSync(SENTRY_USER_CONFIG)) { - // We don't want to warn users here, yet they may have their config in `gatsby-config.js`. + const configFile = SENTRY_USER_CONFIG.find(file => fs.existsSync(file)); + if (!configFile) { return; } // `setWebpackConfig` merges the Webpack config, ignoring some props like `entry`. See // https://www.gatsbyjs.com/docs/reference/config-files/actions/#setWebpackConfig // So it's not possible to inject the Sentry properties with that method. Instead, we // can replace the whole config with the modifications we need. - const finalConfig = injectSentryConfig(getConfig()); + const finalConfig = injectSentryConfig(getConfig(), configFile); actions.replaceWebpackConfig(finalConfig); }; -function injectSentryConfig(config) { +function injectSentryConfig(config, configFile) { const injectedEntries = {}; // TODO: investigate what entries need the Sentry config injected. // We may want to skip some. @@ -52,9 +52,9 @@ function injectSentryConfig(config) { const value = config.entry[prop]; let injectedValue = value; if (typeof value === 'string') { - injectedValue = [SENTRY_USER_CONFIG, value]; + injectedValue = [configFile, value]; } else if (Array.isArray(value)) { - injectedValue = [SENTRY_USER_CONFIG, ...value]; + injectedValue = [configFile, ...value]; } else { // eslint-disable-next-line no-console console.error( From 0e60931c75ab9c006dea7b688750f25e2d4e8635 Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Fri, 15 Oct 2021 11:42:38 +0200 Subject: [PATCH 5/8] simplify browser logic If... - The SDK has been initialized in a config file, use that instance. If options are also defined, log a warning to merge the options. - The SDK has not been initialized, initialize it. Unless options have not been provided, and log an error if that happens. --- packages/gatsby/gatsby-browser.js | 53 ++++++++------------- packages/gatsby/test/gatsby-browser.test.ts | 26 ++++++---- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/packages/gatsby/gatsby-browser.js b/packages/gatsby/gatsby-browser.js index dc9d250c8156..3e2598a0e0b5 100644 --- a/packages/gatsby/gatsby-browser.js +++ b/packages/gatsby/gatsby-browser.js @@ -1,48 +1,37 @@ +/* eslint-disable no-console */ const Sentry = require('@sentry/gatsby'); -// To avoid confusion, you must set the Sentry configuration in one -// place: either in `gatsby-config.js` (without non-serializable -// option support) or in `sentry.config.js` (supporting them). -// Defining them in `sentry.config.js` makes the SDK to initialize -// before this script is run, so we want to make sure to disable -// the SDK if both places contain options and warn the user about -// it. If the SDK hasn't been initialized at this point, we know -// there aren't any options set in `sentry.config.js`, so it's safe -// to initialize it here. - exports.onClientEntry = function(_, pluginParams) { const isIntialized = isSentryInitialized(); - if (!areSentryOptionsDefined(pluginParams)) { - if (isIntialized) { - window.Sentry = Sentry; // For backwards compatibility - } else { - // eslint-disable-next-line no-console - console.error( - 'Sentry Logger [Error]: No config for the Gatsby SDK was found. Learn how to configure it on\n' + - 'https://docs.sentry.io/platforms/javascript/guides/gatsby/', + const areOptionsDefined = areSentryOptionsDefined(pluginParams); + + if (isIntialized) { + window.Sentry = Sentry; // For backwards compatibility + if (areOptionsDefined) { + console.warn( + 'Sentry Logger [Warn]: The SDK was initialized in the Sentry config file, but options were found in the Gatsby config. ' + + 'These have been ignored, merge them to the Sentry config if you want to use them.\n' + + 'Learn more about the Gatsby SDK on https://docs.sentry.io/platforms/javascript/guides/gatsby/', ); } return; } - if (isIntialized) { - // eslint-disable-next-line no-console + if (!areOptionsDefined) { console.error( - 'Sentry Logger [Error]: The SDK has been disabled because your Sentry config must live in one place.\n' + - 'Consider moving it all to your `sentry.config.js`.', + 'Sentry Logger [Error]: No config for the Gatsby SDK was found.\n' + + 'Learn how to configure it on https://docs.sentry.io/platforms/javascript/guides/gatsby/', ); - // TODO: link to the docs where the new approach is documented - window.__SENTRY__.hub.getClient().getOptions().enabled = false; - } else { - Sentry.init({ - // eslint-disable-next-line no-undef - release: __SENTRY_RELEASE__, - // eslint-disable-next-line no-undef - dsn: __SENTRY_DSN__, - ...pluginParams, - }); + return; } + Sentry.init({ + // eslint-disable-next-line no-undef + release: __SENTRY_RELEASE__, + // eslint-disable-next-line no-undef + dsn: __SENTRY_DSN__, + ...pluginParams, + }); window.Sentry = Sentry; // For backwards compatibility }; diff --git a/packages/gatsby/test/gatsby-browser.test.ts b/packages/gatsby/test/gatsby-browser.test.ts index e848784c0362..a3c98524a2fd 100644 --- a/packages/gatsby/test/gatsby-browser.test.ts +++ b/packages/gatsby/test/gatsby-browser.test.ts @@ -16,6 +16,7 @@ jest.mock('@sentry/gatsby', () => { }, }; }); +global.console.warn = jest.fn(); global.console.error = jest.fn(); let tracingAddExtensionMethods = jest.fn(); @@ -55,17 +56,15 @@ describe('onClientEntry', () => { afterEach(() => { delete (window as any).Sentry; delete (window as any).__SENTRY__; + (global.console.warn as jest.Mock).mockClear(); (global.console.error as jest.Mock).mockClear(); }); function setMockedSentryInWindow() { - const sdkOptions = { - enabled: true, - }; (window as any).__SENTRY__ = { hub: { getClient: () => ({ - getOptions: () => sdkOptions, + // Empty object mocking the client }), }, }; @@ -75,6 +74,8 @@ describe('onClientEntry', () => { setMockedSentryInWindow(); onClientEntry(undefined, { plugins: [] }); // eslint-disable-next-line no-console + expect(console.warn).not.toHaveBeenCalled(); + // eslint-disable-next-line no-console expect(console.error).not.toHaveBeenCalled(); expect(sentryInit).not.toHaveBeenCalled(); expect((window as any).Sentry).toBeDefined(); @@ -84,24 +85,27 @@ describe('onClientEntry', () => { setMockedSentryInWindow(); onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' }); // eslint-disable-next-line no-console - expect((console.error as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(` + expect((console.warn as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(` Array [ - "Sentry Logger [Error]: The SDK has been disabled because your Sentry config must live in one place. - Consider moving it all to your \`sentry.config.js\`.", + "Sentry Logger [Warn]: The SDK was initialized in the Sentry config file, but options were found in the Gatsby config. These have been ignored, merge them to the Sentry config if you want to use them. + Learn more about the Gatsby SDK on https://docs.sentry.io/platforms/javascript/guides/gatsby/", ] `); + // eslint-disable-next-line no-console + expect(console.error).not.toHaveBeenCalled(); expect(sentryInit).not.toHaveBeenCalled(); - expect((window as any).__SENTRY__.hub.getClient().getOptions().enabled).toBe(false); expect((window as any).Sentry).toBeDefined(); }); it('not initialized in injected config, without pluginParams', () => { onClientEntry(undefined, { plugins: [] }); // eslint-disable-next-line no-console + expect(console.warn).not.toHaveBeenCalled(); + // eslint-disable-next-line no-console expect((console.error as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(` Array [ - "Sentry Logger [Error]: No config for the Gatsby SDK was found. Learn how to configure it on - https://docs.sentry.io/platforms/javascript/guides/gatsby/", + "Sentry Logger [Error]: No config for the Gatsby SDK was found. + Learn how to configure it on https://docs.sentry.io/platforms/javascript/guides/gatsby/", ] `); expect((window as any).Sentry).not.toBeDefined(); @@ -110,6 +114,8 @@ describe('onClientEntry', () => { it('not initialized in injected config, with pluginParams', () => { onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' }); // eslint-disable-next-line no-console + expect(console.warn).not.toHaveBeenCalled(); + // eslint-disable-next-line no-console expect(console.error).not.toHaveBeenCalled(); expect(sentryInit).toHaveBeenCalledTimes(1); expect(sentryInit.mock.calls[0][0]).toMatchInlineSnapshot(` From 66855525e7a914439b981812fce8007cf7117505 Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 19 Oct 2021 15:57:40 +0200 Subject: [PATCH 6/8] Clarify outdated comment --- packages/gatsby/gatsby-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/gatsby-node.js b/packages/gatsby/gatsby-node.js index 4158add8280d..36d68164ba58 100644 --- a/packages/gatsby/gatsby-node.js +++ b/packages/gatsby/gatsby-node.js @@ -31,7 +31,7 @@ exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { // To configure the SDK, SENTRY_USER_CONFIG is prioritized over `gatsby-config.js`, // since it isn't possible to set non-serializable parameters in the latter. - // Prioritization here means what `init` is being run first. + // Prioritization here means what `init` is run. const configFile = SENTRY_USER_CONFIG.find(file => fs.existsSync(file)); if (!configFile) { return; From 4d634603a1cdc05a627851b19898e5059c1d34fe Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 19 Oct 2021 16:14:18 +0200 Subject: [PATCH 7/8] catch potential erros on `existsSync` --- packages/gatsby/gatsby-node.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/gatsby-node.js b/packages/gatsby/gatsby-node.js index 36d68164ba58..7aca12c61f23 100644 --- a/packages/gatsby/gatsby-node.js +++ b/packages/gatsby/gatsby-node.js @@ -32,7 +32,14 @@ exports.onCreateWebpackConfig = ({ plugins, getConfig, actions }) => { // To configure the SDK, SENTRY_USER_CONFIG is prioritized over `gatsby-config.js`, // since it isn't possible to set non-serializable parameters in the latter. // Prioritization here means what `init` is run. - const configFile = SENTRY_USER_CONFIG.find(file => fs.existsSync(file)); + let configFile = null; + try { + configFile = SENTRY_USER_CONFIG.find(file => fs.existsSync(file)); + } catch (error) { + // Some node versions (like v11) throw an exception on `existsSync` instead of + // returning false. See https://github.com/tschaub/mock-fs/issues/256 + } + if (!configFile) { return; } From 4ce50e0a1551725c25b2e1b93f19f68652ee479c Mon Sep 17 00:00:00 2001 From: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:05:51 +0200 Subject: [PATCH 8/8] Use `forEach` instead of `map` --- packages/gatsby/gatsby-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/gatsby-node.js b/packages/gatsby/gatsby-node.js index 7aca12c61f23..721196b1f943 100644 --- a/packages/gatsby/gatsby-node.js +++ b/packages/gatsby/gatsby-node.js @@ -55,7 +55,7 @@ function injectSentryConfig(config, configFile) { const injectedEntries = {}; // TODO: investigate what entries need the Sentry config injected. // We may want to skip some. - Object.keys(config.entry).map(prop => { + Object.keys(config.entry).forEach(prop => { const value = config.entry[prop]; let injectedValue = value; if (typeof value === 'string') {