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

fix(cache): invalidate codeCache in most cases when imports change #369

Merged

Conversation

agilgur5
Copy link
Collaborator

@agilgur5 agilgur5 commented Jun 26, 2022

Summary

The codeCache should be invalidated when imports change if declarations are needed, because imports' types can change the resulting declaration.

EDIT: also added !isolatedModules to the check, see below comments regarding, e.g. enums, that could cause compiled JS code to change as well when imports change

Details

  • previously, the checkImports flag was set to true for type-checking, but false for compilation
    • I believe it is false because the compiled JS shouldn't change if an import changes
      • though I'm not sure if that was the original intent behind the code
    • problematically though, compilation results can include declarations, and those can change if imports change
      • for instance, the types of an import can change the declaration that is output
    • so now, only set it to false for compilation if declarations are not needed

See my root cause analysis in #292 (comment)

- previously, the `checkImports` flag was set to `true` for type-checking, but `false` for compilation
  - I _believe_ it is `false` because the compiled JS shouldn't change if an import changes
    - though I'm not sure if that was the original intent behind the code
  - problematically though, compilation results can include declarations, and those _can_ change if imports change
    - for instance, the types of an import can change the declaration that is output
  - so now, only set it to `false` for compilation if declarations are _not_ needed
@agilgur5 agilgur5 added kind: bug Something isn't working properly scope: cache Related to the cache labels Jun 26, 2022
@ezolenko
Copy link
Owner

ezolenko commented Jun 29, 2022

I don't remember why it is this way, but I think your change is correct.

Now that I look at it closer, maybe we should always check imports regardless -- there might be cases where typescript generates different code based on what was imported. Maybe inlining enums into literals? And if it doesn't now, it can start doing that in the future.

@agilgur5
Copy link
Collaborator Author

Maybe inlining enums into literals?

OH, that's actually the perfect edge case! I was trying to think of an edge-case where checking imports would be necessary for just compiled JS code (i.e. not declarations) and couldn't come up with one. But I did suspect that there were other edge-cases.

In the RCA I compared it to Babel:

This seems to be because the JS code output shouldn't change if an import changes. That, I believe, is accurate. That's one of the reasons why Babel can just transpile TS code per-file (whereas type-checking requires knowledge of the whole "Program", i.e. the whole module graph).

And, of course, enums are one of those things that Babel can't handle!

TS does have a check for this though, isolatedModules.

So what I can do is checkImports if !isolatedModules || declaration. That's going to be the majority of cases, tbh, but it leaves in some potential for optimization.

Could make that a separate PR though, as checking declaration is still certainly needed

- ezolenko gave a good example of enums, which can indeed cause the compiled JS code to change based on imports
  - so also check `!isolatedModules` as well
  - I thought it might be the case that the code wouldn't handle other edge cases, but couldn't think of one off the top of my head
    - ironically, I compared it to Babel, which transpiles per file, and Babel _requires_ `isolatedModules` to work
@agilgur5
Copy link
Collaborator Author

agilgur5 commented Jul 2, 2022

added another commit to handle !isolatedModules case as well. modified the PR summary and title to match this as well.

think this is good to go now!

@agilgur5 agilgur5 changed the title fix(cache): invalidate declarations when imports change fix(cache): invalidate codeCache in most cases when imports change Jul 2, 2022
@ezolenko ezolenko merged commit 8334c9b into ezolenko:master Jul 5, 2022
@agilgur5 agilgur5 deleted the fix-cache-checkImports-compiled-declaration branch July 2, 2023 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Something isn't working properly scope: cache Related to the cache
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache not invalidating declarations when import changes (need to use clean: true)
2 participants