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

Model abigen!s Source::Path behaviour after include_str! #97

Open
mitchmindtree opened this issue Feb 11, 2022 · 1 comment
Open

Model abigen!s Source::Path behaviour after include_str! #97

mitchmindtree opened this issue Feb 11, 2022 · 1 comment
Labels

Comments

@mitchmindtree
Copy link
Contributor

Follow-up to #94.

Currently the path passed to the right hand side argument of abigen! is expected to be relative to the std::env::current_dir:

let root = env::current_dir()?.canonicalize()?;

This evaluates to whatever happens to be the current directory at the time of macro expansion. This means that the necessary path given to abigen! can change when the project is moved to a different subdirectory of the same workspace, or if its moved from a workspace into its own project, or vice versa. Ideally, we want our src not to be dependent on the directory that the project happens to be executed within.

To fix this, we could model our behaviour after the std include_str! macro. include_str! accepts a path that is relative to the file in which it is invoked. For example if we have:

project/my-contract-abi.json
project/tests/harness.rs

the abigen! macro could then be invoked within the project/tests/harness.rs with

abigen!(MyContract, "../my-contract-abi.json");
@JoshuaBatty
Copy link
Member

JoshuaBatty commented Feb 14, 2022

So I took a look into this. Originally I thought we could use the file!() macro but then we are back to square one with not being able to have a macro evaluate as an input to another macro. Further, the path returned by file!() is where the macro code lives not the file where the macro was called from.

I then started to look at using span to get this information out. Turns out you can use a span to get access to the calling file path, but currently it's an unstable feature. See this open issue

It provides a method on span pub fn source_file(&self) -> SourceFile; This would allow us to get relative paths working, but the issue has been open for over 3 years, so there is a chance it might not land. Still, might be worth periodically checking in to see if there is any movement towards stabilizing this feature.

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

No branches or pull requests

2 participants