Skip to content

Commit

Permalink
Fix bug with names with spaces (#65)
Browse files Browse the repository at this point in the history
* Fix bug with names with spaces

* Add paths validation
  • Loading branch information
Hrom131 authored Dec 17, 2024
1 parent ff78a4b commit ebcc9fe
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-zkit",
"version": "0.5.3",
"version": "0.5.4",
"description": "The ultimate TypeScript environment for Circom development",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
30 changes: 30 additions & 0 deletions src/core/compiler/CircomCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ abstract class BaseCircomCompiler implements ICircomCompiler {
* @returns An array of strings representing the compilation parameters that will be passed to the compiler
*/
public getCompilationArgs(config: CompileConfig): string[] {
this._validateCompileConfig(config);

const args: string[] = this._getBaseCompilationArgs(config);

for (const [key, value] of Object.entries(config.compileFlags)) {
Expand All @@ -71,6 +73,19 @@ abstract class BaseCircomCompiler implements ICircomCompiler {

return args;
}

/**
* Validates the provided compilation configuration to ensure that file paths do not contain invalid characters
*
* @param config The configuration object containing paths for the circuit file and artifacts
* @throws HardhatZKitError If the `circuitFullPath` or `artifactsFullPath` contains double quotes,
* as they can cause issues during the compilation process
*/
protected _validateCompileConfig(config: CompileConfig) {
if (config.circuitFullPath.includes('"') || config.artifactsFullPath.includes('"')) {
throw new HardhatZKitError("Circuit file path must not contain double quotes.");
}
}
}

/**
Expand Down Expand Up @@ -107,6 +122,21 @@ export class BinaryCircomCompiler extends BaseCircomCompiler {
throw new HardhatZKitError(`Compilation failed.\n${err}`);
}
}

/**
* Overrides the {@link BaseCircomCompiler._getBaseCompilationArgs | _getBaseCompilationArgs} method
* of the {@link BaseCircomCompiler} base class to construct the compilation arguments for the Circom compiler,
* ensuring that file paths containing spaces are handled correctly by wrapping them in quotes.
*/
protected _getBaseCompilationArgs(baseConfig: BaseCompileConfig): string[] {
const args = [`"${baseConfig.circuitFullPath}"`, "-o", `"${baseConfig.artifactsFullPath}"`];

for (const linkLibrary of baseConfig.linkLibraries) {
args.push("-l", `"${linkLibrary}"`);
}

return args;
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions test/unit/core/compile/circom-compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,46 @@ describe("WASMCircomCompiler", () => {

fsExtra.rmSync(artifactsFullPath, { recursive: true, force: true });
});

it("should correctly throw error if file path contains double quotes", async function () {
let circuitFullPath: string = getNormalizedFullPath(this.hre.config.paths.root, 'circuits/bas"e/mul2Base.circom');
let artifactsFullPath: string = getNormalizedFullPath(
this.hre.config.paths.root,
"zkit/artifacts/test/mul2.circom",
);
const errorFileFullPath: string = getNormalizedFullPath(artifactsFullPath, "errors.log");

fsExtra.mkdirSync(artifactsFullPath, { recursive: true });

const reason: string = "Circuit file path must not contain double quotes.";

await expect(
circomCompiler.compile({
circuitFullPath,
artifactsFullPath,
errorFileFullPath,
linkLibraries: [],
compileFlags: defaultCompileFlags,
quiet: false,
}),
).to.be.rejectedWith(reason);

circuitFullPath = getNormalizedFullPath(this.hre.config.paths.root, "circuits/base/mul2Base.circom");
artifactsFullPath = getNormalizedFullPath(this.hre.config.paths.root, 'zkit/artifacts/tes"t/mul2.circom');

await expect(
circomCompiler.compile({
circuitFullPath,
artifactsFullPath,
errorFileFullPath,
linkLibraries: [],
compileFlags: defaultCompileFlags,
quiet: false,
}),
).to.be.rejectedWith(reason);

fsExtra.rmSync(artifactsFullPath, { recursive: true, force: true });
});
});

describe("compile:with-libraries", () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/core/compile/compilation-files-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ describe("CompilationFilesResolver", () => {
const expectedSourceNames: string[] = [
"circuits/main/Multiplier3Arr.circom",
"circuits/main/mul2.circom",
"circuits/main/test.circom",
"circuits/main/test_2.circom",
"circuits/test folder/test.circom",
"circuits/test folder/test_2.circom",
"circuits/vendor/SumMul.circom",
];
const expectedCircuitNames: string[] = ["Multiplier3Arr", "Multiplier2", "Test", "Test", "SumMul"];
Expand Down

0 comments on commit ebcc9fe

Please sign in to comment.