Process files sequentially in flow-upgrade runCodemods - #9485
Process files sequentially in flow-upgrade runCodemods#9485rootkiller6788 wants to merge 2 commits 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', () => { |
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
Drop runCodemods-sequential-test.js per review feedback. The test only asserts that files are processed one at a time, which is an implementation detail of the current workaround for the hermes-transform plugin cache collision (facebook#9407). It would block a proper fix of the root cause, where files could again be transformed concurrently. The output-correctness regression test (runCodemods-test.js) still covers the user-visible bug: each file must be written with its own transformed contents, not another file's.
|
Thank you for the review. Fixed as suggested — I've deleted You're right that the test only asserted files are processed one at a time, which is an implementation detail of the current workaround for the hermes-transform/prettier plugin cache collision (#9407). Once that cache issue is fixed properly at the root, files could again be transformed concurrently, and this test would only add blockers. The behavior-level regression coverage stays in Could you please take another look? |
|
This pull request has been merged in 4c3c38f. |
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.