Skip to content

Commit 0dd9e15

Browse files
chore(cli): tighten layout — restore upstream order + offlineEnvShim
Three small refactors after the BuildKit-native cleanup: 1. buildImage.ts: move loadContainerfileModule + generateContainerfile back above parseGenerateOptions to match upstream's ordering. The fork's addition of loadContainerfileModule had pushed the helpers below their sole consumer; restoring upstream order makes the file's structure easier to scan against the upstream version. 2. buildImage.ts: inline `BASE_IMAGE[options.runtime]` directly into the parseGenerateOptions return literal instead of binding it to a local const first. One fewer named binding for a value used exactly once. 3. managed-index-controller.ts: introduce offlineEnvShim() that returns the same `{ success: true, data: { variables } }` envelope as `cliApiClient.getEnvironmentVariables`. Lets the indexer treat both modes uniformly — single `if (!$env.success)` check, single `$env.data.variables` use site — instead of branching on `result.mode` to assemble the envVars value. No behavior change. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 9e00e0a commit 0dd9e15

2 files changed

Lines changed: 47 additions & 38 deletions

File tree

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -715,34 +715,6 @@ const BASE_IMAGE: Record<BuildRuntime, string> = {
715715

716716
const DEFAULT_PACKAGES = ["busybox", "ca-certificates", "dumb-init", "git", "openssl"];
717717

718-
export const parseGenerateOptions = (options: GenerateContainerfileOptions) => {
719-
const buildArgs = Object.entries(options.build.env || {})
720-
.flatMap(([key]) => `ARG ${key}`)
721-
.join("\n");
722-
723-
const buildEnvVars = Object.entries(options.build.env || {})
724-
.flatMap(([key]) => `ENV ${key}=$${key}`)
725-
.join("\n");
726-
727-
const postInstallCommands = (options.build.commands || []).map((cmd) => `RUN ${cmd}`).join("\n");
728-
729-
const baseInstructions = (options.image?.instructions || []).join("\n");
730-
const packages = Array.from(new Set(DEFAULT_PACKAGES.concat(options.image?.pkgs || []))).join(
731-
" "
732-
);
733-
734-
const baseImage = BASE_IMAGE[options.runtime];
735-
736-
return {
737-
baseImage,
738-
baseInstructions,
739-
buildArgs,
740-
buildEnvVars,
741-
packages,
742-
postInstallCommands,
743-
};
744-
};
745-
746718
async function loadContainerfileModule(modulePath: string): Promise<ContainerfileTemplate> {
747719
const absolutePath = resolve(modulePath);
748720

@@ -792,6 +764,35 @@ export async function generateContainerfile(options: GenerateContainerfileOption
792764
}
793765
}
794766

767+
export const parseGenerateOptions = (options: GenerateContainerfileOptions) => {
768+
const buildArgs = Object.entries(options.build.env || {})
769+
.flatMap(([key]) => `ARG ${key}`)
770+
.join("\n");
771+
772+
const buildEnvVars = Object.entries(options.build.env || {})
773+
.flatMap(([key]) => `ENV ${key}=$${key}`)
774+
.join("\n");
775+
776+
const postInstallCommands = (options.build.commands || []).map((cmd) => `RUN ${cmd}`).join("\n");
777+
778+
const baseInstructions = (options.image?.instructions || []).join("\n");
779+
const packages = Array.from(new Set(DEFAULT_PACKAGES.concat(options.image?.pkgs || []))).join(
780+
" "
781+
);
782+
783+
return {
784+
baseImage: BASE_IMAGE[options.runtime],
785+
baseInstructions,
786+
buildArgs,
787+
buildEnvVars,
788+
packages,
789+
postInstallCommands,
790+
};
791+
};
792+
793+
794+
795+
795796
export async function generateBunContainerfile(options: GenerateContainerfileOptions) {
796797
const { baseImage, buildArgs, buildEnvVars, postInstallCommands, baseInstructions, packages } =
797798
parseGenerateOptions(options);

packages/cli-v3/src/entryPoints/managed-index-controller.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ type OfflineBootstrap = {
5656

5757
type BootstrapResult = OnlineBootstrap | OfflineBootstrap;
5858

59+
/**
60+
* Returns the same shape as `cliApiClient.getEnvironmentVariables` for the
61+
* offline path. We never have project env vars at index time in offline mode
62+
* (the build container has no API access), so it's just an empty `variables`
63+
* map wrapped in the success envelope so downstream code can branch once on
64+
* `$env.success`.
65+
*/
66+
const offlineEnvShim = () =>
67+
({ success: true as const, data: { variables: {} as Record<string, string> } });
68+
5969
async function bootstrap(): Promise<BootstrapResult> {
6070
const buildManifest = await loadBuildManifest();
6171

@@ -105,23 +115,21 @@ async function indexDeployment(result: BootstrapResult) {
105115
const stderr: string[] = [];
106116

107117
try {
108-
const envVars =
118+
const $env =
109119
result.mode === "offline"
110-
? {}
111-
: await (async () => {
112-
const $env = await result.cliApiClient.getEnvironmentVariables(result.projectRef);
113-
if (!$env.success) {
114-
throw new Error(`Failed to fetch environment variables: ${$env.error}`);
115-
}
116-
return $env.data.variables;
117-
})();
120+
? offlineEnvShim()
121+
: await result.cliApiClient.getEnvironmentVariables(result.projectRef);
122+
123+
if (!$env.success) {
124+
throw new Error(`Failed to fetch environment variables: ${$env.error}`);
125+
}
118126

119127
const workerManifest = await indexWorkerManifest({
120128
runtime: buildManifest.runtime,
121129
indexWorkerPath: buildManifest.indexWorkerEntryPoint,
122130
buildManifestPath: "./build.json",
123131
nodeOptions: execOptionsForRuntime(buildManifest.runtime, buildManifest),
124-
env: envVars,
132+
env: $env.data.variables,
125133
otelHookExclude: buildManifest.otelImportHook?.exclude,
126134
otelHookInclude: buildManifest.otelImportHook?.include,
127135
handleStdout(data) {

0 commit comments

Comments
 (0)