Skip to content

Commit

Permalink
Make MAX_LOCK_FILE_SIZE configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Jan 13, 2025
1 parent 211f3b8 commit 6c21e16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
22 changes: 22 additions & 0 deletions node-src/lib/getDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,26 @@ describe('getDependencies', () => {
})
).rejects.toThrowError();
});

it('should use MAX_LOCK_FILE_SIZE environment variable, if set', async () => {
vi.stubEnv('MAX_LOCK_FILE_SIZE', (MAX_LOCK_FILE_SIZE + 2000).toString());
statSync.mockReturnValue({ size: MAX_LOCK_FILE_SIZE + 1000 });

const dependencies = await getDependencies(ctx, {
rootPath: path.join(__dirname, '../__mocks__/dependencyChanges'),
manifestPath: 'plain-package.json',
lockfilePath: 'plain-yarn.lock',
});

const [dep] = dependencies;
expect(dep).toMatch(/^[\w/@-]+@@[\d.]+$/);

const dependencyNames = [...dependencies].map((dependency) => dependency.split('@@')[0]);
expect(dependencyNames).toEqual(
expect.arrayContaining([
...Object.keys(packageJson.dependencies),
...Object.keys(packageJson.devDependencies),
])
);
});
});
17 changes: 12 additions & 5 deletions node-src/lib/getDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export const getDependencies = async (
) => {
// We can run into OOM errors if the lock file is too large. Therefore, we bail early and skip
// lock file parsing because some TurboSnap is better than no TurboSnap.
const stats = statSync(path.resolve(rootPath, lockfilePath));
if (stats.size > MAX_LOCK_FILE_SIZE) {
ctx.log.warn({ lockfilePath }, 'Lock file too large to parse, skipping');
throw new Error('Lock file too large to parse');
}
ensureLockFileSize(ctx, path.resolve(rootPath, lockfilePath));

try {
const headTree = await buildDepTreeFromFiles(
Expand All @@ -54,3 +50,14 @@ function flattenDependencyTree(tree: PkgTree['dependencies'], results = new Set<

return results;
}

function ensureLockFileSize(ctx: Context, fullPath: string) {
const maxLockFileSize =
Number.parseInt(process.env.MAX_LOCK_FILE_SIZE ?? '') || MAX_LOCK_FILE_SIZE;

const stats = statSync(fullPath);
if (stats.size > maxLockFileSize) {
ctx.log.warn({ fullPath }, 'Lock file too large to parse, skipping');
throw new Error('Lock file too large to parse');
}
}

0 comments on commit 6c21e16

Please sign in to comment.