Skip to content

Commit 3dd4a58

Browse files
authored
fix(nextjs): Guard for non-absolute paths when injecting sentry config (#8151)
1 parent ea551c9 commit 3dd4a58

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

packages/nextjs/src/config/loaders/wrappingLoader.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,13 @@ export default function wrappingLoader(
202202
templateCode = templateCode.replace(/__COMPONENT_TYPE__/g, 'Unknown');
203203
}
204204

205-
if (sentryConfigFilePath) {
206-
let importPath = sentryConfigFilePath;
207-
208-
// absolute paths do not work with Windows
209-
// https://github.com/getsentry/sentry-javascript/issues/8133
210-
if (path.isAbsolute(importPath)) {
211-
importPath = path.relative(path.dirname(this.resourcePath), importPath);
212-
}
213-
214-
templateCode = `import "${importPath.replace(/\\/g, '/')}";\n`.concat(templateCode);
205+
// We check whether `this.resourcePath` is absolute because there is no contract by webpack that says it is absolute,
206+
// however we can only create relative paths to the sentry config from absolute paths.Examples where this could possibly be non - absolute are virtual modules.
207+
if (sentryConfigFilePath && path.isAbsolute(this.resourcePath)) {
208+
const sentryConfigImportPath = path
209+
.relative(path.dirname(this.resourcePath), sentryConfigFilePath) // Absolute paths do not work with Windows: https://github.com/getsentry/sentry-javascript/issues/8133
210+
.replace(/\\/g, '/');
211+
templateCode = `import "${sentryConfigImportPath}";\n`.concat(templateCode);
215212
}
216213
} else if (wrappingTargetKind === 'middleware') {
217214
templateCode = middlewareWrapperTemplateCode;

0 commit comments

Comments
 (0)