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

Support package exports #6125

Merged
merged 19 commits into from
Jan 31, 2025
Merged

Support package exports #6125

merged 19 commits into from
Jan 31, 2025

Conversation

antico5
Copy link
Contributor

@antico5 antico5 commented Jan 8, 2025

This PR adds support for package.json#exports for packages that distribute solidity files.

Implementation details:

  • Source names for npm modules use the subpath without resolving, e.g. npm/[email protected]/Foo.sol instead of npm/[email protected]/src/Foo.sol, for an import of the kind import 'mypkg/Foo.sol'. This is arbitrary
  • Module resolving was changed from using require.resolve to manual lookup using the help of require.resolve.paths, to support packages that don't export their package.json
  • Exports resolving make use of the resolve.exports package. In this case we avoid using require.resolve because it may resolve to different packages in the filesystem for a given package name and version.

Known issues:

  • When trying to import a module with wrong casing, in a module that uses exports, I'm not sure how we can suggest the correct casing. E.g. mypkg/TheContract.sol resolves to mypkg/src/TheContract.sol. If user does import 'mypkg/thecontract.sol', I know that the correct resolved path is src/TheContract.sol, but I'm not sure how to it back to TheContract.sol. Asking for suggestions on how to approach this

Closes #5992

@antico5 antico5 added the v-next A Hardhat v3 development task label Jan 8, 2025
@antico5 antico5 requested a review from alcuadrado January 8, 2025 17:36
Copy link

vercel bot commented Jan 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 31, 2025 6:12pm

Copy link

changeset-bot bot commented Jan 8, 2025

⚠️ No Changeset found

Latest commit: 44dd929

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package "@nomicfoundation/ignition-core" depends on the ignored package "hardhat", but "@nomicfoundation/ignition-core" is not being ignored. Please add "@nomicfoundation/ignition-core" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-ethers" depends on the ignored package "hardhat", but "@nomicfoundation/hardhat-ignition-ethers" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-ethers" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-viem" depends on the ignored package "hardhat", but "@nomicfoundation/hardhat-ignition-viem" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-viem" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "hardhat", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-ethers" depends on the ignored package "@nomicfoundation/hardhat-ethers", but "@nomicfoundation/hardhat-ignition-ethers" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-ethers" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "@nomicfoundation/hardhat-network-helpers", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "@nomicfoundation/hardhat-verify", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-viem" depends on the ignored package "@nomicfoundation/hardhat-viem", but "@nomicfoundation/hardhat-ignition-viem" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-viem" to the `ignore` option.
The package "@nomicfoundation/ignition-core" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/ignition-core" is not being ignored. Please add "@nomicfoundation/ignition-core" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-ethers" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/hardhat-ignition-ethers" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-ethers" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-viem" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/hardhat-ignition-viem" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-viem" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.

Copy link
Contributor

github-actions bot commented Jan 8, 2025

hardhat

Total size of the bundle: 213M
Total number of dependencies (including transitive): 54

List of dependencies (sorted by size)
208M	total
29M	@ignored/edr-optimism-linux-x64-musl
29M	@ignored/edr-optimism-linux-x64-gnu
26M	@ignored/edr-optimism-linux-arm64-musl
26M	@ignored/edr-optimism-linux-arm64-gnu
22M	@ignored/edr-optimism-win32-x64-msvc
20M	esbuild
20M	@ignored/edr-optimism-darwin-x64
19M	@ignored/edr-optimism-darwin-arm64
2.8M	@sentry/tracing
2.5M	micro-eth-signer
1.9M	@noble/curves
1.7M	undici
1.2M	@sentry/types
1.2M	@noble/hashes
932K	@sentry/node
920K	@sentry/utils
856K	zod
832K	@ignored/hardhat-vnext-utils
624K	micro-packed
576K	tsx
548K	@sentry/core
544K	fast-equals
492K	@scure/bip39
460K	@ignored/edr
368K	ethereum-cryptography
344K	@sentry/hub
324K	@ignored/hardhat-vnext-errors
320K	enquirer
288K	semver
264K	@ignored/edr-optimism
192K	ws
168K	@scure/base
136K	get-tsconfig
136K	adm-zip
96K	@scure/bip32
92K	chalk
88K	tslib
88K	@sentry/minimal
76K	agent-base
72K	@nomicfoundation/solidity-analyzer
68K	debug
64K	lru_map
64K	https-proxy-agent
60K	@ignored/hardhat-vnext-zod-utils
56K	rfdc
48K	ansi-colors
44K	resolve.exports
40K	resolve-pkg-maps
36K	p-map
32K	cookie
24K	strip-ansi
24K	env-paths
24K	ansi-regex
20K	ms

Comment on lines 128 to 129
await fs.promises.access(packageJsonPath, fs.constants.R_OK);
return await fs.promises.realpath(packageJsonPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the utils' fs module

* @param packageName the name of the package to find
* @returns the absolute real path (resolved symlinks) of the package.json
*/
export async function findPackageJson(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should at at least one or two tests of this

@@ -1,3 +1,5 @@
import type { Exports } from "resolve.exports";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should duplicate this type to avoid re-exporting a dependency type

@alcuadrado
Copy link
Member

Note to self:

Double check that this situation:

import "foo/a.sol";

where foo has

exports: {
  "./*.sol": "./src/*.sol"
}

and ./src/a.sol doesn't exist.

throws that "foo/a.sol" doesn't exist, and not "foo/src/a.sol".

@alcuadrado
Copy link
Member

@antico5: my changes are ready to review

@alcuadrado alcuadrado force-pushed the support-package-exports branch from 096846c to 9543f4e Compare January 31, 2025 17:54
@alcuadrado alcuadrado enabled auto-merge January 31, 2025 17:54
@alcuadrado alcuadrado added this pull request to the merge queue Jan 31, 2025
Merged via the queue into v-next with commit a3e1f12 Jan 31, 2025
177 checks passed
@alcuadrado alcuadrado deleted the support-package-exports branch January 31, 2025 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready This issue is ready to be worked on v-next A Hardhat v3 development task
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

npm package export support within Solidity import resolution
3 participants