microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.73k stars 29.09k forks source link

Variable substition: ${packagePath} #27162

Closed omniscient closed 3 years ago

omniscient commented 7 years ago

In the spirit of https://github.com/Microsoft/vscode/issues/396, please consider adding a new variable that can be used in launch.json and that will refer to the package directory (packageRoot) of the currently opened file within the editor.

This is going to be useful to reduce the number of profiles necessary to debug each package. As an example, here's what we have to do currently such that we can debug our tape unit tests directly from the code editor:

Given that we have a mono repo structure like this:

Root
  package.json
  Packages
    api
      package.json
      src
      dist
    orm
      package.json
      src
      dist

Given we develop in Typescript and we resolve the current file name using ${fileBasenameNoExtension}, Given we have both a "api" package and a "orm" package in a git mono repo, We are stuck doing this to debug our tests inside the code editor:

{
        "type": "node",
        "request": "launch",
        "name": "Debug::api current test",
        "sourceMaps": true,
        "protocol": "inspector",
        "stopOnEntry": false,
        "program": "${workspaceRoot}/node_modules/tape/bin/tape",
        "args": [
          "${workspaceRoot}/packages/api/dist/_test/**/${fileBasenameNoExtension}.js"
        ],
        "cwd": "${workspaceRoot}/packages/api",
        "console": "internalConsole"
      },
      {
        "type": "node",
        "request": "launch",
        "name": "Debug::orm current test",
        "sourceMaps": true,
        "protocol": "inspector",
        "stopOnEntry": false,
        "program": "${workspaceRoot}/node_modules/tape/bin/tape",
        "args": [
          "${workspaceRoot}/packages/orm/dist/_test/**/${fileBasenameNoExtension}.js"
        ],
        "cwd": "${workspaceRoot}/packages/orm",
        "console": "internalConsole"
      }

The request is for a variable that identifies the root directory of each package so that we could re-write our launch.json like this:

 {
        "type": "node",
        "request": "launch",
        "name": "Debug unit test with tape",
        "sourceMaps": true,
        "protocol": "inspector",
        "stopOnEntry": false,
        "program": "${workspaceRoot}/node_modules/tape/bin/tape",
        "args": [
          "${packageRoot}/dist/_test/**/${fileBasenameNoExtension}.js"
        ],
        "cwd": "${packageRoot}",
        "console": "internalConsole"
      }
isidorn commented 7 years ago

I believe that ${workspaceRootFolderName} solves your problem. The whole list of variables can be found here https://code.visualstudio.com/docs/editor/tasks#_variable-substitution

If none of them solve your issue than please let me know for the path /a/b/c/d/e.txt and the repository is /a/b what do you want extracted with the variable

omniscient commented 7 years ago

@isidorn Thanks for your quick response on this. As identified above, we are already using the ${workspaceRoot} variable which is identical to ${workspaceRootFolderName} in that it actually returns the root directory. In a git mono repo, you have multiple root, one for each package. By default, a mono repo has a folder into which all package (what would equate to a single git repo) lives in. The standard i've seen is to create subfolder named packages. Under that 'packages' folder lives each different component. There is really 2 things I think for this to work.

  1. A way to tell vscode that you have multiple packages and that the package folder is named 'xyz'
  2. A way, by using the configured package root, to return into a variable each package root folder.

Let's take my example above. On my system I have my git repository in c:\git\myrepo. Within the myrepo git repository, I have a structure as identified above:

myrepo
  package.json
  Packages
    api
      package.json
      src
      dist
    orm
      package.json
      src
      dist

Given the above example, that means I have c:\git\myrepo\packages set as my root for every different package that I manage in that mono repo. In the example, my mono repo handles 2 different packages that are in the following folders on my system:

  1. api which lives in c:\git\myrepo\packages\api
  2. orm which lives in c:\git\myrepo\packages\orm

The request is that in launch.json, I have access to a new variable $(packageRoot} that knows that when I got a file open under the api project, for example c:\git\myrepo\packages\api\src\__test\blabla.ts, the new variable returns c:\git\myrepo\packages\api. Similarly, when I have a file open in the orm project, that new variable would return c:\git\myrepo\packages\orm

Hope this makes sense, let me know if you need more information.

isidorn commented 7 years ago

@omniscient thanks for providing more information however what you seem to be needing seems very specific for mono debugging. On the VSCode side we are language agnostic and do not provide any variable subsitition that is speicifc for some language. This should in theory be handled by the mono debug extension, however our current extension API is not strong enough to support this use case.

Due to the following leaving this open as a feature request and assiging to backlog.

omniscient commented 7 years ago

@isidorn : I am sorry to have to digress here but to be 100% clear, nowhere did I mention a programming language of any sort. What I am talking about is monolithic version control using git.

Would you mind taking a 2nd look at this with that fact in mind? I would really appreciate if you could at least indicate where/what would need to be changed and how, at a high-level, it could done? Maybe I could PR it for you guys if it's not too bad?

Once again, we are talking about a way to identify, that 1, we are dealing with a mono repo and 2, we have a variable that identifies the packages root folder.

isidorn commented 7 years ago

Thanks for additional clarification. This sounds like a very speicifc feature request.

Code pointer https://github.com/Microsoft/vscode/blob/master/src/vs/workbench/services/configurationResolver/node/configurationResolverService.ts#L48

We accept PRs, however keep in mind that if it is too complex or too language specific we will not accept it. Also keep in mind that we currently have no code that detects the type of a repo. Due to that I believe this would be hard to do.

jcrben commented 6 years ago

Would https://github.com/Microsoft/vscode/issues/25786 get the job done? It helps to get into the relative nested folders.

alexr00 commented 3 years ago

I think there is a way you can do this already.

  1. Specify an environment variable that has the relative path to your packages folder in VS Code's workspace settings. Example:
    "terminal.integrated.env.linux": {
        "packagePath": "packages"
    },
  2. Use that variable in you launch.json. Example:
    {
        "type": "node",
        "request": "launch",
        "name": "Debug unit test with tape",
        "sourceMaps": true,
        "protocol": "inspector",
        "stopOnEntry": false,
        "program": "${workspaceRoot}/node_modules/tape/bin/tape",
        "args": [
          "${workspaceRoot}/${env:packagePath}/dist/_test/**/${fileBasenameNoExtension}.js"
        ],
        "cwd": "${workspaceRoot}/${env:packagePath}",
        "console": "internalConsole"
      }