Skip to content

Commit f18b1ea

Browse files
authored
Merge pull request #7126 from NomicFoundation/fix-default-package-exports
Make forge-std work with and without src/
2 parents ef59312 + eea70ff commit f18b1ea

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

.changeset/sharp-goats-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Make `forge-std` work with and without `src/`

v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/resolver/dependency-resolver.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ export class ResolverImplementation implements Resolver {
714714
const dependencyWithPackageExports = {
715715
...dependency,
716716
exports: dependency.exports ?? {
717+
// If someone imports with the full path, we want it to work
718+
"./src/*.sol": "./src/*.sol",
719+
// If they import assuming the typical remapping into src/, we also
720+
// want it to work
717721
"./*.sol": "./src/*.sol",
718722
},
719723
};

v-next/hardhat/test/internal/builtin-plugins/solidity/build-system/resolver/dependency-resolver.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,58 @@ submodule2/=lib/submodule2/src/`,
25722572
);
25732573
});
25742574

2575+
it("Should simulate package.exports for forge-std, but still allow importing with src/", async () => {
2576+
await using project =
2577+
await useTestProjectTemplate(templateWithForgeStd);
2578+
const resolver = await ResolverImplementation.create(
2579+
project.path,
2580+
readUtf8File,
2581+
);
2582+
const absoluteFilePath = path.join(project.path, "contracts/A.sol");
2583+
const result = await resolver.resolveProjectFile(absoluteFilePath);
2584+
assert.ok(result.success, "Result should be successful");
2585+
2586+
assert.deepEqual(
2587+
await resolver.resolveImport(
2588+
result.value,
2589+
"forge-std/src/Test.sol",
2590+
),
2591+
{
2592+
success: true,
2593+
value: {
2594+
file: {
2595+
type: ResolvedFileType.NPM_PACKAGE_FILE,
2596+
fsPath: path.join(
2597+
project.path,
2598+
"node_modules/forge-std/src/Test.sol",
2599+
),
2600+
content: {
2601+
text: `Test`,
2602+
importPaths: [],
2603+
versionPragmas: [],
2604+
},
2605+
inputSourceName: "npm/[email protected]/src/Test.sol",
2606+
package: {
2607+
name: "forge-std",
2608+
version: "1.2.3",
2609+
rootFsPath: path.join(
2610+
project.path,
2611+
"node_modules/forge-std",
2612+
),
2613+
inputSourceNameRoot: "npm/[email protected]",
2614+
exports: undefined,
2615+
},
2616+
},
2617+
remapping: {
2618+
context: "project/",
2619+
prefix: "forge-std/",
2620+
target: "npm/[email protected]/",
2621+
},
2622+
},
2623+
},
2624+
);
2625+
});
2626+
25752627
it("Should return the right errors as if it had a package.exports", async () => {
25762628
await using project =
25772629
await useTestProjectTemplate(templateWithForgeStd);

0 commit comments

Comments
 (0)