juanfranblanco / vscode-solidity

Visual Studio Code language support extension for Solidity smart contracts in Ethereum https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity
MIT License
892 stars 191 forks source link

Go to definition not working when using "relative to project root" import paths. #450

Open beeb opened 7 months ago

beeb commented 7 months ago

Consider the following foundry project structure:

.
└── myproject/
    ├── src/
    │   ├── FileA.sol
    │   └── FileB.sol
    └── test/
        └── Test.t.sol

In FileA.sol, I use import paths relative to the root of the project:

import { MyItem } from "src/FileB.sol";
import { Something } from "test/Test.t.sol";

When I use "go to definition" on those imported items, the file that gets opened is an absolute path: /src/FileB.sol or /test/Test.t.sol which obviously do not exist at the root of my filesystem.

juanfranblanco commented 7 months ago

You need to prefix the imports with ./ for example ./src so they can be differentiated from libraries dependencies

beeb commented 7 months ago

You need to prefix the imports with ./ for example ./src so they can be differentiated from libraries dependencies

Thanks for the quick reply!

Unfortunately what you suggest does not work, as ./ means a relative import, relative to the current file path. Here the path is relative to the project root.

Foundry error:

Compiler run failed:
Error (6275): Source "src/FileB.sol" not found: File not found. Searched the following locations: "/home/user/project".

Foundry has no problem finding the files when imported as shown in my post above. Likewise, the nomic foundation extension can find the files just fine when using that pattern. I like this pattern because it means I can copy-paste the imports into any of my project files and they will work without change.

Is there any way you could support such import paths?

To me it looks like the extension should simply not add the leading / in front of the path, and everything would work fine.

beeb commented 7 months ago

Note that hover cards for the items imported like in my post above work fine, so the LSP can find those items indeed. Only the file that gets opened on "go to definition" has the wrong path, because a leading slash is added, turning them into absolute paths.

ZhangZhuoSJTU commented 7 months ago

Same issue here.