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

Open
wants to merge 2 commits into
base: v-next
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ Remaining test suites: {suites}`,
websiteTitle: "Imported file doesn't exist",
websiteDescription: `An imported file doesn't exist.`,
},
IMPORTED_FILE_WITH_ICORRECT_CASING: {
IMPORTED_FILE_WITH_INCORRECT_CASING: {
number: 1203,
messageTemplate:
'The import "{importPath} from "{from}" exists, but its casing is incorrect. The correct casing is "{correctCasing}".',
Expand Down Expand Up @@ -1195,6 +1195,12 @@ Please check Hardhat's output for more details.`,
websiteTitle: "Invalid solcjs compiler",
websiteDescription: `Hardhat successfully downloaded a WASM version of solc {version} but it is invalid. The compile function is missing.`,
},
RESOLVE_NOT_EXPORTED_NPM_FILE: {
number: 1232,
messageTemplate: `You are tying to resolve the npm file "{module}", but it's not exported by its package`,
websiteTitle: "Resolution of not-exported npm file",
websiteDescription: `You are tying to resolve an npm file that is not exported by its package.`,
},
},
VIEM: {
NETWORK_NOT_FOUND: {
Expand Down
31 changes: 31 additions & 0 deletions v-next/hardhat-utils/src/package.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";

import { ensureError } from "./error.js";
Expand All @@ -7,6 +9,7 @@ import {
} from "./errors/package.js";
import { findUp, readJsonFile } from "./fs.js";
import { getFilePath } from "./internal/package.js";
import { ensureTrailingSlash } from "./string.js";

/**
* The structure of a `package.json` file. This is a subset of the actual
Expand Down Expand Up @@ -102,6 +105,34 @@ export async function findClosestPackageRoot(
return path.dirname(packageJsonPath);
}

/**
* Finds the package json for a given package
* @param from the absolute path from where to start the search
* @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

from: string,
packageName: string,
): Promise<string | undefined> {
const require = createRequire(ensureTrailingSlash(from));

const lookupPaths = require.resolve.paths(packageName) ?? [];

const pathToTest = [...packageName.split("/"), "package.json"];

for (const lookupPath of lookupPaths) {
const packageJsonPath = path.join(lookupPath, ...pathToTest);

try {
await fs.promises.access(packageJsonPath, fs.constants.R_OK);
return await fs.promises.realpath(packageJsonPath);
Comment on lines +128 to +129
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

} catch (error) {
continue;
}
}
}

export {
PackageJsonNotFoundError,
PackageJsonReadError,
Expand Down
7 changes: 7 additions & 0 deletions v-next/hardhat-utils/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ export function camelToSnakeCase(str: string): string {
export function camelToKebabCase(str: string): string {
return str.replace(/[A-Z0-9]/g, (match) => `-${match.toLowerCase()}`);
}

/**
* Ensures a string ends with a slash.
*/
export function ensureTrailingSlash(path: string): string {
return path.endsWith("/") ? path : path + "/";
}
4 changes: 3 additions & 1 deletion v-next/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"./types/tasks": "./dist/src/types/tasks.js",
"./types/user-interruptions": "./dist/src/types/user-interruptions.js",
"./types/utils": "./dist/src/types/utils.js",
"./types/solidity": "./dist/src/types/solidity.js"
"./types/solidity": "./dist/src/types/solidity.js",
"./console.sol": "./console.sol"
},
"keywords": [
"ethereum",
Expand Down Expand Up @@ -97,6 +98,7 @@
"ethereum-cryptography": "^2.2.1",
"micro-eth-signer": "^0.13.0",
"p-map": "^7.0.2",
"resolve.exports": "^2.0.3",
"semver": "^7.6.3",
"tsx": "^4.11.0",
"ws": "^8.18.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function printDependencyGraphAndRemappingsSummary(
const rootRepresentations: string[] = [];

for (const [rootFile, resolvedFile] of roots.entries()) {
if (resolvedFile.type === ResolvedFileType.NPM_PACKGE_FILE) {
if (resolvedFile.type === ResolvedFileType.NPM_PACKAGE_FILE) {
rootRepresentations.push(`- ${rootFile} -> ${resolvedFile.sourceName}
${resolvedFile.fsPath}`);
} else {
Expand Down
Loading
Loading