Skip to content

Commit a355327

Browse files
authored
Resolve dockerfile to absolute path in redirected config in vite plugin (#10038)
* resolve dockerfile to absolute path in redirected config * changeset * resolve buildContext to absolute path in wrangler too * resolve buildContext to absolute path in vite plugin
1 parent 08b37f8 commit a355327

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

.changeset/rich-bottles-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Resolve `containers.image` (if it is a path to a Dockerfile) to an absolute path in the deploy config.

packages/containers-shared/src/build.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ export async function constructBuildCommand(
3030
const baseDir = configPath ? path.dirname(configPath) : process.cwd();
3131
const absDockerfilePath = path.resolve(baseDir, options.pathToDockerfile);
3232
const dockerfile = readFileSync(absDockerfilePath, "utf-8");
33+
34+
const absBuildContext = options.buildContext
35+
? path.resolve(baseDir, options.buildContext)
36+
: path.dirname(absDockerfilePath);
3337
// pipe in the dockerfile
3438
buildCmd.push("-f", "-");
35-
buildCmd.push(options.buildContext ?? path.dirname(absDockerfilePath));
39+
buildCmd.push(absBuildContext);
3640
logger?.debug(`Building image with command: ${buildCmd.join(" ")}`);
3741
return { buildCmd, dockerfile };
3842
}

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getDevContainerImageName } from "@cloudflare/containers-shared/src/knob
66
import {
77
generateContainerBuildId,
88
getContainerIdsByImageTags,
9+
isDockerfile,
910
} from "@cloudflare/containers-shared/src/utils";
1011
import { generateStaticRoutingRuleMatcher } from "@cloudflare/workers-shared/asset-worker/src/utils/rules-engine";
1112
import replace from "@rollup/plugin-replace";
@@ -263,6 +264,45 @@ if (import.meta.hot) {
263264
path.resolve(resolvedViteConfig.root, clientOutputDirectory)
264265
),
265266
};
267+
268+
if (workerConfig.containers?.length) {
269+
workerConfig.containers = workerConfig.containers.map(
270+
(container) => {
271+
const baseDir = workerConfig.configPath
272+
? path.dirname(workerConfig.configPath)
273+
: resolvedViteConfig.root;
274+
// Wrangler's config validation resolves to container.configuration.image, even though it is deprecated
275+
const image =
276+
container.configuration?.image ?? container.image;
277+
if (isDockerfile(image, workerConfig.configPath)) {
278+
const output = {
279+
...container,
280+
image: path.resolve(baseDir, image),
281+
...(container.image_build_context
282+
? {
283+
image_build_context: path.resolve(
284+
baseDir,
285+
container.image_build_context
286+
),
287+
}
288+
: {}),
289+
};
290+
// however, wrangler deploy will re-resolve the config, so we should
291+
// deduplicate the container.configuration.image field in favour of
292+
// the non-deprecated one when we write out the deploy config
293+
delete output.configuration?.image;
294+
// if we don't do this, we get a warning that container.configuration is deprecated
295+
if (
296+
output.configuration &&
297+
Object.keys(output.configuration).length === 0
298+
) {
299+
delete output.configuration;
300+
}
301+
return output;
302+
} else return container;
303+
}
304+
);
305+
}
266306
} else {
267307
workerConfig.assets = undefined;
268308
}

packages/wrangler/src/__tests__/cloudchamber/build.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { mkdirSync, writeFileSync } from "fs";
2+
import path from "path";
23
import {
34
dockerBuild,
45
dockerImageInspect,
@@ -68,7 +69,7 @@ describe("buildAndMaybePush", () => {
6869
"--provenance=false",
6970
"-f",
7071
"-",
71-
"./container-context",
72+
path.resolve(process.cwd(), "./container-context"),
7273
],
7374
dockerfile,
7475
});
@@ -100,7 +101,8 @@ describe("buildAndMaybePush", () => {
100101
"--provenance=false",
101102
"-f",
102103
"-",
103-
"./container-context",
104+
// turn this into a relative path so that this works across different OSes
105+
path.resolve(process.cwd(), "./container-context"),
104106
],
105107
dockerfile,
106108
});
@@ -151,7 +153,7 @@ describe("buildAndMaybePush", () => {
151153
"--provenance=false",
152154
"-f",
153155
"-",
154-
"./container-context",
156+
path.resolve(process.cwd(), "./container-context"),
155157
],
156158
dockerfile,
157159
});
@@ -193,7 +195,7 @@ describe("buildAndMaybePush", () => {
193195
"--provenance=false",
194196
"-f",
195197
"-",
196-
"./container-context",
198+
path.resolve(process.cwd(), "./container-context"),
197199
],
198200
dockerfile,
199201
});
@@ -217,7 +219,7 @@ describe("buildAndMaybePush", () => {
217219
"host",
218220
"-f",
219221
"-",
220-
"./container-context",
222+
path.resolve(process.cwd(), "./container-context"),
221223
],
222224
dockerfile,
223225
});
@@ -237,7 +239,7 @@ describe("buildAndMaybePush", () => {
237239
"--provenance=false",
238240
"-f",
239241
"-",
240-
"./container-context",
242+
path.resolve(process.cwd(), "./container-context"),
241243
],
242244
dockerfile,
243245
});

0 commit comments

Comments
 (0)