Skip to content

Commit 0ce8e2c

Browse files
authored
fix: do not follow absolute paths outside job base (#568)
related to sveltejs/kit#13764 (comment) SvelteKit stringifies environment variables during build so that it can inject static values into the build output instead of always reading the env vars dynamically. However, `@vercel/nft` interprets any absolute path as an asset it should add. This causes builds on Vercel's build system to balloon in function size because it traces and bundles paths such as Node, Yarn global, etc. Often, this causes the deployment to fail because the function size exceeds the limit. This PR ensures asset paths outside the given job base are ignored to help us avoid packaging in system dependencies. ~Draft for now because I'm not confident that this fix doesn't break lots of other things~
1 parent 8f6a609 commit 0ce8e2c

13 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/analyze.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,9 +1169,33 @@ export default async function analyze(
11691169
return;
11701170
}
11711171
if (wildcardIndex !== -1 && stats.isFile()) return;
1172+
// do not emit assets outside the package boundary if inside node_modules
1173+
if (pkgBase) {
1174+
const nodeModulesBase =
1175+
id.substring(0, id.indexOf(path.sep + 'node_modules')) +
1176+
path.sep +
1177+
'node_modules' +
1178+
path.sep;
1179+
if (!assetPath.startsWith(nodeModulesBase)) {
1180+
if (job.log)
1181+
console.log(
1182+
'Skipping asset emission of ' +
1183+
assetPath +
1184+
' for ' +
1185+
id +
1186+
' as it is outside the package base ' +
1187+
pkgBase,
1188+
);
1189+
return;
1190+
}
1191+
}
11721192
if (stats.isFile()) {
1193+
// do not emit file assets outside job.base
1194+
if (job.ignoreFn(path.relative(job.base, assetPath))) return;
11731195
assets.add(assetPath);
11741196
} else if (stats.isDirectory()) {
1197+
// do not emit directory assets outside job.base
1198+
if (job.ignoreFn(path.relative(job.base, assetPath))) return;
11751199
if (validWildcard(assetPath)) emitAssetDirectory(assetPath);
11761200
}
11771201
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('some-pkg');

test/unit/pkg-dir-outside-base/node_modules/some-pkg/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/unit/pkg-dir-outside-base/node_modules/some-pkg/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
"package.json",
3+
"test/unit/pkg-dir-outside-base/input.js",
4+
"test/unit/pkg-dir-outside-base/node_modules/some-pkg/index.js",
5+
"test/unit/pkg-dir-outside-base/node_modules/some-pkg/package.json"
6+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secret
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('some-pkg');

test/unit/pkg-file-outside-base/node_modules/some-pkg/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)