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

improvement(generate:typetests,list): Only write output file when contents differs #21798

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

tylerbutler
Copy link
Member

@tylerbutler tylerbutler commented Jul 9, 2024

Adds a utility function to write to files only if the contents you're writing is different from what's already there. This function is used in the type test generation and flub list to ensure we only write changes when needed.

This function may be useful for other commands as well, but those changes are left for separate PRs.

I also opportunistically replaced some synchronous function calls with async counterparts.

@github-actions github-actions bot added area: build Build related issues base: main PRs targeted against main branch labels Jul 9, 2024
@tylerbutler tylerbutler requested a review from a team July 9, 2024 00:37
Co-authored-by: Alex Villarreal <716334+alexvy86@users.noreply.github.com>
@tylerbutler tylerbutler changed the title improvement(generate:typetests): Only write output file when contents differs Jul 9, 2024
@CraigMacomber
Copy link
Contributor

I think the motive I had for suggesting this work (avoid needless file churn as it could invalidate incremental build state) was flawed for multiple reasons:

  1. We don't use modification time of inputs vs outputs to do our incremental build
  2. If we did use the input vs output build times, tasks which consume other outputs could end up forever dirty due to this change, which is what I was thinking it might fix.

In conclusion: If I was right about how our build worked (and I wasn't) this would be really bad. Since I don't actually know how this part of our build works, someone else should probably make the judgement on if we want to do this.

Copy link
Contributor

@jason-ha jason-ha left a comment

Choose a reason for hiding this comment

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

I still think it is a better idea to understand the root trouble with .done tracking before making things more complicated.

filePath: PathLike,
contents: string,
): Promise<boolean> {
const fileContents = await readFile(filePath, { encoding: "utf8" });
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the file doesn't exist yet? I would guess it throws, but in that case we want an explicit write.

* @param filePath - The path to the file to write.
* @param contents - The contents to write to the file.
* @returns True if the file was written; false otherwise.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a note about performance improvements?
This helper fully reads and closes a file before writing it (if needed).
In the common case that writing is needed, it will be more efficient to open the file for read+write to start and keep it open until any writing is dealt with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: build Build related issues base: main PRs targeted against main branch
4 participants