Skip to content

Commit 595d6ba

Browse files
committed
[worker] warn on unknown build environment
1 parent 9247f3c commit 595d6ba

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @ts-nocheck
2+
import { warnOnUnknownEnvironment } from '../build';
3+
4+
function createCtx(environment) {
5+
return {
6+
metadata: environment === undefined ? undefined : { environment },
7+
markBuildPhaseHasWarnings: jest.fn(),
8+
logger: { warn: jest.fn() },
9+
};
10+
}
11+
12+
describe(warnOnUnknownEnvironment.name, () => {
13+
it('warns and marks the phase for an unknown environment', () => {
14+
const ctx = createCtx('staging');
15+
warnOnUnknownEnvironment(ctx);
16+
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
17+
expect(ctx.logger.warn).toHaveBeenCalledWith(
18+
expect.stringContaining('Unknown environment "staging"')
19+
);
20+
});
21+
22+
it.each(['development', 'preview', 'production'])('does not warn for %s', environment => {
23+
const ctx = createCtx(environment);
24+
warnOnUnknownEnvironment(ctx);
25+
expect(ctx.markBuildPhaseHasWarnings).not.toHaveBeenCalled();
26+
expect(ctx.logger.warn).not.toHaveBeenCalled();
27+
});
28+
29+
it('does not warn when no environment is set', () => {
30+
const ctx = createCtx(undefined);
31+
warnOnUnknownEnvironment(ctx);
32+
expect(ctx.logger.warn).not.toHaveBeenCalled();
33+
});
34+
});

packages/worker/src/build.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ import { Analytics, Event, logProjectDependenciesAsync } from './external/analyt
1919
import { prepareRuntimeEnvironment } from './runtimeEnvironment';
2020
import { cleanUpWorkingdir } from './workingdir';
2121

22+
const KNOWN_BUILD_ENVIRONMENTS = ['development', 'preview', 'production'];
23+
24+
export function warnOnUnknownEnvironment(ctx: BuildContext): void {
25+
const environment = ctx.metadata?.environment;
26+
if (environment && !KNOWN_BUILD_ENVIRONMENTS.includes(environment)) {
27+
ctx.markBuildPhaseHasWarnings();
28+
ctx.logger.warn(
29+
`Unknown environment "${environment}". Expected one of: ${KNOWN_BUILD_ENVIRONMENTS.join(
30+
', '
31+
)}. No environment variables were added for it.`
32+
);
33+
}
34+
}
35+
2236
export async function build({
2337
ctx,
2438
buildId,
@@ -40,6 +54,7 @@ export async function build({
4054
{ job: omit(ctx.job, 'secrets', 'projectArchive') },
4155
'Builder is ready, starting build'
4256
);
57+
warnOnUnknownEnvironment(ctx);
4358
},
4459
{ doNotMarkStart: true }
4560
);

0 commit comments

Comments
 (0)