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

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

Open
beeb opened this issue Feb 27, 2024 · 4 comments

Comments

@beeb
Copy link

beeb commented Feb 27, 2024

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
Copy link
Owner

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

@beeb
Copy link
Author

beeb commented Feb 27, 2024

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
Copy link
Author

beeb commented Feb 27, 2024

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
Copy link

Same issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants