Skip to content

Debug API: Add the ability to open source files using their source reference #32845

Closed

Description

@hbenl

I'd like to implement a "Loaded Scripts Explorer" for the Firefox debug adapter but realized that I can only open the files shown in that explorer if they are "local", but not if they'd need to be fetched from Firefox via the debug adapter (e.g. third-party libraries loaded from a CDN, scripts injected by a Browser extension, etc.). However, the ability to view such "remote" files is one of the primary use cases for this explorer (IMHO).

It is already possible to view a "remote" file if it is referenced by a stackframe - in this case it is fetched via the debug adapter using its numeric source reference. But I can't do that in my explorer for two reasons:

  • I can't tell VS Code about source files and their associated source references (unless they show up in a stackframe)
  • I can't ask VS Code to open a file using its source reference

I was thinking about implementing a document provider to work around these limitations but that would only be a partial solution: VS Code wouldn't know about the relationship between the files from that content provider and the debugger, so it wouldn't be possible to set breakpoints and if the user stepped into that file later on, VS Code would open a second tab for the file (because it is fetched from the debug content provider and hence is a different file from VS Code's point of view).

My proposal would be:

  1. add a NewSourceEvent (containing a Source object in its body) to the debug protocol
  2. add the ability to open a file using a debugSessionId and a sourceReference

Activity

  1. added
    debug
    on Aug 20, 2017
  2. added
    feature-request
    on Aug 21, 2017
  3. weinand commented on Aug 21, 2017

    @weinand
    Contributor

    Until we have designed and implemented an official API for this you can use the same approach as node-debug:

    Since the "path" is not interpreted by VS Code in any you can encode whatever you like in it.

    When constructing the explorer tree, we fetch paths from the DA. If a node in the tree is selected, we use the path to construct a uri and then open a document with it. VS Code sends the path back to the DA to load the content. So only the DA "knows" what's in a uri.

  4. hbenl commented on Aug 21, 2017

    @hbenl
    Author

    Yup, this works, thanks for the detailed explanation!

    Of course this preliminary solution has the same limitation as the content provider approach that I described above, i.e. I can't set breakpoints in the opened "remote" file and when I step into this file, a second tab for the file is opened.

    In the meantime I realized that part 1 of my proposal (the NewSourceEvent) may be unnecessary if VS Code doesn't require the sourceReference used in part 2 of my proposal to be already known - after all it only has to copy it into a SourceRequest and send that to the DA. But it would still need to get a filename for the source to show to the user.

  5. weinand commented on Aug 21, 2017

    @weinand
    Contributor

    Holger Benl (@hbenl) you can set breakpoints in the opened readonly file and they are even persisted by the VS Code frontend. And if you are getting a second tab for the same file, then your DA probably uses different paths for the same file.

    Try node-debug and set breakpoints in internal modules or remote files. They should work like regular files on disk and they don't open in additional tabs.

    You just have to make sure that your DA hands out and receives Source objects in a consistent way.

    The Source.sourceReference is optional, so if you prefer a path, then please use Source.path.
    Source.name is displayed in the UI and Source.path is shown if you hover over a name.

  6. hbenl commented on Aug 25, 2017

    @hbenl
    Author

    OK, I can set breakpoints now (I had set mimeType in the SourceResponse to application/javascript, but Code only recognizes the deprecated text/javascript).
    But I still get 2 tabs for the same file, even though the path is identical.
    I tried node-debug and it also gives me 2 tabs for the same file:

    let path = require('path');
    path.basename('/foo/bar');

    Set a breakpoint on the second line, start the debugger, click on <node_internals>/path.js in the Loaded Scripts Explorer, then click "Step Into". Result:
    screenshot_2017-08-25_15-18-19
    Breakpoints that were set in the tab opened by the Loaded Scripts Explorer are working, but they don't show up in the tab that is used when stepping through the code.

  7. hbenl commented on Sep 10, 2017

    @hbenl
    Author

    Another problem I have with opening a remote file using debug: uris is that I have to put the remote file's url inside the debug: uri. That would be OK if I could encode the url - but then I'd have to encode all urls in the Source objects in order to be consistent.
    I actually tried that, but then I ran into the next problem: if the remote file's url contains an encoded character, it gets messed up somewhere along the way:
    Let's say I have a remote file with the url http://host/test.js?x=%3F, the corresponding debug uri would be debug:http%3A%2F%2Fhost%2Ftest.js%3Fx%3D%253F?session=<uuid>. The path argument in the sourceRequest then becomes http://host/test.js%3Fx=%3F - which is wrong, because if I decode that, I get http://host/test.js?x=?...

  8. weinand commented on Sep 10, 2017

    @weinand
    Contributor

    Yes, I noticed that problem too and I'm working on a solution...

  9. hbenl commented on Sep 25, 2017

    @hbenl
    Author

    Fixed with #23194.

  10. 3 remaining items

  11. weinand commented on Sep 26, 2017

    @weinand
    Contributor

    Holger Benl (@hbenl) thanks a lot for verifying this.
    I've added the "Verified" label.

  12. hbenl commented on Oct 15, 2017

    @hbenl
    Author

    I had only verified this on Linux, now I noticed that on Windows opening remote sources works but setting breakpoints in remote sources opened from the Loaded Scripts Explorer doesn't work (checked with the Chrome and Firefox debug adapters).
    Here's the args of the sourceRequest when I open the remote source from the Loaded Scripts Explorer:

    {
        "sourceReference":5,
        "source":{
            "path":"http://localhost:8080/test.js",
            "sourceReference":5
        }
    }

    And here's the args of the setBreakpointsRequest when I try to set a breakpoint in the opened tab:

    {
        "source":{
            "path":"http:\\localhost:8080\\test.js",
            "name":"test.js"
        },
        "lines":[13],
        "breakpoints":[{"line":13}],
        "sourceModified":false
    }

    Note how the sourceReference is lost in the setBreakpointsRequest and the path contains a broken URL.
    This may also be related to #29985.

  13. added
    verification-needed
    and removed
    verification-needed
    verified
    on Oct 26, 2017
  14. weinand commented on Oct 30, 2017

    @weinand
    Contributor

    Verifiers:
    This feature was reopened because it was not working correctly on Windows.
    To verify you need a debugger that identifies source by http URLs (e.g. http://localhost:8080/test.js) not by file paths, e.g. the Chrome debugger.

    You can use this VS Code project for verification.

    On Windows:

    • unzip and open with VS Code
    • from VS Code integrated terminal do:
      • npm install
      • npm run start
    • run "Launch Chrome" debug session
    • in "Loaded Scripts" explorer find "myScript.js" and set breakpoint in line 5
    • in Chrome press "Click Me!" button

    Breakpoint should be hit.

  15. locked and limited conversation to collaborators on Dec 10, 2017
  16. added a commit that references this issue on Sep 6, 2026
    e8bc158
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

debugfeature-requestverification-neededverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions