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

custom attribute panicked when using it in a member of a workspace #2

Closed
silver-ymz opened this issue May 20, 2023 · 7 comments · Fixed by #3
Closed

custom attribute panicked when using it in a member of a workspace #2

silver-ymz opened this issue May 20, 2023 · 7 comments · Fixed by #3
Assignees

Comments

@silver-ymz
Copy link

When using hs_bindgen macro in a member of a workspace, it will raise error

message: fail to read content of `hsbindgen.toml` configuration file
          n.b. you have to run the command `cargo-cabal` to generate it: Os { code: 2, kind: NotFound, message: "No such file or directory" }

At this time, the current dir is project root, but hsbindgen.toml is generated in member dir. It cause toml::config() failed to find hsbindgen.toml.

Following is a reproduced project

|- Corgo.toml
|- binding-haskell
 |- Cargo.toml
 |- hsbindgen.toml
 |- binding-haskell.cabal
 |- Setup.lhs
 |- src
  |- ...

./Cargo.toml

[workspace]
members = ["binding-haskell"]

[workspace.package]
...

At this project, toml::config() will try finding ./hsbindgen.toml, rather than ./binding-haskell/hsbindgen.toml.

@yvan-sraka yvan-sraka self-assigned this May 24, 2023
@yvan-sraka
Copy link
Owner

This issue seems to be linked to https://github.com/yvan-sraka/antlion/issues/1! Thank you very much for the bug report. I will investigate it and get back to you shortly :)

@silver-ymz
Copy link
Author

Thank you for responding. It seems that they are 2 different issues. I add an empty [workspace] manually in the sandbox's Cargo.toml, which successfully fix https://github.com/yvan-sraka/antlion/issues/1 temporarily. However, custom attribute still panicked.

yvan-sraka added a commit that referenced this issue Jun 18, 2023
- Update dependency to reflexive 0.4 (previously named antlion)

- Fix #2: custom attribute panicked when using it in a member of a workspace
@yvan-sraka yvan-sraka mentioned this issue Jun 18, 2023
@yvan-sraka
Copy link
Owner

yvan-sraka commented Jun 18, 2023

In this pull request, https://github.com/yvan-sraka/hs-bindgen-attribute/pull/3/files#diff-1492d610b0587c015f48aac6fff2fee1fcc7310f2c5ef01d6c178332ebfda682R14-R18, I introduced a HSBINDGEN_CONFIG_DIR environment variable that can be set by users. This approach allows users to specify a path for the hsbindgen.toml config file. However, another potential method is to utilize the CARGO_MANIFEST_DIR https://doc.rust-lang.org/cargo/reference/environment-variables.html environment variable, which inherently points to the directory of the Cargo.toml manifest.

The issue at hand is that you want to place the hsbindgen.toml file in the module directory that uses the binding generation, rather than the root directory of the Cargo workspace. While a CARGO_MANIFEST_DIR-like approach seems cleaner, it might not cater to this specific need as it points to the workspace root, and I've not found yet a non-hacky way of doing it …

@silver-ymz
Copy link
Author

Maybe we need to wait for rust-lang/cargo#3946 to finish.
It imports a new environment variable CARGO_WORKSPACE_DIR, which points to workspace root.

@yvan-sraka
Copy link
Owner

yvan-sraka commented Jun 19, 2023

Thank you for your pointers. It's a bit unclear to me, reading the cargo documentation, how CARGO_MANIFEST_DIR should behave if CARGO_WORKSPACE_DIR is introduced.

From what I understand so far, CARGO_MANIFEST_DIR refers to the directory of the current crate, and could serve as the default value of HSBINDGEN_CONFIG_DIR (the location for the hsbindgen.toml file). Is that in line with what you need in your use case?

I'm also uncertain if these environment variables might be accessible only in a build.rs script. If so, it would require a workaround to export the crate directory path as a constant.

I will update my PR and run some tests to clarify this. If you already have a sample repository, that would be very helpful :)

yvan-sraka added a commit that referenced this issue Jun 19, 2023
- Update dependency to reflexive 0.4 (previously named antlion)

- Fix #2: custom attribute panicked when using it in a member of a workspace
@silver-ymz
Copy link
Author

silver-ymz commented Jun 19, 2023

Thank you! CARGO_MANIFEST_DIR meets my needs. It works well about hsbindgen.toml.

According to my research, cargo environment variables can be accessible here.

However, there is a new bug. Generated haskell source files will try writing in root dir rather than CARGO_MANIFEST_DIR. Maybe we should also use CARGO_MANIFEST_DIR here.

fs::write(
format!("src/{module}.hs"),
haskell::template(&module, signatures),
)

yvan-sraka added a commit that referenced this issue Jun 19, 2023
- Update dependency to reflexive 0.4 (previously named antlion)

- Fix #2: custom attribute panicked when using it in a member of a workspace
@yvan-sraka
Copy link
Owner

Thank you for your feedback! I've updated my PR based on your suggestions :) I also decided to remove the option of setting HSBINDGEN_CONFIG_DIR through an environment variable, as I couldn't really identify a use case that would justify this feature. Let me know if you think there's anything else that needs fixing before I hit the merge button :D

yvan-sraka added a commit that referenced this issue Jun 20, 2023
- Update dependency to reflexive 0.4 (previously named antlion)

- Fix #2: custom attribute panicked when using it in a member of a workspace
yvan-sraka added a commit that referenced this issue Jun 20, 2023
- Update dependency to reflexive 0.4 (previously named antlion)

- Fix #2: custom attribute panicked when using it in a member of a workspace
yvan-sraka added a commit that referenced this issue Jun 20, 2023
- Update dependency to reflexive 0.4 (previously named antlion)

- Fix #2: custom attribute panicked when using it in a member of a workspace
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

Successfully merging a pull request may close this issue.

2 participants