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

Always set the OUT_DIR environment variable #9084

Closed
JOOpdenhoevel opened this issue Jan 18, 2021 · 4 comments
Closed

Always set the OUT_DIR environment variable #9084

JOOpdenhoevel opened this issue Jan 18, 2021 · 4 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@JOOpdenhoevel
Copy link

JOOpdenhoevel commented Jan 18, 2021

Describe the problem you are trying to solve
I want to create a procedural attribute macro that analyzes a function signature and exports this information into a file. This file will be used later by another program, so it's definitely needed. However, the environment variable OUT_DIR isn't always set, which would tell me a good spot to write the file to. If the crate that uses the macro has a build script, OUT_DIR is set, but if it doesn't have a build script, OUT_DIR isn't set. Therefore, I would need to tell my users to always have a build script so that the macro works. This does not work well when there are many users of that macro.

Describe the solution you'd like
My requested solution is simple: Always set the OUT_DIR environment variable to a valid output directory, if the built crate has a build script or not.

Notes
One point against this is that OUT_DIR might collide with other uses of the variable, or that the name OUT_DIR isn't a good name for that in general. However, this would be a whole other discussion since it is already set for and used by build scripts.

@JOOpdenhoevel JOOpdenhoevel added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Jan 18, 2021
@ehuss
Copy link
Contributor

ehuss commented Feb 13, 2021

I think there may be a misunderstanding about how proc-macros work and how they are intended to work and how build scripts work. Generally, proc-macros should not persist any data to disk, and should be idempotent transformations on the input, without side-effects. OUT_DIR is specifically intended for the build script of the package being built, which for the case of a proc-macro is the macro itself, not the packages that are using the proc-macro.

It is not clear from the description why you are using proc-macros to interface with other tooling, but I recommend considering other approaches to what you are trying to accomplish. If you want help discussing different ideas, I would suggest one of the user forums.

@ehuss ehuss closed this as completed Feb 13, 2021
@notdanilo
Copy link

IMO OUT_DIR should be available in every place possible. I am using proc-macros to generate language bindings and it would be helpful.

@Technohacker
Copy link

Technohacker commented Aug 4, 2022

If this isn't to be changed, it'd be helpful to document this behaviour, that OUT_DIR (EDIT: Or maybe more than just OUT_DIR) is only set if there's a build script. We had an issue where we accidentally removed our build script (that generated Rust source files), but our include code panicked saying OUT_DIR was not set. It took me a while to realise this was the source of the issue

I could open a PR on the Cargo Book to make this change if that's alright

@weihanglo
Copy link
Member

Thanks for your advice! It seems to be documented already.

* `OUT_DIR` — If the package has a build script, this is set to the folder where the build
script should place its output. See below for more information.
(Only set during compilation.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

5 participants