Skip to content

Commit 72ca5c2

Browse files
authored
[build-tools] Apply npm cache registry only to the dependency install (#3925)
* [build-tools] Apply npm cache registry only to the dependency install * cache install test * inline env, remove deprecated env * use cache url as signal
1 parent 8aa4cae commit 72ca5c2

4 files changed

Lines changed: 68 additions & 39 deletions

File tree

packages/build-tools/src/common/__tests__/installDependencies.test.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
4040
env: {
4141
EAS_VERBOSE: '1',
4242
EAS_USE_NPM_CACHE: '1',
43-
NPM_CONFIG_REGISTRY: npmCacheUrl,
43+
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
4444
},
4545
logger,
4646
cwd: '/tmp/build',
@@ -56,6 +56,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
5656
env: {
5757
EAS_VERBOSE: '1',
5858
EAS_USE_NPM_CACHE: '1',
59+
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
5960
NPM_CONFIG_REGISTRY: npmCacheUrl,
6061
},
6162
});
@@ -66,6 +67,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
6667
env: {
6768
EAS_VERBOSE: '1',
6869
EAS_USE_NPM_CACHE: '1',
70+
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
6971
},
7072
});
7173
expect(logger.warn).toHaveBeenCalledWith(
@@ -110,7 +112,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
110112
packageManager: PackageManager.NPM,
111113
env: {
112114
EAS_USE_NPM_CACHE: '1',
113-
NPM_CONFIG_REGISTRY: npmCacheUrl,
115+
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
114116
},
115117
logger,
116118
cwd: '/tmp/build',
@@ -158,7 +160,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
158160
packageManager: PackageManager.NPM,
159161
env: {
160162
EAS_USE_NPM_CACHE: '1',
161-
NPM_CONFIG_REGISTRY: 'http://npm.staging.caches.eas-build.internal',
163+
EAS_BUILD_NPM_CACHE_URL: 'http://npm.staging.caches.eas-build.internal',
162164
},
163165
logger,
164166
cwd: '/tmp/build',
@@ -170,7 +172,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
170172
expect(Sentry.capture).not.toHaveBeenCalled();
171173
});
172174

173-
it('does not retry when EAS_USE_NPM_CACHE is not enabled', async () => {
175+
it('does not retry when the npm cache is not enabled', async () => {
174176
const logger = createMockLogger();
175177
const npmCacheUrl = 'http://npm.staging.caches.eas-build.internal';
176178
const error = Object.assign(
@@ -188,9 +190,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
188190
await expect(
189191
installDependenciesWithNpmCacheFallbackAsync({
190192
packageManager: PackageManager.NPM,
191-
env: {
192-
NPM_CONFIG_REGISTRY: npmCacheUrl,
193-
},
193+
env: {},
194194
logger,
195195
cwd: '/tmp/build',
196196
useFrozenLockfile: false,
@@ -200,6 +200,40 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
200200
expect(spawn).toHaveBeenCalledTimes(1);
201201
expect(Sentry.capture).not.toHaveBeenCalled();
202202
});
203+
204+
it('installs through the npm cache registry without retrying when the cache install succeeds', async () => {
205+
const logger = createMockLogger();
206+
const npmCacheUrl = 'http://npm.staging.caches.eas-build.internal';
207+
208+
jest
209+
.mocked(spawn)
210+
.mockReturnValueOnce(createSpawnPromise(Promise.resolve(createSpawnResult())));
211+
212+
await installDependenciesWithNpmCacheFallbackAsync({
213+
packageManager: PackageManager.NPM,
214+
env: {
215+
EAS_USE_NPM_CACHE: '1',
216+
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
217+
},
218+
logger,
219+
cwd: '/tmp/build',
220+
useFrozenLockfile: false,
221+
});
222+
223+
expect(spawn).toHaveBeenCalledTimes(1);
224+
expect(spawn).toHaveBeenCalledWith('npm', ['install', '--include=dev'], {
225+
cwd: '/tmp/build',
226+
logger,
227+
infoCallbackFn: undefined,
228+
lineTransformer: expect.any(Function),
229+
env: {
230+
EAS_USE_NPM_CACHE: '1',
231+
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
232+
NPM_CONFIG_REGISTRY: npmCacheUrl,
233+
},
234+
});
235+
expect(Sentry.capture).not.toHaveBeenCalled();
236+
});
203237
});
204238

205239
function createSpawnPromise(result: Promise<SpawnResult>): SpawnPromise<SpawnResult> {

packages/build-tools/src/common/installDependencies.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,36 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
9393
infoCallbackFn?: SpawnOptions['infoCallbackFn'];
9494
useFrozenLockfile: boolean;
9595
}): Promise<void> {
96-
const npmCacheUrl = env.NPM_CONFIG_REGISTRY;
96+
const npmCacheUrl = env.EAS_BUILD_NPM_CACHE_URL;
97+
98+
if (!npmCacheUrl) {
99+
await (
100+
await installDependenciesAsync({
101+
packageManager,
102+
env,
103+
logger,
104+
infoCallbackFn,
105+
cwd,
106+
useFrozenLockfile,
107+
})
108+
).spawnPromise;
109+
return;
110+
}
111+
112+
logger.info(`Installing dependencies using the npm cache registry (${npmCacheUrl}).`);
113+
97114
let firstErrorLine: string | undefined;
98115
let errorLineCount = 0;
99116

100117
try {
101118
await (
102119
await installDependenciesAsync({
103120
packageManager,
104-
env,
121+
env: { ...env, NPM_CONFIG_REGISTRY: npmCacheUrl },
105122
logger,
106123
infoCallbackFn,
107124
lineTransformer: (line: string) => {
108-
if (isNpmCacheRegistryErrorLine(line, { env, npmCacheUrl })) {
125+
if (isNpmCacheRegistryErrorLine(line, npmCacheUrl)) {
109126
firstErrorLine ??= line;
110127
errorLineCount += 1;
111128
}
@@ -124,7 +141,7 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
124141
});
125142
}
126143
} catch (err: unknown) {
127-
if (!isNpmCacheInstallFailure(err, { env, npmCacheUrl })) {
144+
if (!isNpmCacheInstallFailure(err, npmCacheUrl)) {
128145
throw err;
129146
}
130147

@@ -144,11 +161,10 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
144161
},
145162
});
146163

147-
const { NPM_CONFIG_REGISTRY: _NPM_CONFIG_REGISTRY, ...fallbackEnv } = env;
148164
await (
149165
await installDependenciesAsync({
150166
packageManager,
151-
env: fallbackEnv,
167+
env,
152168
logger,
153169
infoCallbackFn,
154170
cwd,
@@ -172,35 +188,17 @@ export function resolvePackagerDir(ctx: BuildContext<Job>): string {
172188
return packagerRunDir;
173189
}
174190

175-
function isNpmCacheInstallFailure(
176-
err: unknown,
177-
{ env, npmCacheUrl }: { env: Record<string, string | undefined>; npmCacheUrl: string | undefined }
178-
): boolean {
179-
if (!isNpmCacheRegistryEnabled(env, npmCacheUrl)) {
180-
return false;
181-
}
182-
const errorOutput = getErrorOutput(err);
183-
return errorOutput.includes(npmCacheUrl);
191+
function isNpmCacheInstallFailure(err: unknown, npmCacheUrl: string): boolean {
192+
return getErrorOutput(err).includes(npmCacheUrl);
184193
}
185194

186-
function isNpmCacheRegistryErrorLine(
187-
line: string,
188-
{ env, npmCacheUrl }: { env: Record<string, string | undefined>; npmCacheUrl: string | undefined }
189-
): boolean {
195+
function isNpmCacheRegistryErrorLine(line: string, npmCacheUrl: string): boolean {
190196
return (
191-
isNpmCacheRegistryEnabled(env, npmCacheUrl) &&
192197
line.includes(npmCacheUrl) &&
193198
/(?:error|failed|ENOTFOUND|ECONN|ETIMEDOUT|EAI_AGAIN|FetchError)/i.test(line)
194199
);
195200
}
196201

197-
function isNpmCacheRegistryEnabled(
198-
env: Record<string, string | undefined>,
199-
npmCacheUrl: string | undefined
200-
): npmCacheUrl is string {
201-
return env.EAS_USE_NPM_CACHE === '1' && !!npmCacheUrl;
202-
}
203-
204202
function getErrorOutput(err: unknown): string {
205203
if (!(err instanceof Error)) {
206204
return '';

packages/worker/src/__unit__/env.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ describe(getBuildEnv.name, () => {
268268
buildId: 'build-id',
269269
});
270270

271-
expect(env.NPM_CACHE_URL).toBe('https://npm.example');
272-
expect(env.NPM_CONFIG_REGISTRY).toBe('https://npm.example');
271+
expect(env.NPM_CACHE_URL).toBeUndefined();
272+
expect(env.NPM_CONFIG_REGISTRY).toBeUndefined();
273273
expect(env.EAS_BUILD_NPM_CACHE_URL).toBe('https://npm.example');
274274
expect(env.NVM_NODEJS_ORG_MIRROR).toBe('https://node.example');
275275
expect(env.EAS_BUILD_MAVEN_CACHE_URL).toBe('https://maven.example');

packages/worker/src/env.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ export function getBuildEnv({
3636
setEnv(env, 'EAS_BUILD_RUNNER', 'eas-build');
3737
setEnv(env, 'EAS_BUILD_PLATFORM', job.platform);
3838
setEnv(env, 'EAS_CLI_SENTRY_DSN', config.sentry.dsn);
39-
// NPM_CACHE_URL is deprecated
4039
const npmCacheUrl = RuntimeSettings.getNpmCacheUrl();
4140
const nodeJsCacheUrl = RuntimeSettings.getNodeJsCacheUrl();
4241
const mavenCacheUrl = RuntimeSettings.getMavenCacheUrl();
4342
const cocoapodsCacheUrl = RuntimeSettings.getCocoapodsCacheUrl();
4443

45-
setEnv(env, 'NPM_CACHE_URL', npmCacheUrl);
46-
setEnv(env, 'NPM_CONFIG_REGISTRY', npmCacheUrl);
4744
setEnv(env, 'NVM_NODEJS_ORG_MIRROR', nodeJsCacheUrl);
4845
setEnv(env, 'EAS_BUILD_NPM_CACHE_URL', npmCacheUrl);
4946
setEnv(env, 'EAS_BUILD_PROFILE', metadata.buildProfile);

0 commit comments

Comments
 (0)