Skip to content

Commit

Permalink
remove support for module file names in tasks
Browse files Browse the repository at this point in the history
We are removing support for invoking a module just by giving its file
name. Instead the tasks will work in terms of a full or relative path to
the module file.

Users prefer this for autocomplete reasons anyway, but it removes
confusion as to whether it is the `ModuleId` or filename that is in use
here.

Resolves #527.
  • Loading branch information
kanej committed Oct 12, 2023
1 parent 6088ac5 commit 2ea32d5
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 107 deletions.
2 changes: 1 addition & 1 deletion examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a contrived example project, intended mostly to ensure working end-to-en
To run the Ignition deploy against the ephemeral hardhat network:

```shell
npx hardhat ignition deploy CompleteModule.js
npx hardhat ignition deploy ./ignition/modules/CompleteModule.js
```

## Test
Expand Down
4 changes: 2 additions & 2 deletions examples/ens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ This Hardhat project is an example of using ignition to deploy the ENS system of
To run the Ignition deploy against the ephemeral Hardhat network:

```shell
npx hardhat ignition deploy ENS.js
npx hardhat ignition deploy ./ignition/modules/ENS.js
```

To run a deploy of ENS with a test registrar against the local node:

```shell
npx hardhat node
# In another terminal
npx hardhat ignition deploy test-registrar.js --network localhost
npx hardhat ignition deploy ./ignition/modules/test-registrar.js --network localhost
```

## Test
Expand Down
2 changes: 1 addition & 1 deletion examples/sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This project is Hardhat's sample project enhanced with Hardhat Ignition.
To run the Ignition deploy against the ephemeral hardhat network:

```shell
npx hardhat ignition deploy LockModule.js
npx hardhat ignition deploy ./ignition/modules/LockModule.js
```

## Test
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Ignition.
To run the Ignition deploy against the ephemeral Hardhat network:

```shell
npx hardhat ignition deploy LockModule
npx hardhat ignition deploy ./ignition/modules/LockModule
```

## Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("chainId reconciliation", function () {
this.hre.run(
{ scope: "ignition", task: "deploy" },
{
moduleNameOrPath: "./ignition/modules/LockModule.js",
modulePath: "./ignition/modules/LockModule.js",
}
),
/Previous chain id: 123\. Current chain id: 31337/
Expand Down
31 changes: 8 additions & 23 deletions packages/hardhat-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ extendEnvironment((hre) => {

ignitionScope
.task("deploy")
.addPositionalParam(
"moduleNameOrPath",
"The name of the module file within the Ignition modules directory, or a path to the module file"
)
.addPositionalParam("modulePath", "The path to the module file to deploy")
.addOptionalParam(
"parameters",
"A relative path to a JSON file to use for the module parameters,"
Expand All @@ -75,11 +72,11 @@ ignitionScope
.setAction(
async (
{
moduleNameOrPath,
modulePath,
parameters: parametersInput,
id: givenDeploymentId,
}: {
moduleNameOrPath: string;
modulePath: string;
parameters?: string;
id: string;
},
Expand Down Expand Up @@ -117,10 +114,7 @@ ignitionScope

await hre.run("compile", { quiet: true });

const userModule = loadModule(
hre.config.paths.ignition,
moduleNameOrPath
);
const userModule = loadModule(hre.config.paths.ignition, modulePath);

if (userModule === undefined) {
console.warn("No Ignition modules found");
Expand Down Expand Up @@ -174,17 +168,11 @@ ignitionScope
ignitionScope
.task("visualize")
.addFlag("noOpen", "Disables opening report in browser")
.addPositionalParam(
"moduleNameOrPath",
"The name of the module file within the Ignition modules directory, or a path to the module file"
)
.addPositionalParam("modulePath", "The path to the module file to visualize")
.setDescription("Visualize a module as an HTML report")
.setAction(
async (
{
noOpen = false,
moduleNameOrPath,
}: { noOpen: boolean; moduleNameOrPath: string },
{ noOpen = false, modulePath }: { noOpen: boolean; modulePath: string },
hre
) => {
const { IgnitionModuleSerializer, batches } = await import(
Expand All @@ -199,10 +187,7 @@ ignitionScope

await hre.run("compile", { quiet: true });

const userModule = loadModule(
hre.config.paths.ignition,
moduleNameOrPath
);
const userModule = loadModule(hre.config.paths.ignition, modulePath);

if (userModule === undefined) {
console.warn("No Ignition modules found");
Expand Down Expand Up @@ -344,7 +329,7 @@ ignitionScope
ignitionScope
.task("wipe")
.addParam("id", "The id of the deployment that has the future to wipe")
.addParam("future", "The id of the future within the deploment to wipe")
.addParam("future", "The id of the future to wipe")
.setDescription("Reset a deployment's future to allow rerunning")
.setAction(
async (
Expand Down
47 changes: 5 additions & 42 deletions packages/hardhat-plugin/src/load-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MODULES_FOLDER = "modules";

export function loadModule(
ignitionDirectory: string,
moduleNameOrPath: string
modulePath: string
): IgnitionModule | undefined {
const fullModulesDirectoryName = path.resolve(
ignitionDirectory,
Expand Down Expand Up @@ -38,22 +38,19 @@ export function loadModule(

debug(`Loading user modules from '${fullModulesDirectoryName}'`);

const fullpathToModule = resolveFullPathToModule(
fullModulesDirectoryName,
moduleNameOrPath
);
const fullpathToModule = path.resolve(modulePath);

if (fullpathToModule === undefined) {
if (!pathExistsSync(fullpathToModule)) {
throw new HardhatPluginError(
"hardhat-ignition",
`Could not find module ${moduleNameOrPath}`
`Could not find a module file at the path: ${modulePath}`
);
}

if (!isInModuleDirectory(fullModulesDirectoryName, fullpathToModule)) {
throw new HardhatPluginError(
"hardhat-ignition",
`The referenced module ${moduleNameOrPath} is outside the module directory ${shortModulesDirectoryName}`
`The referenced module file ${modulePath} is outside the module directory ${shortModulesDirectoryName}`
);
}

Expand All @@ -64,40 +61,6 @@ export function loadModule(
return module.default ?? module;
}

function resolveFullPathToModule(
modulesDirectory: string,
moduleNameOrPath: string
): string | undefined {
const pathToModule = path.resolve(moduleNameOrPath);
if (pathExistsSync(pathToModule)) {
return pathToModule;
}

const relativeToModules = path.resolve(modulesDirectory, moduleNameOrPath);
if (pathExistsSync(relativeToModules)) {
return relativeToModules;
}

const relativeToModulesWithJsExtension = path.resolve(
modulesDirectory,
`${moduleNameOrPath}.js`
);
if (pathExistsSync(relativeToModulesWithJsExtension)) {
return relativeToModulesWithJsExtension;
}

const relativeToModulesWithTsExtension = path.resolve(
modulesDirectory,
`${moduleNameOrPath}.ts`
);

if (pathExistsSync(relativeToModulesWithTsExtension)) {
return relativeToModulesWithTsExtension;
}

return undefined;
}

function isInModuleDirectory(modulesDirectory: string, modulePath: string) {
const resolvedModulesDirectory = path.resolve(modulesDirectory);
const moduleRelativeToModuleDir = path.relative(
Expand Down
45 changes: 10 additions & 35 deletions packages/hardhat-plugin/test/load-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@ import { useEphemeralIgnitionProject } from "./use-ignition-project";
describe("loadModule", function () {
useEphemeralIgnitionProject("user-modules");

it("should return the module given the module name", () => {
const module = loadModule("ignition", "TestModule");

if (module === undefined) {
assert.fail("Module was not loaded");
}

assert.equal(module.id, "testing123");
});

it("should return the module given the module name and extension", () => {
const module = loadModule("ignition", "TestModule.js");

if (module === undefined) {
assert.fail("Module was not loaded");
}

assert.equal(module.id, "testing123");
});

it("should throw if the module name does not exist", () => {
assert.throws(() => loadModule("ignition", "Fake"));
});

it("should throw if the module name with extension does not exist", () => {
assert.throws(() => loadModule("ignition", "Fake.js"));
it("should throw if given a user module directory that does not exist", async () => {
assert.throws(
() => loadModule("/fake", "AFile.js"),
`Ignition directory /fake not found.`
);
});

it("should throw if the full path to the module does not exist", () => {
assert.throws(() => loadModule("ignition", "./ignition/Fake.js"));
assert.throws(
() => loadModule("ignition", "./ignition/modules/Fake.js"),
"Could not find a module file at the path: ./ignition/modules/Fake.js"
);
});

it("should throw if the full path to the module is outside the module directory", () => {
const unixErrorMessage = `The referenced module ./hardhat.config.js is outside the module directory ignition/modules`;
const unixErrorMessage = `The referenced module file ./hardhat.config.js is outside the module directory ignition/modules`;

const expectedErrorMessage =
process.platform === "win32"
Expand All @@ -53,11 +35,4 @@ describe("loadModule", function () {
expectedErrorMessage
);
});

it("should throw if given a user module directory that does not exist", async () => {
assert.throws(
() => loadModule("/fake", "AFile.js"),
`Ignition directory /fake not found.`
);
});
});
2 changes: 1 addition & 1 deletion packages/hardhat-plugin/test/plan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("visualize", () => {
},
{
noOpen: true,
moduleNameOrPath: "MyModule.js",
modulePath: "./ignition/modules/MyModule.js",
}
);

Expand Down

0 comments on commit 2ea32d5

Please sign in to comment.