Skip to content

Commit

Permalink
Fix mismatched metro-transform-worker version - use absolute worker…
Browse files Browse the repository at this point in the history
… paths

Summary:
Issues such as #1017 (and similar previously) have had users running into broken setups due to "multiple versions of Metro" installed.

Ideally, multiple Metros wouldn't be a problem. Metro packages already specify explicit, pinned dependencies on each other, and well-behaved package resolutions should stay within those disconnected graphs of Metro versions.

In particular `metro` already specifies its dependency on `metro-transform-worker`, so it wasn't obvious how we were "escaping" from the correct `metro` and ending up inside the wrong `metro-transform-worker` here. I was able to reproduce #1017 with Berry and debug it.

It turns out that the problem is the use of the (undocumented) `transformer.workerPath` option, which defaults to `'metro/src/DeltaBundler/Worker'`. We pass that to `jest-worker`, which forks a child process `jest-worker/build/workers/processChild`, which resolves the given path *relative to `jest-worker`* - so usually resolves the *hoisted* `metro`, whatever that happens to be.

This fixes the issue by resolving the path from the parent process, within `metro`, and passing an absolute path to `jest-worker`.

Changelog:
```
 - **[Fix]**: Incorrect worker resolution when multiple `metro` versions are installed.
```

Reviewed By: huntie

Differential Revision: D47209874

fbshipit-source-id: 5aa101852e9d33af6f41c118252d9874701a01be
  • Loading branch information
robhogan authored and facebook-github-bot committed Jul 4, 2023
1 parent 4d4d7e8 commit 6d46078
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/metro/src/DeltaBundler/WorkerFarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class WorkerFarm {
constructor(config: ConfigT, transformerConfig: TransformerConfig) {
this._config = config;
this._transformerConfig = transformerConfig;
const absoluteWorkerPath = require.resolve(config.transformer.workerPath);

if (this._config.maxWorkers > 1) {
const worker = this._makeFarm(
this._config.transformer.workerPath,
absoluteWorkerPath,
['transform'],
this._config.maxWorkers,
);
Expand Down Expand Up @@ -107,7 +108,7 @@ class WorkerFarm {
}

_makeFarm(
workerPath: string,
absoluteWorkerPath: string,
exposedMethods: $ReadOnlyArray<string>,
numWorkers: number,
): any {
Expand All @@ -117,7 +118,7 @@ class WorkerFarm {
FORCE_COLOR: 1,
};

return new JestWorker(workerPath, {
return new JestWorker(absoluteWorkerPath, {
computeWorkerKey: this._config.stickyWorkers
? // $FlowFixMe[method-unbinding] added when improving typing for this parameters
// $FlowFixMe[incompatible-call]
Expand Down

0 comments on commit 6d46078

Please sign in to comment.