Skip to content

Commit 9584031

Browse files
committed
fix(cli): build the code layer with an exclude copy instead of deleting node_modules
The code stage was a snapshot of the build stage that ran `chmod -R u+rwX node_modules && rm -rf node_modules`; on overlayfs the chmod copies every dependency file into the throwaway layer before the rm walks it again, which made deploys of large projects markedly slower since 4.5.11. The stage now copies /app from the build stage with node_modules excluded (`COPY --exclude`, stable in the dockerfile:1 frontend the templates already pin), producing the same dependency/code layer split with no extra walks.
1 parent 04d3264 commit 9584031

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+
Deploy image builds no longer spend time (and disk churn) removing `node_modules` while assembling the code layer; the layer is now copied with the dependency tree excluded. Large projects should see noticeably faster builds.

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)