devcontainers / spec

Development Containers: Use a container as a full-featured development environment.
https://containers.dev
Creative Commons Attribution 4.0 International
3.62k stars 234 forks source link

Allow specifying subdirectory to open when port forwarding detected #413

Open heaths opened 9 months ago

heaths commented 9 months ago

We would like a way to configure a path (subdirectory) of a site to open from a devcontainer. In VSCode, for example, clicking the browse button - or from the automatic prompt to open a browser when a port is forwarded - we could open a site like http://127.0.0.1:4000/azure-sdk instead of only http://127.0.0.1:4000. For GitHub Codespaces, this would be forwarded port to a remote continer.

To portAttributes or otherPortAttributes, this would add:

Property Type Description
path string Optional path relative to the site root to open in a browser e.g., "/blog" on forwarded port 4000 would open http://127.0.0.1:4000/blog on localhost.

To the schema for both portAttributes or otherPortAttributes1, this would look like:

"portsAttributes": {
    "type": "object",
    "patternProperties": {
            "(^\\d+(-\\d+)?$)|(.+)": {
                    "type": "object",
                    "description": "A port, range of ports (ex. \"40000-55000\"), or regular expression (ex. \".+\\\\/server.js\").  For a port number or range, the attributes will apply to that port number or range of port numbers. Attributes which use a regular expression will apply to ports whose associated process command line matches the expression.",
                    "properties": {
+                      "path": {
+                          "type": "string",
+                          "description": "Optional path relative to the site root to open in a browser e.g., '/blog' on forwarded port 4000 would open `http://127.0.0.1:4000/blog` on localhost."
+                      },
                       "onAutoForward": {

See microsoft/vscode-remote-release#9518 for additional context.

1 Since portAttributes and otherPortAttributes appear to be the same schema, wouldn't a $ref to a single object definition be sufficient and easier to maintain?

samruddhikhandale commented 9 months ago

Hi 👋

This sounds cool, thanks for putting this up 💡

Quick clarifying question, as portsAttributes apply to all the ports specified in forwardPorts property. Is this something you are expecting? or do you prefer to only add path property to certain/specific forwarded ports?

heaths commented 9 months ago

Quick clarifying question, as portsAttributes apply to all the ports specified in forwardPorts property. Is this something you are expecting? or do you prefer to only add path property to certain/specific forwarded ports?

How do they apply to all ports when you're required to specify a port specification (single integer or string range) e.g.,

"portAttributes": {
  "4000": {
    "path": "/azure-sdk"
  }
}

I certainly wouldn't want this to apply to all ports specified in the "forwardPorts": [] array.

samruddhikhandale commented 9 months ago

Thanks for clarifying, your request makes sense to me. Along with the cli, spec and schema changes, I believe this will also require a change in VS Code, looping in @chrmarti for his insights.

chrmarti commented 9 months ago

/cc @alexr00 for the VS Code side.

SteveBombardier commented 9 months ago
SteveBombardier commented 9 months ago

Je sais pas comment faire

alexr00 commented 9 months ago

This seems reasonable to me!

heaths commented 9 months ago

This seems reasonable to me!

@alexr00 what would be the next steps? Is this something you'd like help with, if I can? I looked in the VSCode repo originally and didn't find any code that seemed to process portAttributes - just the schemas or something. I may have time for working on this.

chrmarti commented 8 months ago

@samruddhikhandale @alexr00 Is this something Codespaces can adopt too? I remember the implementation there to be more involved than when the VS Code UI runs in a local window.

samruddhikhandale commented 8 months ago

@samruddhikhandale @alexr00 Is this something Codespaces can adopt too? I remember the implementation there to be more involved than when the VS Code UI runs in a local window.

Looping in @osortega who can help answer this question.

alexr00 commented 8 months ago

The next steps:

  1. Find someone to do the work to support this. That would usually be me. I can see if we can fit it into an upcoming plan, or someone else can work on it and I'd accept a PR.
  2. Do the work to support this from the remote.portsAttributes VS Code setting. Code pointer: https://github.com/microsoft/vscode/blob/573a9ff995c4a63624eede783d6daa99da01c012/src/vs/workbench/services/remote/common/tunnelModel.ts#L192-L193
  3. Once this is supported by VS Code I think it makes sense to add support to devcontainers schema.

@alexr00 Is this something Codespaces can adopt too? I remember the implementation there to be more involved than when the VS Code UI runs in a local window.

I would expect that there should be any additional work from Codespaces to make this happen. This would just be a property in the portsAttributes setting, and I think we should be able handle everything else from within VS Code. I would need to check how things currently work in Codespaces in the browser, but so long as they already support navigating to a subdirectory it should be fine.

@heaths when would you expect the subdirectory to be applied?

heaths commented 8 months ago
  • When the url opened from the Ports view? (very doable)
  • When the url is opened from an onAutoForward action in the portsAttributes? (very doable)

Just those two. The link in the terminal window - at least as output from jekyll serve - is already correct e.g., http://localhost:4000/azure-sdk.

I'd be happy to submit PRs to respective repos, but it seems all those - including the one you linked to that I found previously - are just parsing/processing the config. Is the code that actually uses it and does all the forwarding / browser work elsewhere? Is that internal/private and would need to be done by you or someone on your team?

alexr00 commented 8 months ago

I don't think we actually need to make any changes to how the port is forwarded. If a process is listening at localhost:4000 on the remote machine and you want to access http://localhost:4000/azure-sdk, then we should be able to just forward localhost:4000 locally to localhost:4000 then adding a path like /azure-sdk should "just work".

The thing that we need to do is make sure to append the path (eg. /azure-sdk) to the local URI in the correct places:

I would suggest adding a preferredPath property here:

https://github.com/microsoft/vscode/blob/68f565d32a8248b1c5677426621c34a875c1a8da/src/vs/workbench/services/remote/common/tunnelModel.ts#L49-L66

Updating the parsing somewhere here:

https://github.com/microsoft/vscode/blob/68f565d32a8248b1c5677426621c34a875c1a8da/src/vs/workbench/services/remote/common/tunnelModel.ts#L217-L251

The merging of attributes here: https://github.com/microsoft/vscode/blob/68f565d32a8248b1c5677426621c34a875c1a8da/src/vs/workbench/services/remote/common/tunnelModel.ts#L964-L965

Then ensuring the preferredPath gets applied correctly here:

https://github.com/microsoft/vscode/blob/68f565d32a8248b1c5677426621c34a875c1a8da/src/vs/workbench/services/remote/common/tunnelModel.ts#L720-L734

here:

https://github.com/microsoft/vscode/blob/68f565d32a8248b1c5677426621c34a875c1a8da/src/vs/workbench/services/remote/common/tunnelModel.ts#L482-L497

and here:

https://github.com/microsoft/vscode/blob/68f565d32a8248b1c5677426621c34a875c1a8da/src/vs/workbench/services/remote/common/tunnelModel.ts#L449-L468