Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit b169455

Browse files
authored
Merge pull request #5819 from dPreininger/develop
Fix: `truffle console` crash when metadata is missing in JSON artifact
2 parents bf91e46 + 2fa56c5 commit b169455

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

packages/core/lib/console.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,27 @@ class Console extends EventEmitter {
263263
"utf8"
264264
);
265265
const json = JSON.parse(body);
266-
const metadata = JSON.parse(json.metadata);
267-
const sources = Object.keys(metadata.sources);
268-
// filter out Truffle's console.log. We don't want users to interact with in the REPL.
269-
// user contracts named console.log will be imported, and a warning will be issued.
270-
if (
271-
sources.length > 1 ||
272-
(sources.length === 1 &&
273-
!sources.some(source => {
274-
return (
275-
source === "truffle/console.sol" ||
276-
source === "truffle/Console.sol"
277-
);
278-
}))
279-
) {
266+
// Artifacts may not contain metadata. For example, early Solidity versions as well as
267+
// Vyper contracts do not include metadata. Just push them to json blobs.
268+
if (json.metadata === undefined) {
280269
jsonBlobs.push(json);
270+
} else {
271+
// filter out Truffle's console.log. We don't want users to interact with in the REPL.
272+
// user contracts named console.log will be imported, and a warning will be issued.
273+
const metadata = JSON.parse(json.metadata);
274+
const sources = Object.keys(metadata.sources);
275+
if (
276+
sources.length > 1 ||
277+
(sources.length === 1 &&
278+
!sources.some(source => {
279+
return (
280+
source === "truffle/console.sol" ||
281+
source === "truffle/Console.sol"
282+
);
283+
}))
284+
) {
285+
jsonBlobs.push(json);
286+
}
281287
}
282288
} catch (error) {
283289
throw new Error(`Error parsing or reading ${file}: ${error.message}`);

0 commit comments

Comments
 (0)