Skip to content

Commit 4ed4c87

Browse files
committed
refactor(cli): avoid reloading config during deploy
1 parent 32f19ff commit 4ed4c87

3 files changed

Lines changed: 34 additions & 34 deletions

File tree

packages/cli-v3/src/commands/deploy.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,8 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
364364
throw new Error("Failed to get project client");
365365
}
366366

367-
if (projectClient.defaultRuntime) {
368-
resolvedConfig = await loadConfig({
369-
cwd: projectPath,
370-
overrides: { project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF },
371-
configFile: options.config,
372-
defaultRuntime: projectClient.defaultRuntime,
373-
warn: false,
374-
});
367+
if (!resolvedConfig.runtimeWasExplicit && projectClient.defaultRuntime) {
368+
resolvedConfig.runtime = projectClient.defaultRuntime;
375369
}
376370

377371
if (options.nativeBuildServer) {

packages/cli-v3/src/config.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ describe("loadConfig runtime", () => {
4545
await expect(loadConfig({ cwd, warn: false })).resolves.toMatchObject({ runtime: expected });
4646
});
4747

48-
it("uses the project default when runtime is omitted", async () => {
49-
const cwd = await createProject();
48+
it("tracks whether runtime was explicitly configured", async () => {
49+
const cwd = await createProject("node-22");
5050

51-
await expect(
52-
loadConfig({ cwd, defaultRuntime: "node-24", warn: false })
53-
).resolves.toMatchObject({
54-
runtime: "node-24",
51+
await expect(loadConfig({ cwd, warn: false })).resolves.toMatchObject({
52+
runtime: "node-22",
53+
runtimeWasExplicit: true,
5554
});
5655
});
5756

58-
it("prefers an explicit runtime over the project default", async () => {
59-
const cwd = await createProject("node-22");
57+
it("tracks an omitted runtime separately from the legacy default", async () => {
58+
const cwd = await createProject();
6059

61-
await expect(
62-
loadConfig({ cwd, defaultRuntime: "node-24", warn: false })
63-
).resolves.toMatchObject({ runtime: "node-22" });
60+
await expect(loadConfig({ cwd, warn: false })).resolves.toMatchObject({
61+
runtime: "node",
62+
runtimeWasExplicit: false,
63+
});
6464
});
6565

6666
it("keeps node as the legacy default when runtime is omitted", async () => {

packages/cli-v3/src/config.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,37 @@ export type ResolveConfigOptions = {
3434
cwd?: string;
3535
overrides?: Partial<TriggerConfig>;
3636
configFile?: string;
37-
defaultRuntime?: BuildRuntime;
3837
warn?: boolean;
3938
};
4039

40+
export type LoadedConfig = ResolvedConfig & {
41+
runtimeWasExplicit: boolean;
42+
};
43+
4144
export async function loadConfig({
4245
cwd = process.cwd(),
4346
overrides,
4447
configFile,
45-
defaultRuntime,
4648
warn = true,
47-
}: ResolveConfigOptions = {}): Promise<ResolvedConfig> {
49+
}: ResolveConfigOptions = {}): Promise<LoadedConfig> {
4850
const result = await c12.loadConfig<TriggerConfig>({
4951
name: "trigger",
5052
cwd,
5153
configFile,
5254
jitiOptions: { debug: logger.loggerLevel === "debug" },
5355
});
5456

55-
return await resolveConfig(cwd, result, overrides, defaultRuntime, warn);
57+
return await resolveConfig(cwd, result, overrides, warn);
5658
}
5759

5860
type ResolveWatchConfigOptions = ResolveConfigOptions & {
59-
onUpdate: (config: ResolvedConfig) => void;
61+
onUpdate: (config: LoadedConfig) => void;
6062
debounce?: number;
6163
ignoreInitial?: boolean;
6264
};
6365

6466
type ResolveWatchConfigResult = {
65-
config: ResolvedConfig;
67+
config: LoadedConfig;
6668
files: string[];
6769
stop: () => Promise<void>;
6870
};
@@ -74,7 +76,6 @@ export async function watchConfig({
7476
ignoreInitial = true,
7577
overrides,
7678
configFile,
77-
defaultRuntime,
7879
}: ResolveWatchConfigOptions): Promise<ResolveWatchConfigResult> {
7980
const result = await c12.watchConfig<TriggerConfig>({
8081
name: "trigger",
@@ -84,13 +85,13 @@ export async function watchConfig({
8485
chokidarOptions: { ignoreInitial },
8586
jitiOptions: { debug: logger.loggerLevel === "debug" },
8687
onUpdate: async ({ newConfig }) => {
87-
const resolvedConfig = await resolveConfig(cwd, newConfig, overrides, defaultRuntime, false);
88+
const resolvedConfig = await resolveConfig(cwd, newConfig, overrides, false);
8889

8990
onUpdate(resolvedConfig);
9091
},
9192
});
9293

93-
const config = await resolveConfig(cwd, result, overrides, defaultRuntime);
94+
const config = await resolveConfig(cwd, result, overrides);
9495

9596
return {
9697
config,
@@ -159,9 +160,8 @@ async function resolveConfig(
159160
cwd: string,
160161
result: c12.ResolvedConfig<TriggerConfig>,
161162
overrides?: Partial<TriggerConfig>,
162-
defaultRuntime?: BuildRuntime,
163163
warn = true
164-
): Promise<ResolvedConfig> {
164+
): Promise<LoadedConfig> {
165165
// `trigger.config` is the fallback value set by c12. Bail out with actionable guidance before
166166
// touching the filesystem: the pkg-types resolvers below throw raw errors when run outside a
167167
// project (e.g. `dev` before `init`), which would mask this message.
@@ -186,8 +186,7 @@ async function resolveConfig(
186186
["run_engine_v2" as const].concat(config.compatibilityFlags ?? [])
187187
);
188188
const legacyDefaultRuntime: BuildRuntime = features.run_engine_v2 ? "node" : DEFAULT_RUNTIME;
189-
const configuredRuntime =
190-
overrides?.runtime ?? config.runtime ?? defaultRuntime ?? legacyDefaultRuntime;
189+
const configuredRuntime = overrides?.runtime ?? config.runtime ?? legacyDefaultRuntime;
191190
const runtime = resolveBuildRuntime(configuredRuntime);
192191

193192
if (warn && isDeprecatedConfigRuntime(configuredRuntime)) {
@@ -229,7 +228,7 @@ async function resolveConfig(
229228
config,
230229
{
231230
dirs,
232-
runtime: defaultRuntime ?? legacyDefaultRuntime,
231+
runtime: legacyDefaultRuntime,
233232
tsconfig: tsconfigPath,
234233
build: {
235234
jsx: {
@@ -246,12 +245,19 @@ async function resolveConfig(
246245
}
247246
) as ResolvedConfig; // TODO: For some reason, without this, there is a weird type error complaining about tsconfigPath being string | nullish, which can't be assigned to string | undefined
248247

249-
return {
248+
const resolvedConfig = {
250249
...mergedConfig,
251250
dirs: Array.from(new Set(dirs)),
252251
instrumentedPackageNames: getInstrumentedPackageNames(mergedConfig),
253252
runtime,
254253
};
254+
255+
Object.defineProperty(resolvedConfig, "runtimeWasExplicit", {
256+
value: overrides?.runtime !== undefined || config.runtime !== undefined,
257+
enumerable: false,
258+
});
259+
260+
return resolvedConfig as LoadedConfig;
255261
}
256262

257263
function resolveTriggerDir(dir: string, workingDir: string): string {

0 commit comments

Comments
 (0)