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

Cleanup build.rs and friends issue #1111 #1112

Merged
merged 9 commits into from
Apr 20, 2023

Conversation

eeeebbbbrrrr
Copy link
Contributor

@eeeebbbbrrrr eeeebbbbrrrr commented Apr 18, 2023

This PR "cleans up" build.rs so that it now tracks, via cargo:rerun-if-env-changed, every environment variable it uses. The justification here is that if build.rs uses an environment variable, then build.rs will need to re-run if that envar changes. It could be this is overly aggressive in that cargo probably does this for us for things like TARGET or CARGO_MANIFEST_DIR or OUT_DIR, but it seems prudent to not have to then justify why we track some envars and not others.

It also doesn't try to open $PGRX_HOME/config.toml in the case where the pg_config data is wholly described as environment variables.

And it also removes from Pgrx::home() the code that would just blindly try to create $PGRX_HOME if it doesn't exist. It instead moves that to the Err case when it's called via cargo pgrx init.

…X_HOME, should not also be creating that path.
@eeeebbbbrrrr eeeebbbbrrrr changed the title WIP: Cleanup buildrs and friends issue #1111 WIP: Cleanup build.rs and friends issue #1111 Apr 18, 2023
@eeeebbbbrrrr eeeebbbbrrrr changed the title WIP: Cleanup build.rs and friends issue #1111 Cleanup build.rs and friends issue #1111 Apr 18, 2023
@eeeebbbbrrrr eeeebbbbrrrr marked this pull request as ready for review April 18, 2023 19:35
@workingjubilee
Copy link
Member

I'm throwing the Will It Blend tests at this just to make sure.

Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Largely looks good. Some mild nits. Somewhat concerned about duplicating the tracking command confusing cargo.

cargo-pgrx/src/command/init.rs Outdated Show resolved Hide resolved
== "1";
println!("cargo:rerun-if-env-changed=PGRX_PG_SYS_GENERATE_BINDINGS_FOR_RELEASE");
let is_for_release =
env_tracked("PGRX_PG_SYS_GENERATE_BINDINGS_FOR_RELEASE").unwrap_or("0".to_string()) == "1";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial, I know, and preexisting, but it bugs me these aren't consistent.

Suggested change
env_tracked("PGRX_PG_SYS_GENERATE_BINDINGS_FOR_RELEASE").unwrap_or("0".to_string()) == "1";
env_tracked("PGRX_PG_SYS_GENERATE_BINDINGS_FOR_RELEASE").unwrap_or_else(|| "0".to_string()) == "1";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also looking again this should actually be another case of .as_ref() == Some("1")

pgrx-pg-sys/build.rs Outdated Show resolved Hide resolved
pgrx-pg-sys/build.rs Outdated Show resolved Hide resolved
@@ -760,7 +763,7 @@ fn env_tracked(s: &str) -> Option<String> {
}

fn target_env_tracked(s: &str) -> Option<String> {
let target = std::env::var("TARGET").unwrap();
let target = env_tracked("TARGET").unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that we need to add env_tracked to this, I thought Cargo was already tracking this? Are we sure we won't confuse Cargo on this point?

Comment on lines +317 to +318
let manifest_dir = env_tracked("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let out_dir = env_tracked("OUT_DIR").map(PathBuf::from).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concerns re: "should we really be adding this to the env tracking"?

@eeeebbbbrrrr
Copy link
Contributor Author

so... I blindly did this for every environment variable. My thought process was that if build.rs uses an envar, then it'll need to be rerun when that envar changes.

@workingjubilee
Copy link
Member

Whee, errant mouseclicks.

@workingjubilee
Copy link
Member

Hmm. Given the big build test suite passed on a previous version of this PR, we should be okay? I feel like my main concern is really just making sure the commit message or a comment or whatever includes "yeah this was a more... interesting decision" so that if we find an unusual bug later related to the build, shuffling through the git blame eventually turns up something.

@eeeebbbbrrrr
Copy link
Contributor Author

Hmm. Given the big build test suite passed on a previous version of this PR, we should be okay? I feel like my main concern is really just making sure the commit message or a comment or whatever includes "yeah this was a more... interesting decision" so that if we find an unusual bug later related to the build, shuffling through the git blame eventually turns up something.

I updated the PR description. If that works for you, it'll become the commit message -- once CI passes the additional cleanups I just pushed.

Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks good to me!

@thomcc
Copy link
Contributor

thomcc commented Apr 19, 2023

It could be this is overly aggressive in that cargo probably does this for us for things like TARGET or CARGO_MANIFEST_DIR or OUT_DIR, but it seems prudent to not have to then justify why we track some envars and not others.

There used to be issues where doing this for the ones cargo manages would cause spurious rebuilds. I think these have been fixed, but it's possibly worth checking.

If it helps, https://github.com/rust-lang/cc-rs/blob/main/src/lib.rs#L3132-L3139 is what I've used in the past (although the main reason cc-rs tries to avoid it is not that bug, but because emitting any rerun-if-env-changed vars can impact if rebuilds happen).

@eeeebbbbrrrr
Copy link
Contributor Author

If it helps, https://github.com/rust-lang/cc-rs/blob/main/src/lib.rs#L3132-L3139 is what I've used in the past (although

It's funny, I had this same thought yesterday afternoon while walking the dog.

the main reason cc-rs tries to avoid it is not that bug, but because emitting any rerun-if-env-changed vars can impact if rebuilds happen).

Well, we use rerun-if-env-changed to cause rebuilds, so like, /shrug?

I feel like this should be fine. If it causes rebuilds when it shouldn't then I'd argue that's a cargo bug and we should report it there and then work around it like you do in cc-rs. I'm gonna go ahead and merge and we can see what happens during our daily development.

@eeeebbbbrrrr eeeebbbbrrrr merged commit 9d0ce83 into develop Apr 20, 2023
@eeeebbbbrrrr
Copy link
Contributor Author

I feel like this should be fine. If it causes rebuilds when it shouldn't then I'd argue that's a cargo bug and we should report it there and then work around it like you do in cc-rs. I'm gonna go ahead and merge and we can see what happens during our daily development.

As usual, @thomcc was correct! :)

I've put up PR #1127 to address the constant rebuilds that started happening after this PR was merged.

I didn't narrow it down specifically because I got bored, but it's at least caused by one of the envars we access that begins with "CARGO": CARGO_CFG_TARGET_OS, CARGO_FEATURE_PGxx, or CARGO_CFG_PLRUSTC.

I still say this is some kind of cargo bug. Crate authors shouldn't have to do this.

@eeeebbbbrrrr eeeebbbbrrrr deleted the cleanup-buildrs-and-friends-issue1111 branch June 20, 2023 17:58
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 this pull request may close these issues.

3 participants