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

[Investigation] Config overrides should be based on merging the user config, not the resolved #6490

Open
wants to merge 5 commits into
base: v-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ export function resolveNetworkConfigOverride(
);
}

export function resolveNetworkConfig(
networkUserConfigOverride: NetworkUserConfig,
resolveConfigurationVariable: ConfigurationVariableResolver,
): NetworkConfig {
let resolvedConfigOverride: NetworkConfig;

if (networkUserConfigOverride.type === "http") {
resolvedConfigOverride = resolveHttpNetwork(
networkUserConfigOverride,
resolveConfigurationVariable,
);
} else {
resolvedConfigOverride = resolveEdrNetwork(
networkUserConfigOverride,
"",
resolveConfigurationVariable,
);
}

return resolvedConfigOverride;
}

function pickResolvedFromOverrides<
TResolved extends object,
TOverride extends object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default async (): Promise<Partial<HardhatRuntimeEnvironmentHooks>> => ({
created: async (context, hre) => {
let networkManager: NetworkManager | undefined;

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- TMP
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- TMP
const userConfigNetworks = (hre as any).userConfig.networks;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hre already have userConfig.networks but it does not currenlty exist in the type definition (I wonder why?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this configuration is NOT resolved yet

Copy link
Member

@schaable schaable Mar 21, 2025

Choose a reason for hiding this comment

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

wait, you're saying that we're already setting hre.userConfig somewhere in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes exactly, it is already available. Here I'm casting to any just to avoid compilation error during my investigation. I can console.log the userConnfig


hre.network = {
async connect(networkName, chainType, networkConfigOverride) {
const { NetworkManagerImplementation } = await import(
Expand All @@ -20,6 +24,7 @@ export default async (): Promise<Partial<HardhatRuntimeEnvironmentHooks>> => ({
hre.config.networks,
context.hooks,
context.artifacts,
userConfigNetworks,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ArtifactManager } from "../../../types/artifacts.js";
import type {
NetworkConfig,
NetworkConfigOverride,
NetworkUserConfig,
} from "../../../types/config.js";
import type { HookManager } from "../../../types/hooks.js";
import type {
Expand All @@ -21,11 +22,7 @@ import { readBinaryFile } from "@nomicfoundation/hardhat-utils/fs";

import { resolveConfigurationVariable } from "../../core/configuration-variables.js";

import {
mergeConfigOverride,
normalizeNetworkConfigOverride,
} from "./config-override.js";
import { resolveNetworkConfigOverride } from "./config-resolution.js";
import { resolveNetworkConfig } from "./config-resolution.js";
import { EdrProvider } from "./edr/edr-provider.js";
import { isEdrSupportedChainType } from "./edr/utils/chain-type.js";
import { HttpProvider } from "./http-provider.js";
Expand All @@ -43,6 +40,7 @@ export class NetworkManagerImplementation implements NetworkManager {
readonly #networkConfigs: Readonly<Record<string, Readonly<NetworkConfig>>>;
readonly #hookManager: Readonly<HookManager>;
readonly #artifactsManager: Readonly<ArtifactManager>;
readonly #userConfigNetworks: Readonly<Record<string, NetworkUserConfig>>;

#nextConnectionId = 0;

Expand All @@ -52,12 +50,14 @@ export class NetworkManagerImplementation implements NetworkManager {
networkConfigs: Record<string, NetworkConfig>,
hookManager: HookManager,
artifactsManager: ArtifactManager,
userConfigNetworks: Record<string, NetworkUserConfig>,
) {
this.#defaultNetwork = defaultNetwork;
this.#defaultChainType = defaultChainType;
this.#networkConfigs = networkConfigs;
this.#hookManager = hookManager;
this.#artifactsManager = artifactsManager;
this.#userConfigNetworks = userConfigNetworks;
}

public async connect<
Expand Down Expand Up @@ -96,7 +96,7 @@ export class NetworkManagerImplementation implements NetworkManager {
});
}

let resolvedNetworkConfigOverride: Partial<NetworkConfig> | undefined;
let resolvedNetworkConfigOverride: NetworkConfig | undefined;
if (networkConfigOverride !== undefined) {
if (
"type" in networkConfigOverride &&
Expand All @@ -111,17 +111,15 @@ export class NetworkManagerImplementation implements NetworkManager {
);
}

const normalizedNetworkConfigOverride =
await normalizeNetworkConfigOverride(
networkConfigOverride,
this.#networkConfigs[resolvedNetworkName],
);
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/consistent-type-assertions -- tmp
const newConfig: NetworkUserConfig = {
...this.#userConfigNetworks[resolvedNetworkName],
...networkConfigOverride,
} as NetworkUserConfig;

// As normalizeNetworkConfigOverride is not type-safe, we validate the
// normalized network config override immediately after normalizing it.
const validationErrors = await validateNetworkConfigOverride(
normalizedNetworkConfigOverride,
);
const validationErrors = await validateNetworkConfigOverride(newConfig);
if (validationErrors.length > 0) {
throw new HardhatError(
HardhatError.ERRORS.NETWORK.INVALID_CONFIG_OVERRIDE,
Expand All @@ -137,17 +135,16 @@ export class NetworkManagerImplementation implements NetworkManager {
);
}

resolvedNetworkConfigOverride = resolveNetworkConfigOverride(
normalizedNetworkConfigOverride,
resolvedNetworkConfigOverride = resolveNetworkConfig(
newConfig,
(strOrConfigVar) =>
resolveConfigurationVariable(this.#hookManager, strOrConfigVar),
);
}

const resolvedNetworkConfig = mergeConfigOverride(
this.#networkConfigs[resolvedNetworkName],
resolvedNetworkConfigOverride,
);
const resolvedNetworkConfig =
resolvedNetworkConfigOverride ??
this.#networkConfigs[resolvedNetworkName];

/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
-- Cast to ChainTypeT because we know it's valid */
Expand Down
Loading