foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.24k stars 1.73k forks source link

feat(`cheatcodes`): read length of arbitrary array #8467

Open ivanzhelyazkov opened 3 months ago

ivanzhelyazkov commented 3 months ago

Component

Forge

Have you ensured that all of these are up to date?

What version of Foundry are you on?

forge 0.2.0 (af97b2c 2024-07-18T00:22:15.301937000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

sometime between the latest nightly version (18-07-2024) and this commit: https://github.com/foundry-rs/foundry/commit/7bef9caccfe62761225be66e84bea2810e656c96 (abt 3 weeks ago) a change was pushed which breaks the current json parsing: string memory path = "./test.json"; string memory json = vm.readFile(path); testCase = parseTestCase(json, "testCase"); string[] memory testCaseString = vm.parseJsonStringArray(json, initialParseString);

now fails with: [FAIL. Reason: expected string, found JSON object;]

note above code previously worked for a long time (maybe longer than 10 months)

To reproduce, please find attached test which breaks in this repo (along with 22 more after upgrading to latest foundry version): https://github.com/bancorprotocol/carbon-contracts/blob/317e0e9f785b62b1e73a017c9fe6c38bf2e06d27/test/forge/VortexTestCaseParser.t.sol#L51

mattsse commented 3 months ago

@klkvr likely related to the recent parser features?

klkvr commented 3 months ago

So this code relies on parseJsonStringArray successfully parsing arrays of objects. However, the output in this case doesn't make much sense because we are receiving objects without quotes which is basically broken JSON: vm.parseJsonStringArray('[{"a": 1}, {"b": 2}]', "$"); would return ["{a:1}", "{b:2}"]

In linked codebase, this cheatcodes is only used for getting length of a given array by doing something like parseJsonStringArray(...).length. The contents of returned array are never used

I'd prefer to not allow such parsing but it seems that there's basically no other way to get a length of arbitrary array. I think we should either add a cheatcode for this or migrate to smarter jsonpath lib which allows doing e.g. $.array.length().

wdyt @mattsse

mattsse commented 3 months ago

I think we should either add a cheatcode for this

this seems reasonable, although not ideal