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

Fix: truffle console crash when metadata is missing in JSON artifact #5819

Merged
merged 3 commits into from
Jan 4, 2023
Merged
Changes from all 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
34 changes: 20 additions & 14 deletions packages/core/lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,27 @@ class Console extends EventEmitter {
"utf8"
);
const json = JSON.parse(body);
const metadata = JSON.parse(json.metadata);
const sources = Object.keys(metadata.sources);
// filter out Truffle's console.log. We don't want users to interact with in the REPL.
// user contracts named console.log will be imported, and a warning will be issued.
if (
sources.length > 1 ||
(sources.length === 1 &&
!sources.some(source => {
return (
source === "truffle/console.sol" ||
source === "truffle/Console.sol"
);
}))
) {
// Artifacts may not contain metadata. For example, early Solidity versions as well as
// Vyper contracts do not include metadata. Just push them to json blobs.
if (json.metadata === undefined) {
jsonBlobs.push(json);
} else {
// filter out Truffle's console.log. We don't want users to interact with in the REPL.
// user contracts named console.log will be imported, and a warning will be issued.
const metadata = JSON.parse(json.metadata);
const sources = Object.keys(metadata.sources);
if (
sources.length > 1 ||
(sources.length === 1 &&
!sources.some(source => {
return (
source === "truffle/console.sol" ||
source === "truffle/Console.sol"
);
}))
) {
jsonBlobs.push(json);
}
}
} catch (error) {
throw new Error(`Error parsing or reading ${file}: ${error.message}`);
Expand Down