Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pacmak): fails on bundled dependency without entry point #3277

Merged
merged 5 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/jsii-pacmak/lib/dependency-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export interface TraverseDependencyGraphHost {
export interface PackageJson {
readonly dependencies?: { readonly [name: string]: string };
readonly peerDependencies?: { readonly [name: string]: string };
readonly bundleDependencies?: string[];
readonly bundledDependencies?: string[];
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved

readonly [key: string]: unknown;
}
Expand Down Expand Up @@ -88,7 +90,13 @@ async function real$traverseDependencyGraph(
]);
return Promise.all(
Array.from(deps)
.filter((m) => !util.isBuiltinModule(m))
// No need to pacmak the dependency if it's built-in, or if it's bundled
.filter(
(m) =>
!util.isBuiltinModule(m) &&
!meta.bundledDependencies?.includes(m) &&
!meta.bundleDependencies?.includes(m),
)
.map(async (dep) => {
const dependencyDir = await host.findDependencyDirectory(
dep,
Expand Down
88 changes: 88 additions & 0 deletions packages/jsii-pacmak/test/dependency-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,91 @@ test('stops traversing when callback returns false', async () => {
expect(mockHost.readJson).toHaveBeenCalledTimes(2);
expect(mockHost.findDependencyDirectory).toHaveBeenCalledTimes(1);
});

test('dont call findDependencyDirectory for bundledDependencies', async () => {
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
const packages: Record<string, { root: string; meta: any }> = {
A: {
root: join(tmpdir(), 'A'),
meta: { dependencies: { B: '*' }, bundledDependencies: ['B'] },
},
};

const cb: Callback = jest.fn().mockName('callback').mockReturnValue(true);

fakeReadJson(packages);

// WHEN
await expect(
traverseDependencyGraph(packages.A.root, cb, mockHost),
).resolves.not.toThrow();

// THEN
expect(mockHost.findDependencyDirectory).not.toHaveBeenCalled();
});

test('dont call findDependencyDirectory for bundleDependencies', async () => {
const packages: Record<string, { root: string; meta: any }> = {
A: {
root: join(tmpdir(), 'A'),
meta: { dependencies: { B: '*' }, bundleDependencies: ['B'] },
},
};

const cb: Callback = jest.fn().mockName('callback').mockReturnValue(true);

fakeReadJson(packages);

// WHEN
await expect(
traverseDependencyGraph(packages.A.root, cb, mockHost),
).resolves.not.toThrow();

// THEN
expect(mockHost.findDependencyDirectory).not.toHaveBeenCalled();
});

test('dont call findDependencyDirectory for bundleDependencies AND bundledDependencies', async () => {
const packages: Record<string, { root: string; meta: any }> = {
A: {
root: join(tmpdir(), 'A'),
meta: {
dependencies: { B: '*', C: '*' },
bundleDependencies: ['B'],
bundledDependencies: ['C'],
},
},
};

const cb: Callback = jest.fn().mockName('callback').mockReturnValue(true);

fakeReadJson(packages);

// WHEN
await expect(
traverseDependencyGraph(packages.A.root, cb, mockHost),
).resolves.not.toThrow();

// THEN
expect(mockHost.findDependencyDirectory).not.toHaveBeenCalled();
});

function fakeReadJson(
fakePackages: Record<string, { root: string; meta: any }>,
) {
mockHost.readJson.mockImplementation((file) => {
const result = Object.values(fakePackages).find(
({ root }) => file === join(root, 'package.json'),
)?.meta;
return result != null
? Promise.resolve(result)
: Promise.reject(new Error(`Unexpected file access: ${file}`));
});

mockHost.findDependencyDirectory.mockImplementation(async (dep, _dir) => {
const result = fakePackages[dep]?.root;
if (result == null) {
throw new Error(`Unknown dependency: ${dep}`);
}
return result;
});
}