Skip to content

Commit 284fedd

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

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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(expect.stringContaining('Unknown environment "staging"'));
18+
});
19+
20+
it.each(['development', 'preview', 'production'])('does not warn for %s', (environment) => {
21+
const ctx = createCtx(environment);
22+
warnOnUnknownEnvironment(ctx);
23+
expect(ctx.markBuildPhaseHasWarnings).not.toHaveBeenCalled();
24+
expect(ctx.logger.warn).not.toHaveBeenCalled();
25+
});
26+
27+
it('does not warn when no environment is set', () => {
28+
const ctx = createCtx(undefined);
29+
warnOnUnknownEnvironment(ctx);
30+
expect(ctx.logger.warn).not.toHaveBeenCalled();
31+
});
32+
});

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)