Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program instance for custom transformers becomes stale #1216

Closed
andersekdahl opened this issue Dec 2, 2020 · 2 comments · Fixed by #1352
Closed

Program instance for custom transformers becomes stale #1216

andersekdahl opened this issue Dec 2, 2020 · 2 comments · Fixed by #1352

Comments

@andersekdahl
Copy link
Contributor

Expected Behaviour

This should not throw an error:

export function transformer(program: ts.Program) {
  return (context: ts.TransformationContext) => (file: ts.SourceFile) => {
    if (program.getSourceFile(file.fileName).getText() !== file.getText()) {
      throw new Error();
    }
  };
}

Actual Behaviour

The files in the program instance passed in becomes stale when you have a watch running, and program.getSourceFile() will return the file as it looked when the watch started.

I was the one who added the program argument to getCustomTransformers so I'm the one to blame here. 😄 To avoid a breaking change I think we could change it from:

(program: Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; }

to:

(program: Program, getProgram: () => Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; }

And in a new major remove the passed in program and just pass a function that gets the latest program instance.

@johnnyreilly
Copy link
Member

Yeah that sounds like a plan. If you'd like to raise a PR I'll take a look.

@Zn4rK
Copy link
Contributor

Zn4rK commented Aug 10, 2021

It's worth noting that with the proposed changes in #1350 it would still give a stale program for configurations using transpileOnly-flag. Not really sure how to combat that though.

Edit: Before #1350, the program didn't contain any files, so one could argue that this is better, but ideally, I would like to find a solution to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants