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

bug(config): Error: Could not read source code for analysis w/ remappings #2915

Closed
2 tasks done
Tracked by #9157
fedealconada opened this issue Aug 24, 2022 · 16 comments
Closed
2 tasks done
Tracked by #9157
Labels
A-config Area: config A-remappings Area: remappings C-forge Command: forge T-bug Type: bug
Milestone

Comments

@fedealconada
Copy link

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (4e11d1f 2022-08-24T00:12:55.883067Z)

What command(s) is the bug in?

forge coverage

Operating System

macOS (Intel)

Describe the bug

After running forge coverage on this directory I'm getting the following message:

Analysing contracts...
Error:
Could not read source code for analysis

Context:
- Error #0: failed to read from "/src/Divider.sol": No such file or directory (os error 2)
- Error #1: No such file or directory (os error 2)

We are using yarn as dependency manager and we are using yarn workspaces to handle multiple packages on the same repo.

@fedealconada fedealconada added the T-bug Type: bug label Aug 24, 2022
@gakonst gakonst added this to Foundry Aug 24, 2022
@gakonst gakonst moved this to Todo in Foundry Aug 24, 2022
@fedealconada fedealconada changed the title forge coverage: "No such file or directory (os error 2)" Could not read source code for analysis Aug 24, 2022
@onbjerg onbjerg self-assigned this Aug 24, 2022
@onbjerg onbjerg added this to the v1.0.0 milestone Aug 24, 2022
@onbjerg onbjerg added C-forge Command: forge Cmd-forge-coverage Command: forge coverage labels Aug 24, 2022
@onbjerg
Copy link
Collaborator

onbjerg commented Aug 24, 2022

So it seems like it is because of the way the remappings are set up, but I'm still investigating exactly what happens.

Essentially, each of your packages has a remapping for itself. I think this is because all the packages depend on each other somehow, so you need to have all of the remappings defined in case the dependency uses it, correct?

In any case, if you build e.g. the package core, then any source file in that package will end up having an invalid absolute path. You can verify this by just running forge build and inspecting the .ast.absolutePath field in the artifact for e.g. Divider.sol. All other paths are well-formed, which is interesting - if you instead compile e.g. fuse, then all paths are well-formed, except for any file in the fuse package, e.g. PoolManager.sol.

@onbjerg
Copy link
Collaborator

onbjerg commented Aug 24, 2022

Changing up the remappings a bit makes it sort of work, but the report is very cluttered because none of the sub-packages are actually dependencies, it just looks at it as one big source tree.

Changing core/foundry.toml to this:

[profile.default]
root = "."
libs = ['../../node_modules/', '../utils', '../fuse']
tests = "./src/tests"
ffi = true
verbosity = 3
remappings = [
    '@sense-finance/v1-utils=../utils',
    '@sense-finance/v1-core/=../core',
    '@sense-finance/v1-fuse/=../fuse',
    '@rari-capital/solmate=../../node_modules/@rari-capital/solmate',
    '@openzeppelin/contracts=../../node_modules/@openzeppelin/contracts',
    'ds-test/=./src/tests/test-helpers/'
]

Makes it get past the error. I couldn't get the tests to run without failing though so unsure how valid the report is

@fedealconada
Copy link
Author

Thanks so much for investigating the issue!
I tried only changing the core/foundry.toml file as you suggested and got this error:

[⠊] Compiling...
Error:
Failed to resolve file: "/src/libs/Errors.sol": No such file or directory (os error 2).
    --> "/Users/fedealconada/Development/sense-v1/pkg/core/src/Divider.sol"
        "@sense-finance/v1-utils/src/libs/Errors.sol"
    Check configured remappings.

I have then "fixed" the other .toml files (following the same logic) and the error is gone but I got a new one:

forge coverage

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[7]    43932 abort      forge coverage

@onbjerg
Copy link
Collaborator

onbjerg commented Aug 25, 2022

Can you post the tomls? Some parts of the toml are very important here since a misconfiguration leads to recursion in your repository

@fedealconada
Copy link
Author

fedealconada commented Aug 27, 2022

Here they are!

core/foundry.toml

[profile.default]
root = "."
libs = ['../../node_modules/', '../utils', '../fuse']
tests = "./src/tests"
ffi = true
verbosity = 3
remappings = [
    '@sense-finance/v1-utils=../utils',
    '@sense-finance/v1-core/=./',
    '@sense-finance/v1-fuse/=../fuse',
    '@rari-capital/solmate=../../node_modules/@rari-capital/solmate',
    '@openzeppelin/contracts=../../node_modules/@openzeppelin/contracts',
    'ds-test/=./src/tests/test-helpers/'
]

fuse/foundry.toml

[profile.default]
root = "."
libs = ['../../node_modules/', '../utils', '../core']
tests = "./src/tests"
ffi = true
verbosity = 3
# Fork block mined on Apr 18 2022 at 12:00:10 AM UTC
fork_block_number = 14605885
remappings = [
    '@sense-finance/v1-utils=../utils',
    '@sense-finance/v1-core=../core',
    '@sense-finance/v1-fuse=./',
    '@rari-capital/solmate=../../node_modules/@rari-capital/solmate',
    '@openzeppelin/contracts=../../node_modules/@openzeppelin/contracts'
]

utils/foundry.toml

[profile.default]
root = "."
libs = ['../../node_modules/', '../fuse', '../core']
tests = "./src/tests"
ffi = true
verbosity = 3
remappings = [
    '@sense-finance/v1-utils=./',
    '@sense-finance/v1-core=../core',
    '@sense-finance/v1-fuse=../fuse',
    '@rari-capital/solmate=../../node_modules/@rari-capital/solmate',
    '@openzeppelin/contracts=../../node_modules/@openzeppelin/contracts'
]

lnist added a commit to mangrovedao/mangrove.js that referenced this issue Aug 29, 2022
Note: we currently run it from the root; otherwise, the coverage
reporting cannot find our contracts. This may be related to
foundry-rs/foundry#2915 .

Also, we add flags to coveralls in an attempt to separate the reports.
lnist added a commit to mangrovedao/mangrove.js that referenced this issue Aug 29, 2022
Note: we currently run it from the root; otherwise, the coverage
reporting cannot find our contracts. This may be related to
foundry-rs/foundry#2915 .

Also, we add flags to coveralls in an attempt to separate the reports.
lnist added a commit to mangrovedao/mangrove.js that referenced this issue Sep 1, 2022
…, improve dependabot triggers (#669)

* fix(ci): Dependabot workflow should only trigger for select branches
* feat(ci): Verify mgv cli can start as part of checks
* fix(ci): Make coveralls use actual HEAD commit
    * The checkout of the push branch may not actually check out the GITHUB_SHA that triggered the workflow, a newer one may be checked out instead. We retrieve the right one by using git.
* feat(ci): Add solidity code coverage to push trigger
    * Note: we currently run it from the root; otherwise, the coverage reporting cannot find our contracts. This may be related to
foundry-rs/foundry#2915 . Also, we add flags to coveralls in an attempt to separate the reports.
@mattsse
Copy link
Member

mattsse commented Sep 8, 2022

@fedealconada sorry for taking so long,

I had a look, and one thing that's obvious is that this setup uses circular dependencies, (core imports from fuse, fuse imports from core)

I checked the solc compilerinput which looks ok, there's something weird with the output however because it contains duplicate entries for Divider.sol: src/Divider.sol and /src/Divider.sol, this is usually a sign that solc fails to look up an import of a file in the json compilerinput (which works like a virtual file system) and instead tries to find it on disk and also adds this to the compileroutput.

I wasn't able to find the root cause for this but it's definitely related to the relative imports, perhaps in combination with circular setup there are some weird edge cases...

however I was able to resolve this by tuning the remappings and point to the src dir instead, this is also the default dapptools style

    '@sense-finance/v1-utils=../utils/src',
    '@sense-finance/v1-core/=./src',
    '@sense-finance/v1-fuse=../fuse/src',

after I replaced the imports in the sol files, the issue was resolved

@fedealconada
Copy link
Author

fedealconada commented Sep 12, 2022

Hey @mattsse , thanks a ton for the follow up! I've just tried what you suggested (changing the re-mappings and replacing imports on the sol files) but I'm getting the same error as before:

forge coverage

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[68]    42352 abort      forge coverage

Just to double check, I pushed the changes on this branch. This is the commit with the .toml files changes and this one the one where I'm replacing the .sol files.

@fedealconada
Copy link
Author

Any follow up on this @mattsse ? :)

@davidlaprade
Copy link

davidlaprade commented Sep 28, 2022

Thanks for reporting this @fedealconada. Another data-point here.

I'm working on a project that was seeing the same "could not read source code" error when I ran forge coverage. In our case, there weren't circular dependencies there were circular dependencies, but they didn't have to be resolved in order to get the coverage tool working. The real issue seemed to be a nested dependency tree. The project had one submodule (call it A), and A itself had a submodule (call it B). The project imported from both A and B, and thus had remappings to shorten imports from B.

I eventually got forge coverage to work on a frankenstein branch by moving the two submodules directly into src and deleting the corresponding remappings. But it'd obviously be great if this didn't have to be done.

EDIT: see above

@fedealconada
Copy link
Author

@mattsse we finally got this fixed. Not sure exactly why, but seems like our remappings in the foundry.toml needed the /src (and I removed them from the contracts). The other way around was throwing an error.

@xenide
Copy link

xenide commented Jan 4, 2023

Another data point here. In our case we don't have any circular dependencies, the repo imports dependencies from lib/v3-core. forge test works with no issues and it's only coverage that's complaining.

foundry.toml in the root directory:

[profile.default]
solc           = "0.8.17"
bytecode_hash  = "none"
optimizer_runs = 1_000_000
remappings     = ["@openzeppelin/=lib/openzeppelin-contracts/contracts/", "v3-core/=lib/v3-core"]
match_path     = "test/unit/*.sol"
verbosity      = 3

Also, doing export RUST_LOG=trace doesn't give much more helpful info.

@Evalir Evalir moved this from Todo to Shelved in Foundry Jul 13, 2023
0xca11 added a commit to RollaProject/kernel that referenced this issue Sep 20, 2023
when Kernel is used as a dependency on a project, that project's forge coverage might fail to run because of a dependency resolution issue when relative import paths are used

Relevant Foundry issue: foundry-rs/foundry#2915
0xca11 added a commit to RollaProject/kernel that referenced this issue Sep 20, 2023
when Kernel is used as a dependency on a project, that project's forge coverage might fail to run because of a dependency resolution issue when relative import paths are used

Relevant Foundry issue: foundry-rs/foundry#2915
leekt pushed a commit to zerodevapp/kernel that referenced this issue Oct 3, 2023
when Kernel is used as a dependency on a project, that project's forge coverage might fail to run because of a dependency resolution issue when relative import paths are used

Relevant Foundry issue: foundry-rs/foundry#2915
apbendi added a commit to ScopeLift/flexible-voting that referenced this issue Oct 26, 2023
When importing Flexible Voting into another repository, the forge
coverage tool was failing with the error: "Could not read source
code for analysis" linked to the imports of interface files from
the FlexibleVotingClient contract. While the non-relative imports
work fine in the context of this repo, and even in the context of
running tests normally when used as an import, they cause an issue
for the coverage tool.

It's unclear if this is a bug with foundry or something with our
configuration. Regardless, the easiest fix is simply to use relative
imports, which we do here.

Read more about the issue here:
foundry-rs/foundry#2915
apbendi added a commit to ScopeLift/flexible-voting that referenced this issue Oct 26, 2023
When importing Flexible Voting into another repository, the forge
coverage tool was failing with the error: "Could not read source
code for analysis" linked to the imports of interface files from
the FlexibleVotingClient contract. While the non-relative imports
work fine in the context of this repo, and even in the context of
running tests normally when used as an import, they cause an issue
for the coverage tool.

It's unclear if this is a bug with foundry or something with our
configuration. Regardless, the easiest fix is simply to use relative
imports, which we do here.

Read more about the issue here:
foundry-rs/foundry#2915
MaxMoskalenko pushed a commit to MaxMoskalenko/kernel that referenced this issue Mar 11, 2024
when Kernel is used as a dependency on a project, that project's forge coverage might fail to run because of a dependency resolution issue when relative import paths are used

Relevant Foundry issue: foundry-rs/foundry#2915
@fedealconada
Copy link
Author

It's been a while but I'm facing again this issue, any hint on this @mattsse?

@xenide
Copy link

xenide commented Jun 6, 2024

It's been a while but I'm facing again this issue, any hint on this @mattsse?

me too. I'm positive that we don't have circular dependencies, just submodules which we import from

@drinkius
Copy link

A hint for someone who might be researching similar issue: in my case the issue was indeed in remappings but related to OS case-sensitivity, all worked well on macOS but failed in CI on Ubunutu. Re-check the cases of your remappings, this might help

@zerosnacks zerosnacks changed the title Could not read source code for analysis bug(remappings): Error: Could not read source code for analysis Jul 31, 2024
@zerosnacks zerosnacks changed the title bug(remappings): Error: Could not read source code for analysis bug(config): Error: Could not read source code for analysis w/ remappings Jul 31, 2024
@zerosnacks zerosnacks added A-config Area: config and removed Cmd-forge-coverage Command: forge coverage labels Jul 31, 2024
@jenpaff jenpaff moved this from Shelved to Todo in Foundry Sep 30, 2024
@grandizzy grandizzy added the A-remappings Area: remappings label Oct 31, 2024
@grandizzy
Copy link
Collaborator

forge test works with no issues and it's only coverage that's complaining

The coverage issue is related to optimizer being disabled, see #8840 (comment)
I am not able to reproduce the initial report related to remappings / build failures, @zerosnacks could you also pls give it a try? thanks!

@grandizzy
Copy link
Collaborator

closing as it's no longer reproducible with latest version

image

please reopen if still an issue. thank you!

@grandizzy grandizzy closed this as not planned Won't fix, can't repro, duplicate, stale Nov 19, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-config Area: config A-remappings Area: remappings C-forge Command: forge T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

8 participants