code-server: asExternalUri doesn't work
Hi Guys,
I had written a new extension which renders Graphs from matplotlib into vscode webview component, it works great on vscode desktop, but I encounter problems when trying to work with it in code-server.
I cannot state what exactly the problem is but here is the workflow:
- my vscode extension starts and listens to requests for plot viewing on port 11111 (POST request)
- matplotlib custom backend sends a request to the extension with the url to open in webview
- I use vscode.env.asExternalUri tunneling in order to open the url in a webview and embed the generated address in an Iframe.
- Usually by now the webview with the webpage would have been loaded, but instead I get and error openning localhost refuse to connect
Any ideas what went wrong?
- Web Browser: Chrome
- Local OS: Ubuntu
- Remote OS: MacOS
- Remote Architecture: x86_64
code-server --version: v3.4.1
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 26 (15 by maintainers)
To fully fix for the Tabnine case we will need:
asExternalUri.I’ve identified the Root Cause as well and have an idea for fixing this.
Root Cause
Based on my findings, the call stack looks like this:
asExternalUriusingvscode.env.asExternalUriasExternalUrihere$asExternalUrihereopenerService.resolveExternalUrihereThis last function –
resolveExternalUri– tries to resolve this external uri using one the_resolvers(property on class). The problem is…there are no resolvers in the browser (at least in code-server, where i’m running withyarn watch).“Okay…so shouldn’t it throw this error?”
You would think so but if you look at the link in 3 in my call stack list, you’ll notice the code isn’t wrapped in a
try/catchblock. Therefore, that error never bubbles up.“is that a bug?”
I would say so but I might check with @code-asher. Raised upstream: https://github.com/microsoft/vscode/issues/162770
There may be more to this root cause since I’m not sure why there wouldn’t be a resolver for this external URI in the first place. I know it works in Codespaces but I can’t figure out how.
Solution
I’d like to discuss with @code-asher first but I did find this. When I add
resolveExternalUrihere called like so:then my extension uses it!
If this is the correct place to patch, then next steps would be to agree upon how external uris should be resolved. One approach could be to use code-server’s built-in proxy and do something like
localhost:8000-><base-url>/proxy/8000.Notes
I have started looking into this and will share my notes here and update this comment as I go.
CONCLUSION: I successfully reproduced the bug.
Investigation: does
asExternalUriwork for a basic callback URL? ✅Here are the steps I’m following to check:
npx yo codevscode.env.asExternalUriin extension (see code below)Expected It shows callback URL in information notification and that URL is accessible in my browser.
Actual It shows callback URL in information notification and that URL is accessible in my browser.
Investigation: does
asExternalUriwork for a URL likelocalhost:8000? ❌Similar steps as above but with this code:
Expected
It behaves like Codespaces and transforms the URL to be behind the a proxy such as
https://jsjoeio-coder-code-server-rpvvw55xp7f4x9-8000.githubpreview.dev/. More info needed here on how this should behave.Actual
It does not transform URL and returns
http://localhost:8000.Chatting with their team about this. Stay tuned!
Awesome. Thank you!
Good news - PR is pretty much good to go. We should be able to wrap it up Monday assuming all goes well.
I chatted with @code-asher offline and we came up with a fix. I’m going to work on it today and hopefully push up a draft PR soon. Keep an eye out.
Thanks for letting us know @Sominemo! That’ll be helpful when we go to fix this. We may want to write our own extension that uses asExternalUri and add an e2e test to make sure it works (when we fix this).
Dart & Flutter extension for VS Code uses asExternalUri too, and its DevTools iframes don’t work without manual port forwarding, because it tries to connect to localhost. As far as I understand, it’s related to this issue.
The Tabnine team is still facing this so we need to dig into it.
Awesome, that sounds like a really good workaround.
I’m thinking we should leave the issue open until we fix
asExternalUrisince it would be ideal to have parity with local VS Code.