diff --git a/v-next/hardhat/src/internal/cli/telemetry/sentry/anonymizer.ts b/v-next/hardhat/src/internal/cli/telemetry/sentry/anonymizer.ts index bd69aaf4b0..22105d70e8 100644 --- a/v-next/hardhat/src/internal/cli/telemetry/sentry/anonymizer.ts +++ b/v-next/hardhat/src/internal/cli/telemetry/sentry/anonymizer.ts @@ -124,7 +124,7 @@ export class Anonymizer { public anonymizeErrorMessage(errorMessage: string): string { errorMessage = this.#anonymizeMnemonic(errorMessage); - // the \\ before path.sep is necessary for this to work on windows + // Match path separators both for Windows and Unix const pathRegex = /\S+[\/\\]\S+/g; // for files that don't have a path separator @@ -161,6 +161,10 @@ export class Anonymizer { continue; } + if (this.#errorRaisedByPackageToIgnore(frame.filename)) { + return false; + } + // we stop after finding either a hardhat file or a file from the user's // project if (this.#isHardhatFile(frame.filename)) { @@ -197,15 +201,25 @@ export class Anonymizer { } } - #isHardhatFile(filename: string): boolean { - const totNodeModules = filename.split("node_modules").length - 1; + #errorRaisedByPackageToIgnore(filename: string): boolean { + const pkgsToIgnore: string[] = [ + path.join("node_modules", "@random-npm-package"), // TODO: add real packages + ]; - const nomicFoundationPath = path.join("node_modules", "@nomicfoundation"); - if (filename.startsWith(nomicFoundationPath) && totNodeModules === 1) { - return true; + const pkgs = filename.match(/node_modules[\/\\][^\/\\]+/g); // Match path separators both for Windows and Unix + + if (pkgs === null) { + return false; } - return false; + const errorSourcePkg = pkgs[pkgs.length - 1]; + + return pkgsToIgnore.includes(errorSourcePkg); + } + + #isHardhatFile(filename: string): boolean { + const nomicFoundationPath = path.join("node_modules", "@nomicfoundation"); + return filename.startsWith(nomicFoundationPath); } async #anonymizeExceptions(exceptions: Exception[]): Promise { diff --git a/v-next/hardhat/test/internal/cli/telemetry/sentry/anonymizer.ts b/v-next/hardhat/test/internal/cli/telemetry/sentry/anonymizer.ts index 76913890ba..16c91a17cd 100644 --- a/v-next/hardhat/test/internal/cli/telemetry/sentry/anonymizer.ts +++ b/v-next/hardhat/test/internal/cli/telemetry/sentry/anonymizer.ts @@ -434,6 +434,8 @@ describe("Anonymizer", () => { "node_modules", "@nomicfoundation", "node_modules", + "@nomicfoundation-pkg2", + "node_modules", "@random-npm-package", "some-file.js", ),