Skip to content

Commit fe9d726

Browse files
authored
fix(cli): fixed module artifactPath imports (latticexyz#2832)
1 parent 6f85872 commit fe9d726

File tree

10 files changed

+112
-335
lines changed

10 files changed

+112
-335
lines changed

.changeset/smart-moose-run.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/cli": patch
3+
---
4+
5+
Fixed imports of module artifacts via `artifactPath` and removed unused `@latticexyz/world-modules` dependency.

e2e/packages/contracts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@latticexyz/schema-type": "link:../../../packages/schema-type",
1717
"@latticexyz/store": "link:../../../packages/store",
1818
"@latticexyz/world": "link:../../../packages/world",
19+
"@latticexyz/world-modules": "link:../../../packages/world-modules",
1920
"dotenv": "^16.0.3",
2021
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
2122
"forge-std": "https://github.com/foundry-rs/forge-std.git#74cfb77e308dd188d2f58864aaf44963ae6b88b1",

e2e/pnpm-lock.yaml

+10-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"@latticexyz/store": "workspace:*",
4343
"@latticexyz/utils": "workspace:*",
4444
"@latticexyz/world": "workspace:*",
45-
"@latticexyz/world-modules": "workspace:*",
4645
"abitype": "1.0.0",
4746
"asn1.js": "^5.4.1",
4847
"chalk": "^5.0.1",
@@ -51,6 +50,7 @@
5150
"dotenv": "^16.0.3",
5251
"ethers": "^5.7.2",
5352
"execa": "^7.0.0",
53+
"find-up": "^6.3.0",
5454
"glob": "^8.0.3",
5555
"openurl": "^1.1.1",
5656
"p-queue": "^7.4.1",

packages/cli/src/deploy/common.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Abi, Address, Hex, padHex } from "viem";
22
import storeConfig from "@latticexyz/store/mud.config";
33
import worldConfig from "@latticexyz/world/mud.config";
44
import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type: "json" };
5-
import IModuleAbi from "@latticexyz/world-modules/out/IModule.sol/IModule.abi.json" assert { type: "json" };
65
import { Tables, configToTables } from "./configToTables";
76
import { helloStoreEvent } from "@latticexyz/store";
87
import { StoreConfig } from "@latticexyz/store/internal";
@@ -22,7 +21,7 @@ export const worldTables = configToTables(worldToV1(worldConfig));
2221

2322
export const worldDeployEvents = [helloStoreEvent, helloWorldEvent] as const;
2423

25-
export const worldAbi = [...IBaseWorldAbi, ...IModuleAbi] as const;
24+
export const worldAbi = IBaseWorldAbi;
2625

2726
// Ideally, this should be an append-only list. Before adding more versions here, be sure to add backwards-compatible support for old Store/World versions.
2827
export const supportedStoreVersions = ["2.0.0", "2.0.1", "2.0.2"];

packages/cli/src/deploy/configToModules.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { knownModuleArtifacts } from "../utils/knownModuleArtifacts";
1111

1212
export async function configToModules<config extends World>(
1313
config: config,
14+
// TODO: remove/replace `forgeOutDir`
1415
forgeOutDir: string,
1516
): Promise<readonly Module[]> {
1617
// this expects a namespaced table name when used with `resolveTableId`

packages/cli/src/deploy/ensureModules.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export async function ensureModules({
3838
pRetry(
3939
async () => {
4040
try {
41+
// append module's ABI so that we can decode any custom errors
4142
const abi = [...worldAbi, ...mod.abi];
4243
const moduleAddress = mod.prepareDeploy(deployerAddress, libraries).address;
4344
return mod.installAsRoot

packages/cli/src/utils/getContractArtifact.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ import { LibraryPlaceholder } from "../deploy/common";
33
import { findPlaceholders } from "./findPlaceholders";
44
import { z } from "zod";
55
import { Abi as abiSchema } from "abitype/zod";
6+
import { createRequire } from "node:module";
7+
import { findUp } from "find-up";
68

79
export type GetContractArtifactOptions = {
10+
/**
11+
* Path to `package.json` where `artifactPath`s are resolved relative to.
12+
*
13+
* Defaults to nearest `package.json` relative to `process.cwd()`.
14+
*/
15+
packageJsonPath?: string;
816
/**
917
* Import path to contract's forge/solc JSON artifact with the contract's compiled bytecode.
1018
*
11-
* This path is resolved using node's contract resolution, so this supports both relative file paths (`../path/to/MyModule.json`) as well as JS import paths
12-
* (`@latticexyz/world-contracts/out/CallWithSignatureModule.sol/CallWithSignatureModule.json`).
19+
* This path is resolved using node's module resolution relative to `configPath`, so this supports both
20+
* relative file paths (`../path/to/MyModule.json`) as well as JS import paths (`@latticexyz/world-contracts/out/CallWithSignatureModule.sol/CallWithSignatureModule.json`).
1321
*/
1422
artifactPath: string;
1523
};
@@ -42,17 +50,16 @@ const artifactSchema = z.object({
4250
});
4351

4452
export async function getContractArtifact({
53+
packageJsonPath,
4554
artifactPath,
4655
}: GetContractArtifactOptions): Promise<GetContractArtifactResult> {
4756
let importedArtifact;
4857
try {
49-
importedArtifact = (
50-
await import(artifactPath, {
51-
with: { type: "json" },
52-
// `with` is the new approach, but `assert` is kept for backwards-compatibility with Node 18
53-
assert: { type: "json" },
54-
})
55-
).default;
58+
const requirePath = packageJsonPath ?? (await findUp("package.json", { cwd: process.cwd() }));
59+
if (!requirePath) throw new Error("Could not find package.json to import relative to.");
60+
61+
const require = createRequire(requirePath);
62+
importedArtifact = require(artifactPath);
5663
} catch (error) {
5764
console.error();
5865
console.error("Could not import contract artifact at", artifactPath);

0 commit comments

Comments
 (0)