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

Fix --port=0 option to correctly open on random port #1883

Merged
merged 1 commit into from
Nov 23, 2022
Merged

Conversation

GregBrimble
Copy link
Member

@changeset-bot
Copy link

changeset-bot bot commented Sep 19, 2022

🦋 Changeset detected

Latest commit: d744570

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Sep 19, 2022

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/3532075809/npm-package-wrangler-1883

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/1883/npm-package-wrangler-1883

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/3532075809/npm-package-wrangler-1883 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.developers.workers.dev/runs/3532075809/npm-package-cloudflare-pages-shared-1883

@codecov
Copy link

codecov bot commented Sep 19, 2022

Codecov Report

Merging #1883 (980f3f1) into main (f8ea2b6) will decrease coverage by 0.00%.
The diff coverage is 75.00%.

❗ Current head 980f3f1 differs from pull request most recent head d744570. Consider uploading reports for the commit d744570 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1883      +/-   ##
==========================================
- Coverage   71.88%   71.88%   -0.01%     
==========================================
  Files         150      150              
  Lines        9525     9524       -1     
  Branches     2469     2468       -1     
==========================================
- Hits         6847     6846       -1     
  Misses       2678     2678              
Impacted Files Coverage Δ
packages/wrangler/src/dev/local.tsx 22.39% <0.00%> (ø)
packages/wrangler/src/dev.tsx 86.54% <83.33%> (-0.51%) ⬇️
packages/wrangler/src/dev/start-server.ts 62.82% <100.00%> (ø)
...ackages/wrangler/src/__tests__/helpers/mock-bin.ts 100.00% <0.00%> (+5.26%) ⬆️

@rozenmd
Copy link
Contributor

rozenmd commented Oct 10, 2022

Hey so with cloudflare/miniflare#382 merged, is this good to go?

@GregBrimble
Copy link
Member Author

Just waiting on a Miniflare release 😊

@rozenmd
Copy link
Contributor

rozenmd commented Oct 10, 2022

Also may want to add this to --experimental-local

@rozenmd
Copy link
Contributor

rozenmd commented Oct 10, 2022

@mrbbot pls

@mrbbot
Copy link
Contributor

mrbbot commented Oct 11, 2022

https://github.com/cloudflare/miniflare/releases/tag/v2.10.0 🙂

@GregBrimble
Copy link
Member Author

#2003

@GregBrimble
Copy link
Member Author

hmm, this actually isn't quite ready. It improves things, but it doesn't update the hotkey things, so pressing b will try to open localhost:0 :(

@GregBrimble GregBrimble force-pushed the fix-port-0 branch 2 times, most recently from 980f3f1 to 8deef81 Compare November 22, 2022 22:28
@@ -171,25 +171,39 @@ export function DevImplementation(props: DevProps): JSX.Element {
);
}

let ip: string;
Copy link
Member Author

Choose a reason for hiding this comment

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

Sheer madness, I know.

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem is that useHotkeys() relies on these values for it's b "open a browser" feature. If we store this in proper React state, it gets the value fine, but we then need to call setIp or whatever in the onReady callback. This causes the entire component to re-render, which re-initializes Miniflare, which causes the onReady callback to be called again, etc. etc.

I tried an if (newValue !== newValue) { setValue() }, but still couldn't prevent a render loop.

port={args.port || configParam.dev.port || (await getLocalPort())}
ip={args.ip || configParam.dev.ip}
initialPort={
args.port ?? configParam.dev.port ?? (await getLocalPort())
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to come back and refactor getLocalPort and waitForPortToBeAvailable so they're no longer race-y in a subsequent PR.

Copy link
Contributor

@rozenmd rozenmd left a comment

Choose a reason for hiding this comment

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

I had a question, it got answered later in the PR

LGTM 👍

abortSignal: abortController.signal,
});
}
await waitForPortToBeAvailable(initialPort, {
Copy link
Contributor

Choose a reason for hiding this comment

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

if initialPort is 0, we don't have to wait for the port available though - node should just give us an available one?

@@ -651,6 +651,11 @@ export async function waitForPortToBeAvailable(
}

function checkPort() {
if (port === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

...oh okay

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup. I'll tidy this up in a subsequent PR removing these race-y functions.

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

3 participants