Skip to content
Open
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: 3 additions & 1 deletion hardhat/hardhat-exposed/hook-handlers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ export default async (): Promise<Partial<ConfigHooks>> => ({
resolveUserConfig: async (userConfig, resolveConfigurationVariable, next) => {
const partiallyResolvedConfig = await next(userConfig, resolveConfigurationVariable);

// Patterns starting with `npm:` match the npm root paths of the files listed in `solidity.npmFilesToBuild` (e.g.
// `npm:@openzeppelin/contracts/**/*.sol`), and are kept as-is.
const makeAbsolutePath = (p: string) =>
path.isAbsolute(p) ? p : path.resolve(partiallyResolvedConfig.paths.root, p);
path.isAbsolute(p) || p.startsWith('npm:') ? p : path.resolve(partiallyResolvedConfig.paths.root, p);

// Note: the output directory is deliberately NOT added to `paths.sources.solidity` here. The `build` task adds it,
// in place, right after generating its content. That way tasks that don't generate the exposed contracts (docgen,
Expand Down
77 changes: 59 additions & 18 deletions hardhat/hardhat-exposed/internal/expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,40 @@ type ExposedFile = {
content: string;
};

type ParsedRemapping = { prefix: string; target: string };
// `exact` remappings apply to a single file, rather than to every file whose input source name starts with `target`.
type ParsedRemapping = { prefix: string; target: string; exact?: boolean };

export function getExposed(
solidityBuildInfo: SolidityBuildInfo,
solcOutput: CompilerOutput,
config: HardhatConfig,
): Map<string, string> {
const res = new Map<string, string>();
// input source name -> name it was listed with, for every exposed npm root (see `npmFilesToBuild`), including the
// ones that are not part of this build info because they are cached
npmRoots: Map<string, string>,
): {
// exposed file absolute path -> content
files: Map<string, string>;
// exposed files that are needed by an import (see the `imports` option), including the ones of npm roots, which may
// not be part of `files` if they are cached
imported: Set<string>;
} {
const files = new Map<string, string>();
const imported = new Set<string>();
const deref = astDereferencer(solcOutput);
const projectSourcePrefixes = getProjectSourcePrefixes(config);
const applicableRemappings = parseApplicableRemappings(
solidityBuildInfo.input.settings.remappings ?? [],
projectSourcePrefixes,
);
const applicableRemappings: ParsedRemapping[] = [
// npm roots are referred to by the name they were listed with, which may differ from their path in the package when
// it uses `package.json#exports`.
...Array.from(npmRoots, ([inputSourceName, name]) => ({ prefix: name, target: inputSourceName, exact: true })),
...parseApplicableRemappings(solidityBuildInfo.input.settings.remappings ?? [], projectSourcePrefixes),
];

for (const [sourceName, inputSourceName] of Object.entries(solidityBuildInfo.userSourceNameMap)) {
const ast = solcOutput.sources[inputSourceName].ast;

const exposedFile = getExposedFile(sourceName, inputSourceName, ast, deref, config, applicableRemappings);
if (exposedFile !== undefined) {
res.set(exposedFile.absolutePath, exposedFile.content);
files.set(exposedFile.absolutePath, exposedFile.content);
}
}

Expand All @@ -61,6 +74,20 @@ export function getExposed(
)) {
const ast = solcOutput.sources[inputSourceName].ast;

// An npm root's exposed file already exposes all of its contracts: it must not be replaced, even when it is not
// regenerated here because the npm root is cached.
if (npmRoots.has(inputSourceName)) {
imported.add(
getExposedFileAbsolutePath(
inputSourceName.replace(/^project\//, ''),
inputSourceName,
config,
applicableRemappings,
),
);
continue;
}

// sourceName is a fallback path for vendored `project/` deps not covered by a remapping.
const exposedFile = getExposedFile(
inputSourceName.replace(/^project\//, ''),
Expand All @@ -72,15 +99,17 @@ export function getExposed(
c => contracts.has(c),
);
if (exposedFile !== undefined) {
res.set(exposedFile.absolutePath, exposedFile.content);
files.set(exposedFile.absolutePath, exposedFile.content);
imported.add(exposedFile.absolutePath);
}
}
}

return res;
return { files, imported };
}

// Non-interface contracts named in an import from outside the project sources, grouped by declaring file.
// Non-interface contracts named in an import from outside the project sources, grouped by declaring file. npm roots
// (see `npmFilesToBuild`) are not project sources: the imports of their contracts are reported too.
// Blanket imports contribute nothing: only `import {X} from '...'` symbols count.
function getImportedContracts(
solidityBuildInfo: SolidityBuildInfo,
Expand All @@ -90,7 +119,8 @@ function getImportedContracts(
): Map<string, Set<ContractDefinition>> {
const userInputSourceNames = new Set(Object.values(solidityBuildInfo.userSourceNameMap));
const isCoveredByUserSources = (inputSourceName: string) =>
userInputSourceNames.has(inputSourceName) || projectSourcePrefixes.some(p => inputSourceName.startsWith(p));
(userInputSourceNames.has(inputSourceName) && !inputSourceName.startsWith('npm/')) ||
projectSourcePrefixes.some(p => inputSourceName.startsWith(p));

const res = new Map<string, Set<ContractDefinition>>();

Expand Down Expand Up @@ -180,7 +210,9 @@ function parseApplicableRemappings(remappings: string[], projectSourcePrefixes:
}

function getRemappedImportPath(inputSourceName: string, applicableRemappings: ParsedRemapping[]): string | undefined {
const item = applicableRemappings.find(({ target }) => inputSourceName.startsWith(target));
const item = applicableRemappings.find(({ target, exact }) =>
exact ? inputSourceName === target : inputSourceName.startsWith(target),
);
return item && item.prefix + inputSourceName.slice(item.target.length);
}

Expand All @@ -198,11 +230,7 @@ function getExposedFile(
applicableRemappings: ParsedRemapping[],
filter?: (c: ContractDefinition) => boolean,
): ExposedFile | undefined {
const exposedFileAbsolutePath = path.join(
config.exposed.outDir,
getRemappedImportPath(inputSourceName, applicableRemappings) ??
(inputSourceName.startsWith('project/') ? sourceName : getNpmImportPath(inputSourceName)),
);
const exposedFileAbsolutePath = getExposedFileAbsolutePath(sourceName, inputSourceName, config, applicableRemappings);

const content = getExposedContent(
exposedFileAbsolutePath,
Expand All @@ -218,6 +246,19 @@ function getExposedFile(
return content === undefined ? undefined : { absolutePath: exposedFileAbsolutePath, content };
}

function getExposedFileAbsolutePath(
sourceName: string,
inputSourceName: string,
config: HardhatConfig,
applicableRemappings: ParsedRemapping[],
): string {
return path.join(
config.exposed.outDir,
getRemappedImportPath(inputSourceName, applicableRemappings) ??
(inputSourceName.startsWith('project/') ? sourceName : getNpmImportPath(inputSourceName)),
);
}

function getExposedContent(
exposedFileAbsolutePath: string,
ast: SourceUnit,
Expand Down
3 changes: 2 additions & 1 deletion hardhat/hardhat-exposed/internal/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ export class ExposedManifest {
}
}

// npm roots (`npm:<module>`, see `npmFilesToBuild`) are not paths: they are stored as-is.
private relative(p: string): string {
return path.relative(this.rootDir, p);
return p.startsWith('npm:') ? p : path.relative(this.rootDir, p);
}

private absolute(p: string): string {
Expand Down
45 changes: 36 additions & 9 deletions hardhat/hardhat-exposed/tasks/generate-exposed-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSpinner } from '@nomicfoundation/hardhat-utils/spinner';

import type { HardhatRuntimeEnvironment } from 'hardhat/types/hre';
import type { CompilationJob, SolidityBuildInfo } from 'hardhat/types/solidity';
import { ResolvedFileType } from 'hardhat/types/solidity';
import type { Result } from 'hardhat/types/utils';
import assert from 'node:assert';
import path from 'node:path';
Expand Down Expand Up @@ -56,12 +57,26 @@ export default async function generateExposedContracts(

const filteredRootPathsToExpose = rootPathsToExpose.filter(p => force || !compilationJobs.cacheHits.has(p));

// Input source name of each exposed npm root (see `npmFilesToBuild`), mapped to the name it was listed with. Cached
// ones are included, as they are absent from the compilation jobs of the roots that import them.
const npmRoots = new Map(
rootPathsToExpose
.filter(p => p.startsWith('npm:'))
.flatMap(p =>
Array.from(compilationJobs.indexedIndividualJobs.get(p)!.dependencyGraph.getRoots().values(), f => [
f.inputSourceName,
p.slice('npm:'.length),
]),
Comment thread
Copilot marked this conversation as resolved.
Resolved
),
);

// Roots of the compilation job, restricted to the ones being exposed. All of them are regenerated together.
const rootPathsToExposeSet = new Set(rootPathsToExpose);
const getExposedRoots = (compilationJob: CompilationJob) =>
Array.from(compilationJob.dependencyGraph.getRoots().values(), f => f.fsPath).filter(p =>
rootPathsToExposeSet.has(p),
);
// Root paths are formatted as in `getRootFilePaths`: `npm:<module>` for npm roots (see `npmFilesToBuild`).
Array.from(compilationJob.dependencyGraph.getRoots(), ([userSourceName, f]) =>
f.type === ResolvedFileType.NPM_PACKAGE_FILE ? `npm:${userSourceName}` : f.fsPath,
).filter(p => rootPathsToExposeSet.has(p));

const astOnlyBuildInfos = new Map<SolidityBuildInfo, string[]>();
const compilationJobIds = new Set<string>();
Expand Down Expand Up @@ -101,22 +116,34 @@ export default async function generateExposedContracts(
return errorResult();
}

const exposed = getExposed(buildInfo, buildOutput, hre.config);
const exposed = getExposed(buildInfo, buildOutput, hre.config, npmRoots);

for (const [exposedPath, exposedContent] of exposed) {
for (const [exposedPath, exposedContent] of exposed.files) {
fs.mkdirSync(path.dirname(exposedPath), { recursive: true });
fs.writeFileSync(exposedPath, exposedContent);
exposedPaths.add(exposedPath);
}

// The exposed file of a root mirrors its path. Other exposed files (see the `imports` option) are attributed to
// all the roots of the job, as any of them may be the one importing it.
// The exposed file of a root mirrors its path (the name it was listed with for an npm root, see
// `npmFilesToBuild`). Exposed files needed by an import (see the `imports` option) are attributed to all the
// roots of the job, as any of them may be the one importing it. An npm root's exposed file can be both.
const exposedPathToRoot = new Map(
roots.map(root => [path.join(hre.config.exposed.outDir, path.relative(hre.config.paths.root, root)), root]),
roots.map(root => [
path.join(
hre.config.exposed.outDir,
root.startsWith('npm:') ? root.slice('npm:'.length) : path.relative(hre.config.paths.root, root),
Comment thread
Copilot marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
),
root,
]),
);
manifest.setGenerated(
roots,
new Map(Array.from(exposed.keys(), p => [p, exposedPathToRoot.has(p) ? [exposedPathToRoot.get(p)!] : roots])),
new Map(
Array.from(new Set([...exposed.files.keys(), ...exposed.imported]), p => [
p,
exposedPathToRoot.has(p) && !exposed.imported.has(p) ? [exposedPathToRoot.get(p)!] : roots,
]),
),
);
}
} finally {
Expand Down
111 changes: 109 additions & 2 deletions hardhat/hardhat-exposed/test/incremental-generation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ describe('hardhat-exposed: incremental generation', function () {
};
this.manifest = () => path.join(this.outDir, 'manifest.json');
// A new environment per build mimics separate CLI invocations
this.build = async exposed => {
this.build = async (exposed, npmFilesToBuild = []) => {
const hre = await createHardhatRuntimeEnvironment(
{ plugins: [hardhatExposed], solidity: '0.8.35', paths: { sources: 'contracts' }, exposed },
{
plugins: [hardhatExposed],
solidity: { version: '0.8.35', npmFilesToBuild },
paths: { sources: 'contracts' },
exposed,
},
{},
this.root,
);
Expand Down Expand Up @@ -205,6 +210,108 @@ describe('hardhat-exposed: incremental generation', function () {
});
});

describe('npm files', function () {
beforeEach(function () {
const dependency = path.join(this.root, 'node_modules', 'dep');
fs.mkdirSync(dependency, { recursive: true });
fs.writeFileSync(path.join(dependency, 'package.json'), '{"name":"dep","version":"1.0.0"}');
fs.writeFileSync(path.join(dependency, 'Dep.sol'), contract('Dep'));
this.write('A');
});

it('generates a wrapper for an npm file to build that is included', async function () {
await this.build({ include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] }, ['dep/Dep.sol']);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol', 'dep/Dep.sol']);
});

it('does not generate a wrapper for an npm file to build that is not included', async function () {
await this.build({ include: ['contracts/**/*.sol'] }, ['dep/Dep.sol']);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol']);
});

it('does not generate a wrapper for an npm file to build that is excluded', async function () {
await this.build({ include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'], exclude: ['npm:dep/Dep.sol'] }, [
'dep/Dep.sol',
]);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol']);
});

it('removes the wrapper of an npm file that is no longer built', async function () {
await this.build({ include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] }, ['dep/Dep.sol']);
await this.build({ include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] });
expect(this.wrappers()).to.deep.equal(['contracts/A.sol']);
});

it('keeps the wrapper of an npm file when building from another directory', async function () {
const exposed = { include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] };
await this.build(exposed, ['dep/Dep.sol']);
const cwd = process.cwd();
try {
process.chdir(this.root);
await this.build(exposed, ['dep/Dep.sol']);
} finally {
process.chdir(cwd);
}
expect(this.wrappers()).to.deep.equal(['contracts/A.sol', 'dep/Dep.sol']);
});

it('keeps the wrapper of an npm file that is no longer built while a source still imports it', async function () {
fs.writeFileSync(
this.source('Importer'),
contract('Importer').replace('contract Importer', 'import {Dep} from "dep/Dep.sol";\ncontract Importer is Dep'),
);
const exposed = { imports: true, include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] };
await this.build(exposed, ['dep/Dep.sol']);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol', 'contracts/Importer.sol', 'dep/Dep.sol']);
await this.build(exposed);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol', 'contracts/Importer.sol', 'dep/Dep.sol']);
});

it('keeps the full wrapper of an npm file when only a source importing part of it changes', async function () {
const dependency = path.join(this.root, 'node_modules', 'dep');
fs.writeFileSync(path.join(dependency, 'Dep.sol'), contract('Dep') + 'contract Extra {}\n');
fs.writeFileSync(
this.source('Importer'),
contract('Importer').replace('contract Importer', 'import {Dep} from "dep/Dep.sol";\ncontract Importer is Dep'),
);
const exposed = { imports: true, include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] };
await this.build(exposed, ['dep/Dep.sol']);
const wrapper = path.join(this.outDir, 'dep', 'Dep.sol');
expect(fs.readFileSync(wrapper, 'utf8')).to.include('contract $Extra');
fs.appendFileSync(this.source('Importer'), '// modified\n');
await this.build(exposed, ['dep/Dep.sol']);
expect(fs.readFileSync(wrapper, 'utf8')).to.include('contract $Extra');
});

it('generates the wrappers of npm files whose exported paths are prefixes of one another', async function () {
const dependency = path.join(this.root, 'node_modules', 'dep');
fs.mkdirSync(path.join(dependency, 'src'));
fs.renameSync(path.join(dependency, 'Dep.sol'), path.join(dependency, 'src', 'Dep.sol'));
fs.writeFileSync(path.join(dependency, 'src', 'Dep.solExtra.sol'), contract('Extra'));
fs.writeFileSync(
path.join(dependency, 'package.json'),
'{"name":"dep","version":"1.0.0","exports":{"./Dep.sol":"./src/Dep.sol","./Extra.sol":"./src/Dep.solExtra.sol"}}',
);
await this.build({ include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] }, ['dep/Dep.sol', 'dep/Extra.sol']);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol', 'dep/Dep.sol', 'dep/Extra.sol']);
});

it('generates the wrapper of an npm file at its exported path', async function () {
const dependency = path.join(this.root, 'node_modules', 'dep');
fs.mkdirSync(path.join(dependency, 'src'));
fs.renameSync(path.join(dependency, 'Dep.sol'), path.join(dependency, 'src', 'Dep.sol'));
fs.writeFileSync(
path.join(dependency, 'package.json'),
'{"name":"dep","version":"1.0.0","exports":{"./Dep.sol":"./src/Dep.sol"}}',
);
const exposed = { include: ['contracts/**/*.sol', 'npm:dep/**/*.sol'] };
await this.build(exposed, ['dep/Dep.sol']);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol', 'dep/Dep.sol']);
await this.build(exposed);
expect(this.wrappers()).to.deep.equal(['contracts/A.sol']);
});
});

describe('imports option', function () {
beforeEach(function () {
const dependency = path.join(this.root, 'node_modules', 'dep');
Expand Down
Loading