Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 15 additions & 6 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 @@ -59,9 +60,10 @@ export default async function generateExposedContracts(
// 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 @@ -109,10 +111,17 @@ export default async function generateExposedContracts(
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 (its module path for an npm root, see `npmFilesToBuild`). 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.
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,
Expand Down
40 changes: 38 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,37 @@ 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({ exclude: ['npm:dep/**/*.sol'] }, ['dep/Dep.sol']);
Comment thread
Copilot marked this conversation as resolved.
Resolved
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']);
});
});

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