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 forwarding in VS Code #2656

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

lalver1
Copy link
Member

@lalver1 lalver1 commented Jan 31, 2025

Closes #2642

This PR changes the behavior of the dev container to only auto forward the ports associated with the docs and server services in the compose file. By setting "otherPortsAttributes": { "onAutoForward": "ignore" }, any other ports should not be auto-forwarded at all.

Reviewing

  1. Exit your current dev container
  2. Switch to this branch
  3. Rebuild and reopen the dev container
  4. Start the debugger
  5. Verify that the app is always reachable from your browser at localhost:DJANGO_LOCAL_PORT/ (open the benefits app, submit the eligibility form, navigate to the admin, etc.) and that the PORTS tab in VS Code always only shows 2 ports, the server and docs ports

@lalver1 lalver1 self-assigned this Jan 31, 2025
@github-actions github-actions bot added the docker Application container, devcontainer, Compose, etc. label Jan 31, 2025
Copy link

Coverage report

This PR does not seem to contain any modification to coverable code.

@lalver1 lalver1 marked this pull request as ready for review January 31, 2025 21:54
@lalver1 lalver1 requested a review from a team as a code owner January 31, 2025 21:54
@thekaveman
Copy link
Member

I'm still not quite sure about the underlying issue here. I've never experienced the situation described in #2642, and it doesn't seem straightforwardly reproducible (e.g. does it happen that way every single time going through an Agency Card flow?)

In any case, on Linux at least, this change appears to have no effect. I'm still seeing all the various ports on the ports tab:

image

@thekaveman thekaveman added the chore Chores and tasks for code cleanup, dev experience, admin/configuration settings, etc. label Feb 1, 2025
@machikoyasuda machikoyasuda self-requested a review February 3, 2025 17:14
@machikoyasuda
Copy link
Member

I tested this locally on my macOS.

My ports tab looks like this:
image

I am able to access the server and healthcheck:
image

And the docs: http://localhost:8001/benefits/
image

But not the Benefits client app at http://localhost:11369/ after running the debugger. Am I mising something?

Copy link
Member

@machikoyasuda machikoyasuda left a comment

Choose a reason for hiding this comment

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

Confirmed on VS Code Insiders (pre-release) 1.97.0-insider. Ran the app in container, opened docs, server and the client. Went through client flow.

@thekaveman
Copy link
Member

@machikoyasuda:

But not the Benefits client app at http://localhost:11369/ after running the debugger. Am I mising something?

I'm guessing, but from the screenshot it looks like maybe DJANGO_LOCAL_PORT was not set to 11369?

image

I would expect that very last entry to have 0.0.0.0:11369 in the Forwarded Address column.

@lalver1
Copy link
Member Author

lalver1 commented Feb 7, 2025

I added dev:8000 to forwardPorts

  "forwardPorts": ["docs:8001", "server:8000", "dev:8000"],
  "otherPortsAttributes": { "onAutoForward": "ignore" },

but did not quite get the behavior I was expecting:
image
VS Code did not open extra ports when debugging (that's good) but dev was available at both ports 11369 and 51422. This may be confusing for the dev experience, so I'm leaning to not making changes to this branch.

My guess was that dev would have behaved just like docs and server, but there are 2 differences that may be the reason for this behavior:

  1. dev is the dev container service in devcontainer.json ("service": "dev"), somehow making port allocation in the Ports tab different compared to the other services
  2. In the compose file, dev is assigned a specific port on the host but docs and server get allocated any unassigned port of the host, and this creates a different behavior in the Ports tab of VS Code

@angela-tran
Copy link
Member

So I noticed in your screenshots @lalver1 that you don't have any "Statically forwarded" ports.

In mine, I have two.

image

I've realized they correspond with the ports being published in compose.yml for dev.

If I comment out ports for dev, they go away. Then, if I start my app through the debugger, I see I can not reach it when I go to http://localhost:11369 in my browser, which makes sense because I'm not forwarding the port from the devcontainer to my host.

image

(Side note: I'm not sure why I have two since we're just publishing one port, but I guess they are for the same port number, just with different hosts.)

I'm not sure why your VS Code isn't showing you the ports corresponding to the published ports for dev, but clearly they are being forwarded for you since you can reach the app at http://localhost:11369.

@angela-tran
Copy link
Member

angela-tran commented Feb 7, 2025

I was also curious about the "Auto Forwarded" ports that y'all have, and I've learned that "auto-forwarding" is referring to a feature in VS Code where the terminal will detect ports being allocated and set up forwarding for that port.

I didn't find this in any official documentation, but rather from piecing together various comments online:

I think auto-forwarding is what's happening when we click the Starting development server at http://0.0.0.0:8000/ link in the VS Code terminal and see a new port in the Ports tab.

@lalver1 Maybe the extra port that would show up and cause hanging is from the auto-forwarding feature?

@angela-tran
Copy link
Member

I noticed I don't get auto-forwarded ports 🤔 and then I found that in my User Settings, I have it turned off:

Screenshot from 2025-02-07 11-29-42

image

I don't remember doing this, but in my Firefox history, I see I searched for "vs code keeps adding auto forwarded port" on June 5, 2024, so maybe I did change it? 🤣

image

@thekaveman
Copy link
Member

thekaveman commented Feb 7, 2025

Oh this seems relevant too... from that same settings pane:

image

The "will not be un-forwarded" bit seems like a potential to cause a hang.

@angela-tran
Copy link
Member

With what we know now, does @lalver1's solution seem ok? I guess there are multiple places where the behavior can be changed (Settings vs. devcontainer.json)

@thekaveman
Copy link
Member

With what we know now, does @lalver1's solution seem ok? I guess there are multiple places where the behavior can be changed (Settings vs. devcontainer.json)

Oh yeah I think it seems OK, but also maybe we should disable this automatic forwarding? Seems like it doesn't matter (you've had it disabled) and it also seems like it could be the source of the some of the problems @lalver1 and @machikoyasuda experience on Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Chores and tasks for code cleanup, dev experience, admin/configuration settings, etc. docker Application container, devcontainer, Compose, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port Forwarding in VS Code is unstable
4 participants