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 watch #2100

Merged
merged 25 commits into from
May 24, 2024
Merged

Build watch #2100

merged 25 commits into from
May 24, 2024

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented May 9, 2024

This is a prerequisite for supporting h2 debug cpu in Vite. Features:

  • Add --watch to h2 build
  • Add --build --watch --codegen to h2 preview
  • Support --diff in h2 preview

Note

Miniflare is currently not compatible with CSP nonces and WS connections. Therefore, live reload doesn't work with h2 preview --build --watch until that's fixed and you need to refresh the browser manually.
I've reported the problem here: cloudflare/workers-sdk#5829

Copy link
Contributor

shopify bot commented May 9, 2024

Oxygen deployed a preview of your fd-build-watch branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment May 23, 2024 6:14 AM
subscriptions ✅ Successful (Logs) Preview deployment Inspect deployment May 23, 2024 6:13 AM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment May 23, 2024 6:13 AM
vite ✅ Successful (Logs) Preview deployment Inspect deployment May 23, 2024 6:13 AM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment May 23, 2024 6:13 AM
optimistic-cart-ui ✅ Successful (Logs) Preview deployment Inspect deployment May 23, 2024 6:13 AM

Learn more about Hydrogen's GitHub integration.

Comment on lines 166 to 178
{
name: 'hydrogen:cli:client',
buildStart() {
clientBuildStatus?.resolve();
clientBuildStatus = deferPromise();
},
buildEnd(error) {
if (error) clientBuildStatus.reject(error);
},
writeBundle() {
clientBuildStatus.resolve();
},
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The server rebuild needs to wait until the client build is finished. This deferred promise helps with that.

Comment on lines +268 to +269
if ('close' in clientBuild) promises.push(clientBuild.close());
if ('close' in serverBuild) promises.push(serverBuild.close());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type of clientBuild and serverBuild is different in watch mode. Using in here correctly discriminates the type.

Comment on lines -66 to +120
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production';
if (!process.env.NODE_ENV)
process.env.NODE_ENV = watch ? 'development' : 'production';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using development with watch mode to allow our CSP to connect to Miniflare's loopback server (localhost:).

Comment on lines +159 to +161
async onRebuild() {
if (projectBuild.state === 'pending') {
projectBuild.resolve();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need the first build finished before starting MiniOxygen with files in disk.

@@ -92,7 +92,6 @@ export const commonFlags = {
description:
'Automatically generates GraphQL types for your project’s Storefront API queries.',
required: false,
default: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out Oclif's dependOn for flags doesn't work if the flag has a default value.

@frandiox frandiox requested a review from a team May 16, 2024 09:12
@frandiox frandiox marked this pull request as ready for review May 16, 2024 09:12
@blittle
Copy link
Contributor

blittle commented May 16, 2024

Testing notes:

✅ - skeleton template - h2 build --watch - successfully rebuilds as I modify files (everything I tried except the vite config, which I don't think matters)
✅ - skeleton template - h2 preview --build
❌ - skeleton template - h2 preview failed
image
✅ - skeleton template - h2 preview --build --watch --codegen - Modifying files rebuilds, and also changing queries updates the codegen
❌ - infinite-scroll example - h2 build --watch --diff - It built successfully, just watch didn't rebuild when I modified the collection route.
❌ - infinite-scroll example - h2 preview --diff - Fails with a similar error as above. Does preview now require a --build arg?
❌ - infinite-scroll example - h2 preview --build --watch --codegen --diff - built successfully, but also does not rebuild when I modify the route (or the query, codegen doesn't rerun).

@frandiox
Copy link
Contributor Author

@blittle Thanks for the thorough review! Most problem were due to a bad last minute copy-paste 🤦

❌ - skeleton template - h2 preview failed

diff had default: false so Oclif thinks the flag is provided... and it then triggered the requirement on build. I've removed all the default: false values to make them undefined instead, which doesn't create this problem.

❌ - infinite-scroll example - h2 build --watch --diff - It built successfully, just watch didn't rebuild when I modified the collection route.

Copy-pasted code and forgot to enable the watcher for the diff folder 🙃

❌ - infinite-scroll example - h2 preview --diff - Fails with a similar error as above. Does preview now require a --build arg?

Yes, it does. I've added a comment in the code to explain this because I even forgot about it myself when you mentioned it:

    // Diff in preview only makes sense when combined with --build.
    // Without the build flag, preview only needs access to the existing
    // `dist` directory in the project, so there's no need to merge the
    // project with the skeleton template in a temporary directory.

❌ - infinite-scroll example - h2 preview --build --watch --codegen --diff - built successfully, but also does not rebuild when I modify the route (or the query, codegen doesn't rerun).

Same thing, forgot to enable the watcher. Note that codegen reruns but we are ignoring the changes in Git.


I think this is all now fixed in the recent commints 👍

@blittle
Copy link
Contributor

blittle commented May 23, 2024

I can only repro this issue 50% of the time or so, but when running h2 build --watch --diff inside infinite-scroll example. I modify a file, it rebuilds, and then I try and kill the process (ctr + c) but the process doesn't die. It just infinitely keeps printing this error:

image

I have to kill the whole terminal to get it to stop.

@frandiox
Copy link
Contributor Author

I can only repro this issue 50% of the time or so, but when running h2 build --watch --diff inside infinite-scroll example. I modify a file, it rebuilds, and then I try and kill the process (ctr + c) but the process doesn't die. It just infinitely keeps printing this error:

@blittle I was only able to reproduce this very few times. It looks like even when we close Rollup watchers, the build still continues and then it starts failing synchronously when it can't find the diff files that we just removed on process exit.

I've added a few changes to try to ensure the build exits before we remove files so that we don't go into an infinite sync error loop. I'm not able to reproduce it after these changes but maybe I was just lucky. Please try it again but, in any case, these errors only happens with --diff so it's probably not a big issue.

Copy link
Contributor

@blittle blittle 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 see the --diff problem, but like you said, it only affects us.

@frandiox frandiox merged commit 608389d into main May 24, 2024
13 checks passed
@frandiox frandiox deleted the fd-build-watch branch May 24, 2024 05:52
michenly pushed a commit that referenced this pull request May 24, 2024
* Improve deferPromise utility for debugging

* Implement --watch in build command

* Support --codegen with --watch in build command

* Add --build to preview command

* Fix codegen flag relationship

* Use new flag in skeleton

* Pass --entry from preview to build

* Extract resource cleanup logic

* Setup resource cleanup for build watch

* Add --watch to preview command

* Replace command in examples

* Consider classic compiler in preview

* Support --diff in preview to test in examples

* Changesets

* Silence non-actionable build logs during preview

* Typo

* Improve error when worker file is not found

* Remove default false values in flags to avoid issues with flag dependencies

* Fix diff watcher in build and preview

* Add explanatory comment for flag combination

* Use helper

* Ensure processExit is called after 5 seconds

* Try to ensure process exits
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.

None yet

2 participants