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: noir-compiler breadth-first resolver #3307

Merged
merged 2 commits into from
Nov 17, 2023
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
2 changes: 0 additions & 2 deletions yarn-project/noir-compiler/src/cli/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ export function compileContract(program: Command, name = 'contract', log: LogFn
const tsPath = resolve(projectPath, typescript, `${contract.name}.ts`);
log(`Writing ${contract.name} typescript interface to ${path.relative(currentDir, tsPath)}`);
let relativeArtifactPath = path.relative(path.dirname(tsPath), artifactPath);
log(`Relative path: ${relativeArtifactPath}`);
if (relativeArtifactPath === `${contract.name}.json`) {
// relative path edge case, prepending ./ for local import - the above logic just does
// `${contract.name}.json`, which is not a valid import for a file in the same directory
relativeArtifactPath = `./${contract.name}.json`;
}
log(`Relative path after correction: ${relativeArtifactPath}`);
const tsWrapper = generateTypescriptContractInterface(contract, relativeArtifactPath);
mkdirpSync(path.dirname(tsPath));
writeFileSync(tsPath, tsWrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('DependencyManager', () => {
lib2: {
path: '/lib2',
},
lib3: {
path: '/lib3',
},
},
package: {
name: 'test_contract',
Expand All @@ -38,7 +41,7 @@ describe('DependencyManager', () => {

it('resolves root dependencies', async () => {
await manager.resolveDependencies();
expect(manager.getEntrypointDependencies()).toEqual(['lib1', 'lib2']);
expect(manager.getEntrypointDependencies()).toEqual(['lib1', 'lib2', 'lib3']);
});

it('resolves library dependencies', async () => {
Expand Down Expand Up @@ -75,7 +78,7 @@ class TestDependencyResolver implements NoirDependencyResolver {
package: new NoirPackage('/lib2', '/lib2/src', {
dependencies: {
lib3: {
path: '/lib3',
path: '../lib3',
},
},
package: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class NoirDependencyManager {
* Resolves dependencies for a package.
*/
public async resolveDependencies(): Promise<void> {
await this.#recursivelyResolveDependencies('', this.#entryPoint);
await this.#breadthFirstResolveDependencies();
}

/**
Expand All @@ -59,26 +59,46 @@ export class NoirDependencyManager {
return dep?.version;
}

async #recursivelyResolveDependencies(packageName: string, noirPackage: NoirPackage): Promise<void> {
for (const [name, config] of Object.entries(noirPackage.getDependencies())) {
// TODO what happens if more than one package has the same name but different versions?
if (this.#libraries.has(name)) {
this.#log(`skipping already resolved dependency ${name}`);
async #breadthFirstResolveDependencies(): Promise<void> {
/** Represents a package to resolve dependencies for */
type Job = {
/** Package name */
packageName: string;
/** The package location */
noirPackage: NoirPackage;
};

const queue: Job[] = [
{
packageName: '',
noirPackage: this.#entryPoint,
},
];

while (queue.length > 0) {
const { packageName, noirPackage } = queue.shift()!;
for (const [name, config] of Object.entries(noirPackage.getDependencies())) {
// TODO what happens if more than one package has the same name but different versions?
if (this.#libraries.has(name)) {
this.#log(`skipping already resolved dependency ${name}`);
this.#dependencies.set(packageName, [...(this.#dependencies.get(packageName) ?? []), name]);

continue;
}
const dependency = await this.#resolveDependency(noirPackage, config);
if (dependency.package.getType() !== 'lib') {
this.#log(`Non-library package ${name}`, config);
throw new Error(`Dependency ${name} is not a library`);
}

this.#libraries.set(name, dependency);
this.#dependencies.set(packageName, [...(this.#dependencies.get(packageName) ?? []), name]);

continue;
queue.push({
noirPackage: dependency.package,
packageName: name,
});
}

const dependency = await this.#resolveDependency(noirPackage, config);
if (dependency.package.getType() !== 'lib') {
this.#log(`Non-library package ${name}`, config);
throw new Error(`Dependency ${name} is not a library`);
}

this.#libraries.set(name, dependency);
this.#dependencies.set(packageName, [...(this.#dependencies.get(packageName) ?? []), name]);

await this.#recursivelyResolveDependencies(name, dependency.package);
}
}

Expand Down