Skip to content

fix(error-reporters): Store diagnostics in TransformOutput#10027

Merged
kdy1 merged 12 commits intoswc-project:mainfrom
GiveMe-A-Name:fix/emit_warning
Feb 20, 2025
Merged

fix(error-reporters): Store diagnostics in TransformOutput#10027
kdy1 merged 12 commits intoswc-project:mainfrom
GiveMe-A-Name:fix/emit_warning

Conversation

@GiveMe-A-Name
Copy link
Collaborator

@GiveMe-A-Name GiveMe-A-Name commented Feb 12, 2025

Description:

SWC will return a Result<Error> to js, then js console message then throw error, when swc apply pass occur errors.
But SWC would not print warning message if emit_handler was only had warning message. So we print warning message in rust.

In the future, It might be more appropriate to let rust print the information and let the js side end the process

BREAKING CHANGE:

Related issue (if exists):
#9887

@GiveMe-A-Name GiveMe-A-Name requested a review from a team as a code owner February 12, 2025 09:47
@changeset-bot
Copy link

changeset-bot bot commented Feb 12, 2025

⚠️ No Changeset found

Latest commit: 66f3611

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@GiveMe-A-Name GiveMe-A-Name changed the title fix(swc_error_reporters): Add warning message emit and related methods fix(swc_error_reporters): add warning message emit and related methods Feb 12, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 12, 2025

CodSpeed Performance Report

Merging #10027 will degrade performances by 5.92%

Comparing GiveMe-A-Name:fix/emit_warning (66f3611) with main (c75578b)

Summary

⚡ 2 improvements
❌ 3 regressions
✅ 191 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
es/full/minify/libraries/jquery 167 ms 173.5 ms -3.76%
es/full/minify/libraries/lodash 190.2 ms 184.6 ms +3%
es/minifier/libs/jquery 137.4 ms 131.5 ms +4.49%
es/minifier/libs/moment 82.1 ms 87.3 ms -5.92%
es/minifier/libs/vue 188.4 ms 194.7 ms -3.22%

@kdy1 kdy1 added this to the Planned milestone Feb 12, 2025
@GiveMe-A-Name GiveMe-A-Name requested a review from a team as a code owner February 17, 2025 08:44
@GiveMe-A-Name GiveMe-A-Name changed the title fix(swc_error_reporters): add warning message emit and related methods fix(swc_error_reporters): emit diagnostics to transformOutput Feb 17, 2025
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is inevitably breaking change, so I think it's overly complicated.

@GiveMe-A-Name
Copy link
Collaborator Author

According to Handler code, user's plugin can emit error or warning to Emitter by handler. Then try_with_handler would create a pretty_emitter and return Error if handler has errors. pretty_emitter can't handle Ret to add warning.
I can found two way emit diagnostics to TransformOutput

  1. define a trait on Ret, and call it in try_with_handler
pub trait DiagnosticEmitter {
     emit(&mut self, d: Diagnostic);
 }

pub fn try_with_handler<F, Ret>(
    cm: Lrc<SourceMap>,
    config: HandlerOpts,
    op: F,
) -> Result<Ret, Error>
where
    Ret: DiagnosticEmitter,
    F: FnOnce(&Handler) -> Result<Ret, Error>,
{  
   let emitter = PrettyEmitter::new(
        cm,
        wr.clone(),
        to_miette_reporter(config.color),
        PrettyEmitterConfig {
            skip_filename: config.skip_filename,
        },
    );
    
    ....... 
    
    for d in emitter.take_diagnostics { 
          ret.emit(d);
    }
}
  1. pass diagnostics go though Handler that a global variable.
transformOutput.diagnostic = handler.take_diagnostics();

impl Handler {
  pub fn take_diagnostics(&self) {
      self.emitter.take_diagnositc.
  }
}

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the effort! I think this PR is fine to be merged, but please tell me if you want to add json support to this PR.

}
}

pub fn take_diagnostics(&self) -> Vec<String> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds good. Nice work!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, you can merge it at any time.

Can we have an option to set the formatting of error messages?

About option to set the formatting of error messages, i can implement in another PR.

@kdy1 kdy1 changed the title fix(swc_error_reporters): emit diagnostics to transformOutput fix(error-reporters): Store diagnostics in TransformOutput Feb 20, 2025
@kdy1 kdy1 merged commit 52caf23 into swc-project:main Feb 20, 2025
173 checks passed
@kdy1 kdy1 modified the milestones: Planned, v1.11.1 Feb 25, 2025
@swc-project swc-project locked as resolved and limited conversation to collaborators Mar 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants