Skip to content

Commit 927b5e2

Browse files
committed
Fix: Detect circular dependency
1 parent e2ab440 commit 927b5e2

7 files changed

Lines changed: 39 additions & 7 deletions

File tree

example/circular.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { addTest } from './second';
2+
3+
const circular = () => {
4+
console.log('addTest', addTest);
5+
console.log('Testing circular dependency');
6+
};
7+
8+
export default circular;

example/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"zip": true
99
},
1010
"functions": {
11-
"test": "./example/example.ts"
11+
"test": "./example.ts"
1212
}
13+
},
14+
"devDependencies": {
15+
"funpack": "file:.."
1316
}
1417
}

example/second.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import rimraf from 'rimraf';
22

3+
import circular from './circular';
4+
35
export const addTest = (a: number, b: number) => a + b;
46

57
const test = () => {
8+
console.log('circular', circular);
69
console.log('rimraf', rimraf);
710
};
811

src/packTests/basic.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ describe('funpack', () => {
4444
expect(console.log).toBeCalledWith(
4545
join('example', 'dist', 'testfunc.zip'),
4646
'-',
47-
21114,
47+
21199,
4848
'total bytes'
4949
);
5050
expect(console.log).toBeCalledWith(
5151
join('example', 'dist', 'second.zip'),
5252
'-',
53-
20815,
53+
20876,
5454
'total bytes'
5555
);
5656
});

src/packTests/splitting.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ describe('funpack', () => {
5252
expect(logMock.mock.calls[0][0]).toBe(
5353
join('example', 'dist-splitting', 'secondSplit.zip')
5454
);
55-
expect(logMock.mock.calls[0][2]).toBeCloseTo(67352, -2);
55+
expect(logMock.mock.calls[0][2]).toBeCloseTo(67547, -2);
5656

5757
expect(logMock.mock.calls[1][0]).toBe(
5858
join('example', 'dist-splitting', 'firstSplit.zip')
5959
);
60-
expect(logMock.mock.calls[1][2]).toBeCloseTo(69327, -2);
60+
expect(logMock.mock.calls[1][2]).toBeCloseTo(69491, -2);
6161
});
6262
});

src/parts/parseConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type ConfigSchemaType = z.infer<typeof configSchema>;
3030

3131
const parseConfig = (config: unknown) => {
3232
try {
33+
// TODO: Use strict (https://github.com/colinhacks/zod#strict)
3334
return configSchema.parse(config);
3435
} catch (e) {
3536
if (e instanceof ZodError) {

src/utils/getImportedPackages.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const getPackageNameFromPath = (path: string) => {
1515

1616
const getImportedPackages = (
1717
inputPath: string,
18-
metafile: Metafile
18+
metafile: Metafile,
19+
importChain: string[] = []
1920
): string[] => {
2021
const input = metafile.inputs[inputPath];
2122
if (!input) {
@@ -29,7 +30,23 @@ const getImportedPackages = (
2930
const packageName = getPackageNameFromPath(inputImport.path);
3031
importedPackages.add(packageName);
3132
} else {
32-
const packages = getImportedPackages(inputImport.path, metafile);
33+
const updatedImportChain = [...importChain, inputImport.path];
34+
35+
// Check for circular dependency
36+
if (importChain.includes(inputImport.path)) {
37+
console.warn(
38+
'Circular dependency detected! Chain:',
39+
updatedImportChain
40+
);
41+
continue;
42+
}
43+
44+
// Check imports of specified path
45+
const packages = getImportedPackages(
46+
inputImport.path,
47+
metafile,
48+
updatedImportChain
49+
);
3350
for (const packageName of packages) {
3451
importedPackages.add(packageName);
3552
}

0 commit comments

Comments
 (0)