Process files sequentially in flow-upgrade runCodemods - #9485
Draft
rootkiller6788 wants to merge 1 commit into
Draft
Process files sequentially in flow-upgrade runCodemods#9485rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
Fixes flow-upgrade codemods writing another file's output when run over multiple files (issue facebook#9407). hermes-transform is not safe to run concurrently: prettier memoizes the loaded parser plugin keyed by a JSON serialization of the plugin options, and hermes-transform's printer wires the current file's AST up through that parser. Because the only per-file difference is the parse closure (which JSON.stringify drops), the cache key collides across files and a file can end up being printed with a different file's AST. The require cache clearing already present only works when files are processed one at a time; under Promise.allSettled the cache reset races between files. Process files sequentially instead, preserving the previous behavior of not aborting the whole run when a single file fails.
|
This pull request has been imported. If you are a Meta employee, you can view this in D116917614. (Because this pull request was imported automatically, there will not be any future comments.) |
| }, | ||
| })); | ||
|
|
||
| describe('runCodemods', () => { |
Contributor
There was a problem hiding this comment.
delete this test. it's only asserting that things are running sequentially which is an impl detail. it just adds blockers once the cache issue is properly fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #9407. When running a codemod over multiple files (e.g.
flow-codemod convertLegacyUtilityTypes), every file was rewritten with the first file's transformed output.Root cause
runCodemodsusedPromise.allSettled(filePaths.map(...))so all files were transformed concurrently.hermes-transformis not safe to use this way:cacheKey: JSON.stringifyof the plugin options.hermes-transform's printer passes a plugin whose only per-call difference is theparseclosure returning the current file's AST.JSON.stringifydrops functions, so the cache key is identical for every file.prettier.formattherefore reuses the first file's parser, which returns the first file's AST, and every file gets printed with the first file's content.Clearing
hermes-transform/prettierfromrequire.cache(the existing workaround) only helps when files are processed one at a time; under concurrent execution the reset races between files.Fix
Process files sequentially. The per-file failure tolerance of
Promise.allSettledis preserved with a try/catch around each file.Added two regression tests:
runCodemods-test.js— verifies each file is transformed independently (the reported bug's exact scenario).runCodemods-sequential-test.js— mockshermes-transformand asserts that transform invocations never overlap.Test plan
npx jest src/__tests__/runCodemods-test.js src/__tests__/runCodemods-sequential-test.js— both pass.npx jest src/— all 14 suites / 91 tests pass.