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 zkProgramFile to support nested paths #690

Merged
merged 5 commits into from
Aug 21, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## Unreleased

### Fixed

- Fixed zkProgramFile to support nested paths. [#690](https://github.com/o1-labs/zkapp-cli/pull/690)

## [0.21.5](https://github.com/o1-labs/zkapp-cli/compare/0.21.4...0.21.5) - 2024-06-06

### Changed
Expand Down
13 changes: 12 additions & 1 deletion src/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,19 @@ async function findZkProgramFile(buildPath, zkProgramNameArg) {
// eslint-disable-next-line no-unused-vars
const [_, zkProgramVarName, nameArg] = match;

const buildSrcPath = buildPath.replace('**/*.js', 'src');
const relativePath = path.relative(buildSrcPath, file);

const isNested =
!relativePath.startsWith('..') && !path.isAbsolute(relativePath);

const zkProgramFile = isNested ? relativePath : path.basename(file);

if (nameArg === zkProgramNameArg) {
return { zkProgramVarName, zkProgramFile: path.basename(file) };
return {
zkProgramVarName,
zkProgramFile,
};
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/lib/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ jest.unstable_mockModule('node:path', () => ({
resolve: jest.fn(),
dirname: jest.fn(),
sep: '/',
relative: jest.fn().mockImplementation((from, to) => {
const fromParts = from.split('/');
const toParts = to.split('/');

let commonLength = 0;
for (let i = 0; i < Math.min(fromParts.length, toParts.length); i++) {
if (fromParts[i] !== toParts[i]) break;
commonLength++;
}

const up = fromParts.slice(commonLength).map(() => '..');
const down = toParts.slice(commonLength);

return [...up, ...down].join('/') || '.';
}),
},
}));

Expand Down Expand Up @@ -1075,6 +1090,31 @@ describe('deploy.js', () => {
);
});

it('should return the ZkProgram when found in nested folders', async () => {
const projectRoot = '/some/path/';
const zkProgramNameArg = 'myZkProgram';
const zkProgramFile = 'proofs/file1.js';
const zkProgramMock = { name: 'myZkProgram' };
glob.mockResolvedValue(['/some/path/file1.js']);
fs.readFileSync.mockReturnValue(
`
const myVar = ZkProgram({
name: 'myZkProgram'
});
`
);
jest.spyOn(path, 'basename').mockReturnValue('proofs/file1.js');
dynamicImport.mockResolvedValue({ myVar: zkProgramMock });
const { getZkProgram } = await import('./deploy.js');

const result = await getZkProgram(projectRoot, zkProgramNameArg);

expect(result).toBe(zkProgramMock);
expect(dynamicImport).toHaveBeenCalledWith(
`${projectRoot}/build/src/${zkProgramFile}`
);
});

it('should handle Windows paths correctly', async () => {
const originalPlatform = process.platform;
try {
Expand Down