-
Notifications
You must be signed in to change notification settings - Fork 219
[worker] warn on unknown build environment #3952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gwdp
wants to merge
1
commit into
main
Choose a base branch
from
gwdp/warn-unknown-build-environment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"')); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } | ||
| ); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
wwwset 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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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