From f2697bbf26018947a1d9f81cacb9243d9ad967a4 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 16 May 2023 17:32:39 +0200 Subject: [PATCH 1/5] fix(nextjs): Inject init code via relative path --- packages/nextjs/src/config/loaders/wrappingLoader.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index 28c8f58b2eb9..6aca94ec1826 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -79,6 +79,8 @@ export default function wrappingLoader( vercelCronsConfig, } = 'getOptions' in this ? this.getOptions() : this.query; + this.resourcePath; + this.async(); let templateCode: string; @@ -202,8 +204,10 @@ export default function wrappingLoader( templateCode = templateCode.replace(/__COMPONENT_TYPE__/g, 'Unknown'); } - if (sentryConfigFilePath) { - templateCode = `import "${sentryConfigFilePath}";`.concat(templateCode); + if (sentryConfigFilePath && path.isAbsolute(this.resourcePath)) { + // We need the import to be relative because webpack cannot process any absolute paths on windows: + // "" + templateCode = `import "${path.relative(this.resourcePath, sentryConfigFilePath)}";`.concat(templateCode); } } else if (wrappingTargetKind === 'middleware') { templateCode = middlewareWrapperTemplateCode; From 2c2537b831bc25bb9cb71253b714e3b3ffdb02f2 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 16 May 2023 17:42:03 +0200 Subject: [PATCH 2/5] . --- packages/nextjs/src/config/loaders/wrappingLoader.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index 6aca94ec1826..f8c5d325ed70 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -79,8 +79,6 @@ export default function wrappingLoader( vercelCronsConfig, } = 'getOptions' in this ? this.getOptions() : this.query; - this.resourcePath; - this.async(); let templateCode: string; From 34b9b35a24e3236d383bcce0b3c144f349fbd824 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 16 May 2023 17:59:24 +0200 Subject: [PATCH 3/5] test log --- packages/nextjs/src/config/loaders/wrappingLoader.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index f8c5d325ed70..67ddf0b9b311 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -203,8 +203,12 @@ export default function wrappingLoader( } if (sentryConfigFilePath && path.isAbsolute(this.resourcePath)) { - // We need the import to be relative because webpack cannot process any absolute paths on windows: - // "" + console.log('TEST LOG', { + sentryConfigFilePath, + rp: this.resourcePath, + imp: path.relative(this.resourcePath, sentryConfigFilePath), + }); + // We need the import to be relative because webpack cannot process any absolute paths on windows templateCode = `import "${path.relative(this.resourcePath, sentryConfigFilePath)}";`.concat(templateCode); } } else if (wrappingTargetKind === 'middleware') { From 700776262ede299518482d236af83fda5cd016a7 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 17 May 2023 12:34:13 +0200 Subject: [PATCH 4/5] hm --- packages/nextjs/src/config/loaders/wrappingLoader.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index 67ddf0b9b311..d83d4e3ccf52 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -203,13 +203,12 @@ export default function wrappingLoader( } if (sentryConfigFilePath && path.isAbsolute(this.resourcePath)) { - console.log('TEST LOG', { - sentryConfigFilePath, - rp: this.resourcePath, - imp: path.relative(this.resourcePath, sentryConfigFilePath), - }); + const sentryConfigFileImportPath = path + .relative(this.resourcePath, sentryConfigFilePath) // Get path relative to current module because webpack can't handle absolute paths on windows: https://github.com/getsentry/sentry-javascript/issues/8133 + .replace(/\.[^/.]+$/, ''); // Remove the file extension from the import + // We need the import to be relative because webpack cannot process any absolute paths on windows - templateCode = `import "${path.relative(this.resourcePath, sentryConfigFilePath)}";`.concat(templateCode); + templateCode = `import "${sentryConfigFileImportPath}";`.concat(templateCode); } } else if (wrappingTargetKind === 'middleware') { templateCode = middlewareWrapperTemplateCode; From b56aa7d79b52436063c3c6fbd0207214313ed2cc Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 17 May 2023 13:08:36 +0200 Subject: [PATCH 5/5] . --- packages/nextjs/src/config/loaders/wrappingLoader.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index d83d4e3ccf52..3f69b79faaa2 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -202,12 +202,10 @@ export default function wrappingLoader( templateCode = templateCode.replace(/__COMPONENT_TYPE__/g, 'Unknown'); } + // We check that `this.resourcePath` is absolte because webpack doesn't really have a contract for what the value looks like. It could basically be anything. if (sentryConfigFilePath && path.isAbsolute(this.resourcePath)) { - const sentryConfigFileImportPath = path - .relative(this.resourcePath, sentryConfigFilePath) // Get path relative to current module because webpack can't handle absolute paths on windows: https://github.com/getsentry/sentry-javascript/issues/8133 - .replace(/\.[^/.]+$/, ''); // Remove the file extension from the import - - // We need the import to be relative because webpack cannot process any absolute paths on windows + // Get path relative to current module because webpack can't handle absolute paths on windows: https://github.com/getsentry/sentry-javascript/issues/8133 + const sentryConfigFileImportPath = path.relative(path.dirname(this.resourcePath), sentryConfigFilePath); templateCode = `import "${sentryConfigFileImportPath}";`.concat(templateCode); } } else if (wrappingTargetKind === 'middleware') {