Skip to content

Commit 7447919

Browse files
authored
fix(cli): build the code layer with an exclude copy instead of deleting node_modules (#4878)
Fixes a deploy slowdown introduced in #4551 to build the code layer, the generated Containerfile deleted `node_modules` from a snapshot of the build stage. New approach: the code stage starts empty and copies the app files with the dependency tree excluded. `COPY --exclude` is stable in the `docker/dockerfile:1` frontend the templates already pin.
1 parent 7ad0d92 commit 7447919

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
Fixes an issue introduced in 4.5.11 that made deploy image builds of projects with large dependency trees noticeably slower.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ describe("generateContainerfile", () => {
156156

157157
const user = runtime === "bun" ? "bun:bun" : "node:node";
158158

159-
expect(containerfile).toContain("FROM build AS code");
159+
expect(containerfile).toContain("FROM scratch AS code");
160+
expect(containerfile).toContain("COPY --from=build --exclude=node_modules /app /app");
160161
expect(containerfile).toContain(
161162
`COPY --from=build --chown=${user} /app/node_modules ./node_modules`
162163
);
@@ -180,15 +181,15 @@ describe("generateContainerfile", () => {
180181
const postInstall = containerfile.indexOf("RUN echo post-install");
181182
// guard after post-install so a command that prunes node_modules can't break the COPY
182183
const mkdirGuard = containerfile.indexOf("RUN mkdir -p node_modules");
183-
const codeStage = containerfile.indexOf("FROM build AS code");
184-
const rmNodeModules = containerfile.indexOf(
185-
"RUN chmod -R u+rwX node_modules && rm -rf node_modules"
184+
const codeStage = containerfile.indexOf("FROM scratch AS code");
185+
const excludeCopy = containerfile.indexOf(
186+
"COPY --from=build --exclude=node_modules /app /app"
186187
);
187188

188189
expect(postInstall).toBeGreaterThan(-1);
189190
expect(mkdirGuard).toBeGreaterThan(postInstall);
190191
expect(codeStage).toBeGreaterThan(mkdirGuard);
191-
expect(rmNodeModules).toBeGreaterThan(codeStage);
192+
expect(excludeCopy).toBeGreaterThan(codeStage);
192193
}
193194
);
194195
});

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,9 @@ ${postInstallCommands}
837837
# node_modules may not exist when there are no dependencies to install
838838
RUN mkdir -p node_modules
839839
840-
FROM build AS code
840+
FROM scratch AS code
841841
842-
# u+rwX first: non-root rm fails on read-only or non-traversable directories
843-
RUN chmod -R u+rwX node_modules && rm -rf node_modules
842+
COPY --from=build --exclude=node_modules /app /app
844843
845844
FROM build AS indexer
846845
@@ -945,10 +944,9 @@ COPY --chown=node:node . .
945944
# node_modules may not exist when there are no dependencies to install
946945
RUN mkdir -p node_modules
947946
948-
FROM build AS code
947+
FROM scratch AS code
949948
950-
# u+rwX first: non-root rm fails on read-only or non-traversable directories
951-
RUN chmod -R u+rwX node_modules && rm -rf node_modules
949+
COPY --from=build --exclude=node_modules /app /app
952950
953951
FROM build AS indexer
954952

0 commit comments

Comments
 (0)