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

build(lib): do not run build script based on changes to kernel_v0.rs #991

Merged
merged 2 commits into from
Nov 28, 2024

Conversation

eightfilms
Copy link

@eightfilms eightfilms commented Nov 28, 2024

#887 added a build script that builds kernel_v0.rs, but the script runs on changes to the file itself. I suspect this was the reason for test times doubling after that PR as pointed out in #903.

This PR simply removes this rerun check. I doubt this check is the correct behaviour either way, since if it reruns based on changes to the kernel_v0.rs, it means it reruns every time the file is generated - which means the build script is triggered between each invocation of cargo nextest as well.

I've tested this in my personal repo - the run is here: https://github.com/eightfilms/miden-base/actions/runs/12064550221/job/33641593187 and is based on this branch: https://github.com/eightfilms/miden-base/tree/refs/heads/kernel-build-once. There is no longer recompilation between the different test steps, which means we should expect to see CI times for the test step go back down to ~15 minutes from ~40 minutes (which is similar to the last commit prior to #887)

0xPolygonMiden#887 added a build
script that builds `kernel_v0.rs`, but the script runs on changes to the
file itself. I suspect this was the reason for test times doubling
after that PR as pointed out in 0xPolygonMiden#903.

This PR simply removes this rerun check. I doubt this check is the correct
behaviour either way, since if it reruns based on changes to the
`kernel_v0.rs`, it means it reruns every time the file is generated - which means
the build script is triggered between each invocation of `cargo nextest`
as well.
@Mirko-von-Leipzig Mirko-von-Leipzig added the no changelog This PR does not require an entry in the `CHANGELOG.md` file label Nov 28, 2024
@Mirko-von-Leipzig
Copy link
Contributor

Thank you, I think your diagnosis is correct. I wonder if the kernel errors file should also be removed in a similar fashion.

More generally I think the issue is that we're generating code in the source directory which isn't correct.

@PhilippGackstatter
Copy link
Contributor

PhilippGackstatter commented Nov 28, 2024

Thanks!

We recently had problems with the docs.rs docs not building and the build was failing because of a "read-only" filesystem. This was also because we're writing to src rather than OUT_DIR, e.g.:

We actually should not write into the src directory directly. I think build.rs files should always write into OUT_DIR (which can be written to in docs.rs environment) and then simply be included with something like include!(concat!(env!("OUT_DIR"), "/build_file.rs"));. docs.rs is setup with a read-only src directory because, I guess, it assumes that these best practices are followed. Us having problems with rustfmt and generated files in the past was basically also a symptom of us writing directly into src.
#970 (review)

So not necessarily for this PR, but it seems to me that it would be better to just move to the include approach and not write kernel_v0.rs and tx_kernel_errors.rs directly into src.

I'm curious why the CI times already went down even though the same problem should exist for the println!("cargo:rerun-if-changed={KERNEL_ERRORS_FILE}"); line, which is also built in that build.rs file. Shouldn't that file change roughly whenever the kernel_v0 file changes? (Edit: Ah @Mirko-von-Leipzig pointed this out already :D)

@Mirko-von-Leipzig
Copy link
Contributor

I'm curious why the CI times already went down even though the same problem should exist for the println!("cargo:rerun-if-changed={KERNEL_ERRORS_FILE}"); line, which is also built in that build.rs file. Shouldn't that file change roughly whenever the kernel_v0 file changes? (Edit: Ah @Mirko-von-Leipzig pointed this out already :D)

The error file write is skipped most of the time:

    // Skip this build script in BUILD_KERNEL_ERRORS environment variable is not set to `1`.
    if env::var("BUILD_KERNEL_ERRORS").unwrap_or("0".to_string()) == "1" {
        // Generate kernel error constants.
        generate_kernel_error_constants(&source_dir)?;
    }

@eightfilms could you also remove this ^^ variable from the rerun-if pragmas? This would wrap this PR up nicely, and we can go bikeshed internally over the dangers of generating code in-tree.

@eightfilms
Copy link
Author

eightfilms commented Nov 28, 2024

I'm curious why the CI times already went down even though the same problem should exist for the println!("cargo:rerun-if-changed={KERNEL_ERRORS_FILE}"); line, which is also built in that build.rs file. Shouldn't that file change roughly whenever the kernel_v0 file changes? (Edit: Ah @Mirko-von-Leipzig pointed this out already :D)

Probably because generating that file is gated behind an environment variable: https://github.com/0xPolygonMiden/miden-base/blob/main/miden-lib/build.rs#L80C5-L83C6

Regardless, +1 on the sentiment that writing into src is definitely not the best practice. We should definitely have a follow up PR that fixes this.

Edit: got frontrun by @Mirko-von-Leipzig :) just saw your comment, will do once I'm back at the computer

@eightfilms
Copy link
Author

I'm curious why the CI times already went down even though the same problem should exist for the println!("cargo:rerun-if-changed={KERNEL_ERRORS_FILE}"); line, which is also built in that build.rs file. Shouldn't that file change roughly whenever the kernel_v0 file changes? (Edit: Ah @Mirko-von-Leipzig pointed this out already :D)

The error file write is skipped most of the time:

    // Skip this build script in BUILD_KERNEL_ERRORS environment variable is not set to `1`.
    if env::var("BUILD_KERNEL_ERRORS").unwrap_or("0".to_string()) == "1" {
        // Generate kernel error constants.
        generate_kernel_error_constants(&source_dir)?;
    }

@eightfilms could you also remove this ^^ variable from the rerun-if pragmas? This would wrap this PR up nicely, and we can go bikeshed internally over the dangers of generating code in-tree.

done

@Mirko-von-Leipzig Mirko-von-Leipzig changed the base branch from main to next November 28, 2024 13:23
Copy link
Contributor

@Mirko-von-Leipzig Mirko-von-Leipzig left a comment

Choose a reason for hiding this comment

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

Thanks again!

@Mirko-von-Leipzig Mirko-von-Leipzig merged commit 57ca781 into 0xPolygonMiden:next Nov 28, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no changelog This PR does not require an entry in the `CHANGELOG.md` file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants