Skip to content

Commit

Permalink
Fix the RN bundling in CI servers by checking for CI servers and turn… (
Browse files Browse the repository at this point in the history
#503)

Summary:
**Summary**

*Issue : #355
*Follow up on my old PR : #497

The RN app build process is failing in CI servers due to the always-on file watch of metro. Mostly the CI servers will have a limit on no. of file watch at a time. This is a problem for RN apps with large no. of dependencies, and thus with a large no. of files in `node_modules`. Which cause the build to fail with `ENOSPC` error. This PR will detect the CI servers with [`ci-info`](https://github.com/watson/ci-info/) module and turn off the [file-watching of JestHasteMap in  metro](https://github.com/facebook/metro/blob/f6314e43071ae498b41d9e5579ad4a13941a4baa/packages/metro/src/node-haste/DependencyGraph.js#L95).
~~Also in the [Metro documentation we have a `watch` field](https://facebook.github.io/metro/docs/en/configuration#watch), but it is not working since [this commit ](0d6c135#diff-75dc57ca2d6ab410090adb917784434f). So this PR will also make it working, so that people who wish to turn off the watch can do that with their metro config.~~ And this change is a no-op to current metro users, as it will maintain the default watch behavior.

Also we can manually turn off the file watch by setting the `CI=true` environment variable.

**Motivation**

*Why this fix is required now ?*
This file watching functionality will help in accelerating the local development, but that is not expected when we are building the project to release in our CI pipelines, where the build is happening in some docker containers or some ec2 servers, where sometimes we do not have access to set the `fs.inotify.max_user_watches`. So people try the workarounds like, npm script for deleting the folders in the node_modules which are not required in the bundling process.

**Test plan**
Completing the [metro pull request workflow](https://github.com/facebook/metro/blob/master/CONTRIBUTING.md#workflow-and-pull-requests).
And I tested the functionality by creating a [sample RN app](https://github.com/alanjoxa/AwesomeProject) by applying this [patch](https://github.com/alanjoxa/AwesomeProject/tree/master/patches) to its metro dependencies in node-modules, as part of its [postinstall script](https://github.com/alanjoxa/AwesomeProject/blob/e52b92fd04d61e9ed8530d5fdf7f59657b1a8a2e/package.json#L11). It is working as expected.

**Test simulation instructions**
```
git clone https://github.com/alanjoxa/AwesomeProject.git
cd AwesomeProject
npm install
npm start
```
This will start a metro server and serve the bundles by watching all files in the project root. We can turn off the file watch by restarting it by `CI=true npm start`.
A sample bundle can be triggered by making a GET request at `http://localhost:8081/index.bundle?platform=ios`.
When in

- `npm start` - Metro server will watch for  all files in the project folder, including `node_modules` and apply any file changes to the bundle on the subsequent build request.
- `CI=true npm start` - Will not watch for files, that means we need to restart the server for seeing the file changes.
Pull Request resolved: #503

Differential Revision: D19286561

Pulled By: cpojer

fbshipit-source-id: 68f99f746440ab2e199189bba92489a79e800f5d
  • Loading branch information
alanjoxa authored and facebook-github-bot committed Jan 6, 2020
1 parent 984aab8 commit c90ea3a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
7 changes: 1 addition & 6 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,14 @@ The root folder of your project.
Type: `Array<string>`

Specify any additional (to projectRoot) watch folders, this is used to know which files to watch.
(By default the file watching is disabled in CI environments. Also it can be manually disabled by setting the env variable `CI=true`)

#### `transformerPath`

Type: `string`

The path to the transformer to use.

#### `watch`

Type: `boolean`

Whether we should watch for all files.

#### `reporter`

Type: `{update: () => void}`
Expand Down
1 change: 1 addition & 0 deletions packages/metro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"babel-preset-fbjs": "^3.3.0",
"buffer-crc32": "^0.2.13",
"chalk": "^2.4.1",
"ci-info": "^2.0.0",
"concat-stream": "^1.6.0",
"connect": "^3.6.5",
"debug": "^2.2.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/metro/src/node-haste/DependencyGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Module = require('./Module');
const ModuleCache = require('./ModuleCache');
const ResolutionRequest = require('./DependencyGraph/ResolutionRequest');

const ci = require('ci-info');
const fs = require('fs');
const path = require('path');

Expand Down Expand Up @@ -92,7 +93,7 @@ class DependencyGraph extends EventEmitter {
roots: config.watchFolders,
throwOnModuleCollision: true,
useWatchman: config.resolver.useWatchman,
watch: true,
watch: !ci.isCI,
});
}

Expand Down

0 comments on commit c90ea3a

Please sign in to comment.