Compare commits

..
Author SHA1 Message Date
Gabe Debes bf57252ee4 [worker] warn on unknown build environment 2026-07-03 16:43:55 -07:00
8 changed files with 138 additions and 202 deletions

No files matched your search

@@ -40,7 +40,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
env: {
EAS_VERBOSE: '1',
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
NPM_CONFIG_REGISTRY: npmCacheUrl,
},
logger,
cwd: '/tmp/build',
@@ -56,7 +56,6 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
env: {
EAS_VERBOSE: '1',
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
NPM_CONFIG_REGISTRY: npmCacheUrl,
},
});
@@ -67,7 +66,6 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
env: {
EAS_VERBOSE: '1',
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
},
});
expect(logger.warn).toHaveBeenCalledWith(
@@ -112,7 +110,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
packageManager: PackageManager.NPM,
env: {
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
NPM_CONFIG_REGISTRY: npmCacheUrl,
},
logger,
cwd: '/tmp/build',
@@ -160,7 +158,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
packageManager: PackageManager.NPM,
env: {
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: 'http://npm.staging.caches.eas-build.internal',
NPM_CONFIG_REGISTRY: 'http://npm.staging.caches.eas-build.internal',
},
logger,
cwd: '/tmp/build',
@@ -172,7 +170,7 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
expect(Sentry.capture).not.toHaveBeenCalled();
});
it('does not retry when the npm cache is not enabled', async () => {
it('does not retry when EAS_USE_NPM_CACHE is not enabled', async () => {
const logger = createMockLogger();
const npmCacheUrl = 'http://npm.staging.caches.eas-build.internal';
const error = Object.assign(
@@ -190,7 +188,9 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
await expect(
installDependenciesWithNpmCacheFallbackAsync({
packageManager: PackageManager.NPM,
env: {},
env: {
NPM_CONFIG_REGISTRY: npmCacheUrl,
},
logger,
cwd: '/tmp/build',
useFrozenLockfile: false,
@@ -200,40 +200,6 @@ describe(installDependenciesWithNpmCacheFallbackAsync, () => {
expect(spawn).toHaveBeenCalledTimes(1);
expect(Sentry.capture).not.toHaveBeenCalled();
});
it('installs through the npm cache registry without retrying when the cache install succeeds', async () => {
const logger = createMockLogger();
const npmCacheUrl = 'http://npm.staging.caches.eas-build.internal';
jest
.mocked(spawn)
.mockReturnValueOnce(createSpawnPromise(Promise.resolve(createSpawnResult())));
await installDependenciesWithNpmCacheFallbackAsync({
packageManager: PackageManager.NPM,
env: {
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
},
logger,
cwd: '/tmp/build',
useFrozenLockfile: false,
});
expect(spawn).toHaveBeenCalledTimes(1);
expect(spawn).toHaveBeenCalledWith('npm', ['install', '--include=dev'], {
cwd: '/tmp/build',
logger,
infoCallbackFn: undefined,
lineTransformer: expect.any(Function),
env: {
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
NPM_CONFIG_REGISTRY: npmCacheUrl,
},
});
expect(Sentry.capture).not.toHaveBeenCalled();
});
});
function createSpawnPromise(result: Promise<SpawnResult>): SpawnPromise<SpawnResult> {
@@ -93,24 +93,7 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
infoCallbackFn?: SpawnOptions['infoCallbackFn'];
useFrozenLockfile: boolean;
}): Promise<void> {
const npmCacheUrl = env.EAS_BUILD_NPM_CACHE_URL;
if (!npmCacheUrl) {
await (
await installDependenciesAsync({
packageManager,
env,
logger,
infoCallbackFn,
cwd,
useFrozenLockfile,
})
).spawnPromise;
return;
}
logger.info(`Installing dependencies using the npm cache registry (${npmCacheUrl}).`);
const npmCacheUrl = env.NPM_CONFIG_REGISTRY;
let firstErrorLine: string | undefined;
let errorLineCount = 0;
@@ -118,11 +101,11 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
await (
await installDependenciesAsync({
packageManager,
env: { ...env, NPM_CONFIG_REGISTRY: npmCacheUrl },
env,
logger,
infoCallbackFn,
lineTransformer: (line: string) => {
if (isNpmCacheRegistryErrorLine(line, npmCacheUrl)) {
if (isNpmCacheRegistryErrorLine(line, { env, npmCacheUrl })) {
firstErrorLine ??= line;
errorLineCount += 1;
}
@@ -141,7 +124,7 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
});
}
} catch (err: unknown) {
if (!isNpmCacheInstallFailure(err, npmCacheUrl)) {
if (!isNpmCacheInstallFailure(err, { env, npmCacheUrl })) {
throw err;
}
@@ -161,10 +144,11 @@ export async function installDependenciesWithNpmCacheFallbackAsync({
},
});
const { NPM_CONFIG_REGISTRY: _NPM_CONFIG_REGISTRY, ...fallbackEnv } = env;
await (
await installDependenciesAsync({
packageManager,
env,
env: fallbackEnv,
logger,
infoCallbackFn,
cwd,
@@ -188,17 +172,35 @@ export function resolvePackagerDir(ctx: BuildContext<Job>): string {
return packagerRunDir;
}
function isNpmCacheInstallFailure(err: unknown, npmCacheUrl: string): boolean {
return getErrorOutput(err).includes(npmCacheUrl);
function isNpmCacheInstallFailure(
err: unknown,
{ env, npmCacheUrl }: { env: Record<string, string | undefined>; npmCacheUrl: string | undefined }
): boolean {
if (!isNpmCacheRegistryEnabled(env, npmCacheUrl)) {
return false;
}
const errorOutput = getErrorOutput(err);
return errorOutput.includes(npmCacheUrl);
}
function isNpmCacheRegistryErrorLine(line: string, npmCacheUrl: string): boolean {
function isNpmCacheRegistryErrorLine(
line: string,
{ env, npmCacheUrl }: { env: Record<string, string | undefined>; npmCacheUrl: string | undefined }
): boolean {
return (
isNpmCacheRegistryEnabled(env, npmCacheUrl) &&
line.includes(npmCacheUrl) &&
/(?:error|failed|ENOTFOUND|ECONN|ETIMEDOUT|EAI_AGAIN|FetchError)/i.test(line)
);
}
function isNpmCacheRegistryEnabled(
env: Record<string, string | undefined>,
npmCacheUrl: string | undefined
): npmCacheUrl is string {
return env.EAS_USE_NPM_CACHE === '1' && !!npmCacheUrl;
}
function getErrorOutput(err: unknown): string {
if (!(err instanceof Error)) {
return '';
@@ -1,132 +0,0 @@
import { BuildStepContext, BuildStepEnv } from '@expo/steps';
import spawn, { SpawnPromise, SpawnResult } from '@expo/turtle-spawn';
import { createMockLogger } from '../../../__tests__/utils/logger';
import {
PackageManager,
findPackagerRootDir,
resolvePackageManager,
shouldUseFrozenLockfile,
} from '../../../utils/packageManager';
import { readPackageJson } from '../../../utils/project';
import { installNodeModules } from '../installNodeModules';
jest.mock('@expo/turtle-spawn', () => jest.fn());
jest.mock('../../../sentry', () => ({
Sentry: {
capture: jest.fn(),
},
}));
jest.mock('../../../utils/packageManager', () => ({
...jest.requireActual('../../../utils/packageManager'),
resolvePackageManager: jest.fn(),
findPackagerRootDir: jest.fn(),
shouldUseFrozenLockfile: jest.fn(),
}));
jest.mock('../../../utils/project', () => ({
...jest.requireActual('../../../utils/project'),
readPackageJson: jest.fn(),
}));
describe(installNodeModules, () => {
beforeEach(() => {
jest.clearAllMocks();
jest.mocked(resolvePackageManager).mockReturnValue(PackageManager.NPM);
jest.mocked(findPackagerRootDir).mockReturnValue('/tmp/build');
jest.mocked(shouldUseFrozenLockfile).mockReturnValue(false);
jest.mocked(readPackageJson).mockReturnValue({});
});
function createStepCtx(logger: ReturnType<typeof createMockLogger>): BuildStepContext {
return {
logger,
workingDirectory: '/tmp/build',
global: {
projectTargetDirectory: '/tmp/build',
staticContext: { metadata: {} },
},
} as unknown as BuildStepContext;
}
it('retries the install without the npm cache registry when the cache install fails', async () => {
const logger = createMockLogger();
const npmCacheUrl = 'http://npm.caches.eas-build.internal';
const env: BuildStepEnv = {
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
};
const error = Object.assign(new Error('npm install exited with non-zero code: 1'), {
status: 1,
signal: null,
stdout: '',
stderr: `npm error request to ${npmCacheUrl}/left-pad failed, reason: getaddrinfo ENOTFOUND`,
});
jest
.mocked(spawn)
.mockReturnValueOnce(createSpawnPromise(Promise.reject(error)))
.mockReturnValueOnce(createSpawnPromise(Promise.resolve(createSpawnResult())));
await installNodeModules(createStepCtx(logger), env);
expect(spawn).toHaveBeenCalledTimes(2);
expect(spawn).toHaveBeenNthCalledWith(
1,
'npm',
['install', '--include=dev'],
expect.objectContaining({
env: {
EAS_USE_NPM_CACHE: '1',
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
NPM_CONFIG_REGISTRY: npmCacheUrl,
},
})
);
expect(spawn).toHaveBeenNthCalledWith(
2,
'npm',
['install', '--include=dev'],
expect.objectContaining({
env: { EAS_USE_NPM_CACHE: '1', EAS_BUILD_NPM_CACHE_URL: npmCacheUrl },
})
);
expect(logger.warn).toHaveBeenCalledWith(
`Failed to install dependencies using the npm cache registry (${npmCacheUrl}). Retrying without the npm cache registry.`
);
});
it('does not retry when the npm cache is not enabled', async () => {
const logger = createMockLogger();
const error = Object.assign(new Error('npm install exited with non-zero code: 1'), {
status: 1,
signal: null,
stdout: '',
stderr: 'npm error some unrelated failure',
});
jest.mocked(spawn).mockReturnValueOnce(createSpawnPromise(Promise.reject(error)));
await expect(installNodeModules(createStepCtx(logger), {})).rejects.toThrow(error);
expect(spawn).toHaveBeenCalledTimes(1);
});
});
function createSpawnPromise(result: Promise<SpawnResult>): SpawnPromise<SpawnResult> {
return Object.assign(result, {
child: {
pid: 123,
},
}) as SpawnPromise<SpawnResult>;
}
function createSpawnResult(): SpawnResult {
return {
pid: 123,
output: ['', ''],
stdout: '',
stderr: '',
status: 0,
signal: null,
};
}
@@ -1,7 +1,7 @@
import { BuildFunction, BuildStepContext, BuildStepEnv } from '@expo/steps';
import path from 'path';
import { installDependenciesWithNpmCacheFallbackAsync } from '../../common/installDependencies';
import { installDependenciesAsync } from '../../common/installDependencies';
import {
findPackagerRootDir,
getPackageVersionFromPackageJson,
@@ -63,7 +63,7 @@ export async function installNodeModules(
packageName: 'react-native',
});
await installDependenciesWithNpmCacheFallbackAsync({
const { spawnPromise } = await installDependenciesAsync({
packageManager,
env,
logger: stepCtx.logger,
@@ -74,4 +74,5 @@ export async function installNodeModules(
reactNativeVersion,
}),
});
await spawnPromise;
}
@@ -0,0 +1,75 @@
import { BuildContext, Builders } from '@expo/build-tools';
import { Platform } from '@expo/eas-build-job';
import { build, warnOnUnknownEnvironment } from '../build';
import { Analytics } from '../external/analytics';
jest.mock('@expo/build-tools', () => {
const actual = jest.requireActual('@expo/build-tools');
return {
...actual,
Builders: {
androidBuilder: jest.fn(async () => ({})),
iosBuilder: jest.fn(async () => ({})),
},
};
});
jest.mock('../displayRuntimeInfo', () => ({ displayWorkerRuntimeInfo: jest.fn() }));
jest.mock('../workingdir', () => ({ cleanUpWorkingdir: jest.fn() }));
jest.mock('../external/analytics', () => ({
...jest.requireActual('../external/analytics'),
logProjectDependenciesAsync: jest.fn(),
}));
function createEnvironmentWarningCtx(environment?: string) {
return {
metadata: environment === undefined ? undefined : { environment },
markBuildPhaseHasWarnings: jest.fn(),
logger: { warn: jest.fn() },
};
}
describe(warnOnUnknownEnvironment.name, () => {
it('warns and marks the phase for an unknown environment', () => {
const ctx = createEnvironmentWarningCtx('staging');
warnOnUnknownEnvironment(ctx);
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('Unknown environment "staging"')
);
});
it.each(['development', 'preview', 'production'])('does not warn for %s', environment => {
const ctx = createEnvironmentWarningCtx(environment);
warnOnUnknownEnvironment(ctx);
expect(ctx.markBuildPhaseHasWarnings).not.toHaveBeenCalled();
expect(ctx.logger.warn).not.toHaveBeenCalled();
});
it('does not warn when no environment is set', () => {
const ctx = createEnvironmentWarningCtx(undefined);
warnOnUnknownEnvironment(ctx);
expect(ctx.logger.warn).not.toHaveBeenCalled();
});
});
describe(build.name, () => {
it('warns during spin-up when the job environment is unknown', async () => {
const warn = jest.fn();
const markBuildPhaseHasWarnings = jest.fn();
const ctx = {
metadata: { environment: 'staging' },
job: { platform: Platform.ANDROID },
logger: { info: jest.fn(), warn, child: jest.fn() },
markBuildPhaseHasWarnings,
runBuildPhase: jest.fn(async (_phase, callback) => callback()),
} as unknown as BuildContext;
const analytics = { logEvent: jest.fn(), flushEventsAsync: jest.fn() } as unknown as Analytics;
await build({ ctx, buildId: 'build-id', analytics });
expect(Builders.androidBuilder).toHaveBeenCalled();
expect(markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(warn).toHaveBeenCalledWith(expect.stringContaining('Unknown environment "staging"'));
});
});
+2 -2
View File
@@ -268,8 +268,8 @@ describe(getBuildEnv.name, () => {
buildId: 'build-id',
});
expect(env.NPM_CACHE_URL).toBeUndefined();
expect(env.NPM_CONFIG_REGISTRY).toBeUndefined();
expect(env.NPM_CACHE_URL).toBe('https://npm.example');
expect(env.NPM_CONFIG_REGISTRY).toBe('https://npm.example');
expect(env.EAS_BUILD_NPM_CACHE_URL).toBe('https://npm.example');
expect(env.NVM_NODEJS_ORG_MIRROR).toBe('https://node.example');
expect(env.EAS_BUILD_MAVEN_CACHE_URL).toBe('https://maven.example');
+21
View File
@@ -19,6 +19,26 @@ import { Analytics, Event, logProjectDependenciesAsync } from './external/analyt
import { prepareRuntimeEnvironment } from './runtimeEnvironment';
import { cleanUpWorkingdir } from './workingdir';
const KNOWN_BUILD_ENVIRONMENTS = ['development', 'preview', 'production'];
type EnvironmentWarningContext = {
metadata?: { environment?: string };
logger: Pick<bunyan, 'warn'>;
markBuildPhaseHasWarnings: () => void;
};
export function warnOnUnknownEnvironment(ctx: EnvironmentWarningContext): void {
const environment = ctx.metadata?.environment;
if (environment && !KNOWN_BUILD_ENVIRONMENTS.includes(environment)) {
ctx.markBuildPhaseHasWarnings();
ctx.logger.warn(
`Unknown environment "${environment}". Expected one of: ${KNOWN_BUILD_ENVIRONMENTS.join(
', '
)}. No environment variables were added for it.`
);
}
}
export async function build({
ctx,
buildId,
@@ -40,6 +60,7 @@ export async function build({
{ job: omit(ctx.job, 'secrets', 'projectArchive') },
'Builder is ready, starting build'
);
warnOnUnknownEnvironment(ctx);
},
{ doNotMarkStart: true }
);
+3
View File
@@ -36,11 +36,14 @@ export function getBuildEnv({
setEnv(env, 'EAS_BUILD_RUNNER', 'eas-build');
setEnv(env, 'EAS_BUILD_PLATFORM', job.platform);
setEnv(env, 'EAS_CLI_SENTRY_DSN', config.sentry.dsn);
// NPM_CACHE_URL is deprecated
const npmCacheUrl = RuntimeSettings.getNpmCacheUrl();
const nodeJsCacheUrl = RuntimeSettings.getNodeJsCacheUrl();
const mavenCacheUrl = RuntimeSettings.getMavenCacheUrl();
const cocoapodsCacheUrl = RuntimeSettings.getCocoapodsCacheUrl();
setEnv(env, 'NPM_CACHE_URL', npmCacheUrl);
setEnv(env, 'NPM_CONFIG_REGISTRY', npmCacheUrl);
setEnv(env, 'NVM_NODEJS_ORG_MIRROR', nodeJsCacheUrl);
setEnv(env, 'EAS_BUILD_NPM_CACHE_URL', npmCacheUrl);
setEnv(env, 'EAS_BUILD_PROFILE', metadata.buildProfile);