Skip to content

Commit 08c6cd4

Browse files
authored
Support ignoring Hardhat compile errors when extracting detailed namespaced storage layouts for validations (#1090)
1 parent 52ae59e commit 08c6cd4

File tree

7 files changed

+57
-19
lines changed

7 files changed

+57
-19
lines changed

packages/core/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Changelog
22

3-
## Unreleased
3+
## 1.40.0 (2024-10-10)
44

55
- Fix Hardhat compile error when overriding interface functions with public constant variables. ([#1091](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/1091))
6+
- Support ignoring Hardhat compile errors when extracting detailed namespaced storage layouts for validations. ([#1090](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/1090))
67

78
## 1.39.0 (2024-10-02)
89

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openzeppelin/upgrades-core",
3-
"version": "1.39.0",
3+
"version": "1.40.0",
44
"description": "",
55
"repository": "https://github.com/OpenZeppelin/openzeppelin-upgrades/tree/master/packages/core",
66
"license": "MIT",

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ export { getUpgradeInterfaceVersion } from './upgrade-interface-version';
6363
export { makeNamespacedInput } from './utils/make-namespaced';
6464
export { isNamespaceSupported } from './storage/namespace';
6565
export { inferProxyAdmin } from './infer-proxy-admin';
66+
export { assertUnreachable } from './utils/assert';

packages/plugin-hardhat/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.5.0 (2024-10-10)
4+
5+
- Support ignoring Hardhat compile errors when extracting detailed namespaced storage layouts for validations. ([#1090](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/1090))
6+
37
## 3.4.0 (2024-09-23)
48

59
### Potentially breaking changes

packages/plugin-hardhat/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openzeppelin/hardhat-upgrades",
3-
"version": "3.4.0",
3+
"version": "3.5.0",
44
"description": "",
55
"repository": "https://github.com/OpenZeppelin/openzeppelin-upgrades/tree/master/packages/plugin-hardhat",
66
"license": "MIT",
@@ -38,7 +38,7 @@
3838
"@openzeppelin/defender-sdk-base-client": "^1.14.4",
3939
"@openzeppelin/defender-sdk-deploy-client": "^1.14.4",
4040
"@openzeppelin/defender-sdk-network-client": "^1.14.4",
41-
"@openzeppelin/upgrades-core": "^1.38.0",
41+
"@openzeppelin/upgrades-core": "^1.40.0",
4242
"chalk": "^4.1.0",
4343
"debug": "^4.1.1",
4444
"ethereumjs-util": "^7.1.5",

packages/plugin-hardhat/src/index.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { subtask, extendEnvironment, extendConfig } from 'hardhat/config';
66
import { TASK_COMPILE_SOLIDITY, TASK_COMPILE_SOLIDITY_COMPILE } from 'hardhat/builtin-tasks/task-names';
77
import { lazyObject } from 'hardhat/plugins';
88
import { HardhatConfig, HardhatRuntimeEnvironment } from 'hardhat/types';
9-
import type { silenceWarnings, SolcInput, SolcOutput } from '@openzeppelin/upgrades-core';
9+
import { assertUnreachable, type silenceWarnings, type SolcInput, type SolcOutput } from '@openzeppelin/upgrades-core';
1010
import type { DeployFunction } from './deploy-proxy';
1111
import type { PrepareUpgradeFunction } from './prepare-upgrade';
1212
import type { UpgradeFunction } from './upgrade-proxy';
@@ -101,7 +101,46 @@ subtask(TASK_COMPILE_SOLIDITY_COMPILE, async (args: RunCompilerArgs, hre, runSup
101101
if (isNamespaceSupported(args.solcVersion)) {
102102
const namespacedInput = makeNamespacedInput(args.input, output, args.solcVersion);
103103
namespacedOutput = (await runSuper({ ...args, quiet: true, input: namespacedInput })).output;
104-
await checkNamespacedCompileErrors(namespacedOutput);
104+
105+
const namespacedCompileErrors = getNamespacedCompileErrors(namespacedOutput);
106+
if (namespacedCompileErrors.length > 0) {
107+
// If there are compile errors in the namespaced output, show error or warning if needed, then only use the original output
108+
109+
const msg = `Failed to compile modified contracts for namespaced storage layout validations:\n\n${namespacedCompileErrors.join('\n')}`;
110+
const preamble = [
111+
'Please report this at https://zpl.in/upgrades/report. If possible, include the source code for the contracts mentioned in the errors above.',
112+
'This step allows for advanced storage modifications such as tight varible packing when performing upgrades with namespaced storage layouts.',
113+
];
114+
115+
switch (hre.config.namespacedCompileErrors) {
116+
case undefined:
117+
case 'error': {
118+
const { UpgradesError } = await import('@openzeppelin/upgrades-core');
119+
const details = [
120+
...preamble,
121+
'If you are not using namespaced storage, or if you do not anticipate making advanced modifications to namespaces during upgrades,',
122+
"you can set namespacedCompileErrors: 'warn' or namespacedCompileErrors: 'ignore' in your hardhat config to convert this to a warning or to ignore this.",
123+
];
124+
throw new UpgradesError(msg, () => details.join('\n'));
125+
}
126+
case 'warn': {
127+
const { logWarning } = await import('@openzeppelin/upgrades-core');
128+
const details = [
129+
...preamble,
130+
'If you are not using namespaced storage, or if you do not anticipate making advanced modifications to namespaces during upgrades,',
131+
"you can set namespacedCompileErrors: 'ignore' in your hardhat config to ignore this.",
132+
];
133+
logWarning(msg, details);
134+
break;
135+
}
136+
case 'ignore':
137+
break;
138+
default:
139+
assertUnreachable(hre.config.namespacedCompileErrors);
140+
}
141+
142+
namespacedOutput = undefined;
143+
}
105144
}
106145

107146
const validations = validate(output, decodeSrc, args.solcVersion, args.input, namespacedOutput);
@@ -111,11 +150,7 @@ subtask(TASK_COMPILE_SOLIDITY_COMPILE, async (args: RunCompilerArgs, hre, runSup
111150
return { output, solcBuild };
112151
});
113152

114-
/**
115-
* Checks for compile errors in the modified contracts for namespaced storage.
116-
* If errors are found, throws an error with the compile error messages.
117-
*/
118-
async function checkNamespacedCompileErrors(namespacedOutput: SolcOutput) {
153+
function getNamespacedCompileErrors(namespacedOutput: SolcOutput) {
119154
const errors = [];
120155
if (namespacedOutput.errors !== undefined) {
121156
for (const error of namespacedOutput.errors) {
@@ -124,14 +159,7 @@ async function checkNamespacedCompileErrors(namespacedOutput: SolcOutput) {
124159
}
125160
}
126161
}
127-
if (errors.length > 0) {
128-
const { UpgradesError } = await import('@openzeppelin/upgrades-core');
129-
throw new UpgradesError(
130-
`Failed to compile modified contracts for namespaced storage:\n\n${errors.join('\n')}`,
131-
() =>
132-
'Please report this at https://zpl.in/upgrades/report. If possible, include the source code for the contracts mentioned in the errors above.',
133-
);
134-
}
162+
return errors;
135163
}
136164

137165
extendEnvironment(hre => {

packages/plugin-hardhat/src/type-extensions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ export interface HardhatDefenderConfig {
1717
network?: string;
1818
}
1919

20+
export type NamespacedCompileErrorsRule = 'error' | 'warn' | 'ignore';
21+
2022
declare module 'hardhat/types/config' {
2123
export interface HardhatUserConfig {
2224
defender?: HardhatDefenderConfig;
25+
namespacedCompileErrors?: NamespacedCompileErrorsRule;
2326
}
2427

2528
export interface HardhatConfig {
2629
defender?: HardhatDefenderConfig;
30+
namespacedCompileErrors?: NamespacedCompileErrorsRule;
2731
}
2832
}

0 commit comments

Comments
 (0)