Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions packages/worker/src/__unit__/build.test.ts
Original file line number Diff line number Diff line change
@@ -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"'));
});
});
21 changes: 21 additions & 0 deletions packages/worker/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support custom environments, so specifying a not-known environment is not an issue per se as long as the user has environment variables in that environment?

Initially I thought we might color the environment pill yellow on workflow run page if the environment has no variables, but that would color it yellow if a user does not use EAS Environment Variables at all which is bad?

Maybe we do this ^ but only if environment is not one of the known environments? Or sth like that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tysm for the review! I missed the custom environments UI, and the www enum only listing dev/preview/prod made me assume they weren't a thing. Should've asked claude:rolling_on_the_floor_laughing:

Agreed that false warnings would be bad here. I think the cleaner fix is to have www set a flag (that's where the resolved vars actually live from what I see) saying the environment matched no variables, and have the worker just react to it instead of hardcoding the known-environments list. wdyt?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it depends on where do we want the flag to live!

We could have it be a column / flag (property in some new JSONB column) on a Workflow Job. Then, based on the flag supplied at creation time we would later show the warning in the UI or not. This requires adding a new field.

We could have it be something calculated dynamically, like we open workflow run page, website gets the list of environments used in the workflow run, website asks www if those environments currently have variables assigned to them. Bad thing about it is that this may drift as we don't have a notion of "environment variables at a given point in time".

We could have it be, as you suggest, a flag on the job, but then we can't show the warning on non-VM jobs!

I think the first is the most correct one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I was okay with not showing it for non-VM jobs, but reasoning through, the warning I'm showing was never the perfect place imho. Further on, maybe a new warningson WorkflowJob would allow us to have those warnings issues on www and more flexibility on how to show? (Maybe in the annotations?). Will dig further but sharing in case you have a different take or any input

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,
Expand All @@ -40,6 +60,7 @@ export async function build({
{ job: omit(ctx.job, 'secrets', 'projectArchive') },
'Builder is ready, starting build'
);
warnOnUnknownEnvironment(ctx);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

started with a new build phase but thought it was overkill and moved here. Let me know if you have any different thoughts

},
{ doNotMarkStart: true }
);
Expand Down
Loading