Skip to content

Commit a65d411

Browse files
Fix zkProgramFile to support nested paths (#690)
1 parent aab2288 commit a65d411

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## Unreleased
1919

20+
### Fixed
21+
22+
- Fixed zkProgramFile to support nested paths. [#690](https://github.com/o1-labs/zkapp-cli/pull/690)
23+
2024
## [0.21.5](https://github.com/o1-labs/zkapp-cli/compare/0.21.4...0.21.5) - 2024-06-06
2125

2226
### Changed

src/lib/deploy.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,19 @@ async function findZkProgramFile(buildPath, zkProgramNameArg) {
792792
// eslint-disable-next-line no-unused-vars
793793
const [_, zkProgramVarName, nameArg] = match;
794794

795+
const buildSrcPath = buildPath.replace('**/*.js', 'src');
796+
const relativePath = path.relative(buildSrcPath, file);
797+
798+
const isNested =
799+
!relativePath.startsWith('..') && !path.isAbsolute(relativePath);
800+
801+
const zkProgramFile = isNested ? relativePath : path.basename(file);
802+
795803
if (nameArg === zkProgramNameArg) {
796-
return { zkProgramVarName, zkProgramFile: path.basename(file) };
804+
return {
805+
zkProgramVarName,
806+
zkProgramFile,
807+
};
797808
}
798809
}
799810
}

src/lib/deploy.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ jest.unstable_mockModule('node:path', () => ({
6161
resolve: jest.fn(),
6262
dirname: jest.fn(),
6363
sep: '/',
64+
relative: jest.fn().mockImplementation((from, to) => {
65+
const fromParts = from.split('/');
66+
const toParts = to.split('/');
67+
68+
let commonLength = 0;
69+
for (let i = 0; i < Math.min(fromParts.length, toParts.length); i++) {
70+
if (fromParts[i] !== toParts[i]) break;
71+
commonLength++;
72+
}
73+
74+
const up = fromParts.slice(commonLength).map(() => '..');
75+
const down = toParts.slice(commonLength);
76+
77+
return [...up, ...down].join('/') || '.';
78+
}),
6479
},
6580
}));
6681

@@ -1075,6 +1090,31 @@ describe('deploy.js', () => {
10751090
);
10761091
});
10771092

1093+
it('should return the ZkProgram when found in nested folders', async () => {
1094+
const projectRoot = '/some/path/';
1095+
const zkProgramNameArg = 'myZkProgram';
1096+
const zkProgramFile = 'proofs/file1.js';
1097+
const zkProgramMock = { name: 'myZkProgram' };
1098+
glob.mockResolvedValue(['/some/path/file1.js']);
1099+
fs.readFileSync.mockReturnValue(
1100+
`
1101+
const myVar = ZkProgram({
1102+
name: 'myZkProgram'
1103+
});
1104+
`
1105+
);
1106+
jest.spyOn(path, 'basename').mockReturnValue('proofs/file1.js');
1107+
dynamicImport.mockResolvedValue({ myVar: zkProgramMock });
1108+
const { getZkProgram } = await import('./deploy.js');
1109+
1110+
const result = await getZkProgram(projectRoot, zkProgramNameArg);
1111+
1112+
expect(result).toBe(zkProgramMock);
1113+
expect(dynamicImport).toHaveBeenCalledWith(
1114+
`${projectRoot}/build/src/${zkProgramFile}`
1115+
);
1116+
});
1117+
10781118
it('should handle Windows paths correctly', async () => {
10791119
const originalPlatform = process.platform;
10801120
try {

0 commit comments

Comments
 (0)