From f62ac54f2950df55739f10afb5f921d8e9e42f94 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Fri, 1 Jul 2022 23:02:25 -0400 Subject: [PATCH 1/2] fix(diagnostics): workaround Rollup duplicating error messages - per my investigation in the linked issues, it seems like Rollup has a bug where it duplicates some error message - this occurs when the error has a stack (or frame) which contains the error message itself - Rollup prints _both_ the error message _and_ the stack in that case, causing duplication - this fix adds a workaround for this upstream Rollup bug - it detects if there is a stack and if the message is duplicated in the stack - if so, it removes the duplication in the stack - this workaround should be forward-compatible if this behavior is fixed upstream - this code should just end up re-throwing in that case (effectively a no-op) --- src/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/index.ts b/src/index.ts index 30352ab7..e5d6b945 100644 --- a/src/index.ts +++ b/src/index.ts @@ -253,6 +253,19 @@ const typescript: PluginImpl = (options) => return transformResult; }, + buildEnd(err) + { + if (!err) + return + + // workaround: err.stack contains err.message and Rollup prints both, causing duplication, so split out the stack itself if it exists (c.f. https://github.com/ezolenko/rollup-plugin-typescript2/issues/103#issuecomment-1172820658) + const stackOnly = err.stack?.split(err.message)[1]; + if (stackOnly) + this.error({ message: err.message, stack: stackOnly }); + else + this.error(err); + }, + generateBundle(bundleOptions) { self._ongenerate(); From d3faa2eababcee053791e6eadcad4bd4e9ceef07 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 2 Jul 2022 00:36:53 -0400 Subject: [PATCH 2/2] fix watch mode by spreading err - apparently Rollup attaches several properties to the error object, including `watchFiles` - so removing them / not spreading causes watch to just stop working here are some of the additional properties I logged out, for example: ```js { id: '/project-dir/src/difference.ts', hook: 'transform', code: 'PLUGIN_ERROR', plugin: 'rpt2', watchFiles: [ '/project-dir/src/index.ts', '/project-dir/tsconfig.json', '/project-dir/src/types.ts', '/project-dir/src/difference.ts' ] } } ``` --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index e5d6b945..31dd6edf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -261,7 +261,7 @@ const typescript: PluginImpl = (options) => // workaround: err.stack contains err.message and Rollup prints both, causing duplication, so split out the stack itself if it exists (c.f. https://github.com/ezolenko/rollup-plugin-typescript2/issues/103#issuecomment-1172820658) const stackOnly = err.stack?.split(err.message)[1]; if (stackOnly) - this.error({ message: err.message, stack: stackOnly }); + this.error({ ...err, message: err.message, stack: stackOnly }); else this.error(err); },