Skip to content

Commit

Permalink
fix: extract whole archive instead of subset (#3604)
Browse files Browse the repository at this point in the history
This PR changes the behaviour of the dependency resolver in
noir-compiler to extract a whole zip archiver from Github instead of
cherry-picking just the given path. This makes it so that relative
dependencies inside the archive are resolved correctly with the tradeoff
that now it extract everything (e.g. aztec-packages extracts 80mb but
aztec-nr is only ~1MB of code)
  • Loading branch information
alexghr authored Dec 6, 2023
1 parent 2f08423 commit cb000d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ export class GithubDependencyResolver implements NoirDependencyResolver {

async #extractZip(dependency: NoirGitDependencyConfig, archivePath: string): Promise<string> {
const gitUrl = new URL(dependency.git);
// extract the archive to this location
const extractLocation = join('libs', safeFilename(gitUrl.pathname + '@' + (dependency.tag ?? 'HEAD')));
const tmpExtractLocation = extractLocation + '.tmp';

// where we expect to find this package after extraction
// it might already exist if the archive got unzipped previously
const packagePath = join(extractLocation, dependency.directory ?? '');

if (this.#fm.hasFileSync(packagePath)) {
Expand All @@ -82,24 +85,21 @@ export class GithubDependencyResolver implements NoirDependencyResolver {

const { entries } = await unzip(this.#fm.readFileSync(archivePath));

// extract to a temporary directory, then move it to the final location
// TODO empty the temp directory first
const tmpExtractLocation = extractLocation + '.tmp';
for (const entry of Object.values(entries)) {
if (entry.isDirectory) {
continue;
}

// remove the first path segment, because it'll be the archive name
const name = stripSegments(entry.name, 1);
if (dependency.directory && !name.startsWith(dependency.directory)) {
continue;
}
const path = join(tmpExtractLocation, name);
await this.#fm.writeFile(path, (await entry.blob()).stream());
}

if (dependency.directory) {
this.#fm.moveFileSync(join(tmpExtractLocation, dependency.directory), packagePath);
} else {
this.#fm.moveFileSync(tmpExtractLocation, packagePath);
}
this.#fm.moveFileSync(tmpExtractLocation, extractLocation);

return packagePath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NoirDependencyConfig } from '@aztec/foundation/noir';

import { resolve } from 'path';
import { isAbsolute, join } from 'path';

import { FileManager } from '../file-manager/file-manager.js';
import { NoirPackage } from '../package.js';
Expand All @@ -16,12 +16,14 @@ export class LocalDependencyResolver implements NoirDependencyResolver {
this.#fm = fm;
}

resolveDependency(pkg: NoirPackage, config: NoirDependencyConfig): Promise<NoirDependency | null> {
resolveDependency(parent: NoirPackage, config: NoirDependencyConfig): Promise<NoirDependency | null> {
if ('path' in config) {
const parentPath = parent.getPackagePath();
const dependencyPath = isAbsolute(config.path) ? config.path : join(parentPath, config.path);
return Promise.resolve({
// unknown version, Nargo.toml doesn't have a version field
version: undefined,
package: NoirPackage.open(resolve(pkg.getPackagePath(), config.path), this.#fm),
package: NoirPackage.open(dependencyPath, this.#fm),
});
} else {
return Promise.resolve(null);
Expand Down

0 comments on commit cb000d8

Please sign in to comment.