richterger / Perl-LanguageServer

Language Server for Perl
Other
222 stars 53 forks source link

env launch configuration value isn't implemented #74

Closed bradmccormack closed 3 years ago

bradmccormack commented 3 years ago

Thanks for this extension.

I was trying to fire up the debugger to debug a unit test file using Prove. The particular test uses environment variables which I had specified for the "env" key in my launch config which doesn't seem to be implemented.

I checked the extension source and can't see "env" anywhere which explains why the Perl script wasn't seeing the environment variables with the values I had specified.

If this is not the case, could you please tell me how to use the env key. I have zero experience developing Vs code extensions so might have missed something.

richterger commented 3 years ago

It's implementend and working like this

            {
            "name": "current file",
            "type": "perl",
            "request": "launch",
            "program": "${workspaceFolder}/${relativeFile}",
            "stopOnEntry": true,
            "env": { "TEST":"foo"}
            },
bradmccormack commented 3 years ago

@richterger they are not passed through. I tried that and the Perl script didn't have the corresponding environment values populated.

When I set a breakpoint on the debugging session the executable (of type vscode.DebugAdapterExecutable) shows that the options param is null.

The options param should have the env key set as far as I understand for the environment to get passed through.

This is my first time working with vs code extensions so I may be missing something.

Thanks for your prompt response.

bradmccormack commented 3 years ago

I think something like this will be required

    vscode.debug.registerDebugAdapterDescriptorFactory('perl', {
        createDebugAdapterDescriptor(session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable) {
            executable = new vscode.DebugAdapterExecutable(executable.command, executable.args.concat(debug_adapter_port), {env: session.configuration.env})

            console.log ('start perl debug adapter: ' + executable.command + ' ' + executable.args.join (' '));
            return executable;
        }
    });
bradmccormack commented 3 years ago

I think something like this will be required

  vscode.debug.registerDebugAdapterDescriptorFactory('perl', {
      createDebugAdapterDescriptor(session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable) {
          executable = new vscode.DebugAdapterExecutable(executable.command, executable.args.concat(debug_adapter_port), {env: session.configuration.env})

          console.log ('start perl debug adapter: ' + executable.command + ' ' + executable.args.join (' '));
          return executable;
      }
  });

If it helps, I made the above changes and also added the cwd and the environment variables are exposed as expected.

dseynhae commented 3 years ago

I think that the "env" key should be a hash, but I get it to work as an array:

"env" : [
          "USER",
          "${env:USER}",
          "KONSOLE_DBUS_SESSION",
          "${env:KONSOLE_DBUS_SESSION}",
          "PSG_PDK",
          "${env:PSG_PDK}",
          "LOGNAME",
          "${env:LOGNAME}",
...
],

Note that I put a wrapper around my code invocation, which reads my current environment, and forces it into my code-workspace file, before I open it up in code... That way, I always inherit my full environment in VS Code...

bradmccormack commented 3 years ago

Thanks for trying to help. I agree it should be a hash / object.

To give you more context this is executing in a virtual machine. So your ssh options are in place. The wrapper in this case won't help because I want to provide a bunch of launch configs with different environment options and have the launch.json file tracked in Git.

If you are able to test if you are able to debug a simple Perl test file using prove and ssh that would be appreciated. I completely forgot to mention that before which complicates things.

What I'm trying to achieve is

i) Start vscode on my host. ii) with a "t" file selected launch my "Debug active Perl test" debugging config. iii) Start debugging

I do see the language server processes fire up in the virtual machine.

My settings.json config (the relevant options)

    "perl.enable" : true,
    "perl.sshAddr" : "local.foobar.com",
    "perl.sshUser" : "foo.bar",
    "perl.sshWorkspaceRoot": "/data/foobar",
    "perl.fileFilter": [".pm, .pl , .t"],
    "perl.ignoreDirs": [".vscode, .git"],
    "perl.logLevel" : 2,
    "perl.showLocalVars" : true,
    "perl.perlInc": [
        "/data/lib/modules",
        "/data/foobar/t/lib",
        "/data/foobar/t/lib/critic_policies",
        "/opt/perl-5.22.0/lib/site_perl/5.22.0/x86_64-linux-thread-multi",
        "/opt/perl-5.22.0/lib/site_perl/5.22.0",
        "/opt/perl-5.22.0/lib/5.22.0/x86_64-linux-thread-multi",
        "/opt/perl-5.22.0/lib/5.22.0"
    ]

The specific launch config entry I'm trying to get work looks like

        {
            "name": "Debug active Perl test",
            "presentation" : {
                "group" : "perldebug",
                "order": 2,
            },
            "type": "perl",
            "request": "launch",
            "program": "/opt/perl/bin/prove",
            "args" : [
                "--lib",
                /* sshWorkSpaceRoot doesn't appear to work, provide the full path for now */
                "/data/foobar/t/${fileBasename}"
            ],
            "env": [
                "FOO" , "1",
                "BAR" , "2"
            ],
            "stopOnEntry": false,
            "reloadModules": true,
        },

If you have any advice I would really appreciate it. Thanks in advance.

bradmccormack commented 3 years ago

I'm at the point now where prove starts executing and the debugger stops just after the shebang in the prove file but exits after the process_args sub executes.

It looks like

BEGIN { pop @INC if $INC[-1] eq '.' }
use strict;
use warnings;
use App::Prove;

my $app = App::Prove->new;
$app->process_args(@ARGV);
exit( $app->run ? 0 : 1 );

Is this due to Prove forking? Will your extension support this use case? An existing extension required addressing this explicitely

I wasn't sure whether to create a new issue around this behavior so just put it here.

richterger commented 3 years ago

env setting is an object now, the array issue was fixed in 2.1.0

For debugging you need to run a single process. Fork will not work. This is an limitation of Perl itself, not of the LanguageServer

dseynhae commented 3 years ago

I must be doing something wrong then: image

Yet when I try to change my array into an object: image

bradmccormack commented 3 years ago

Thanks @richterger.

@beltaurus it complains for me too.

richterger commented 3 years ago

This issue was fix in 2.1.0, so it worked, but you get the waning in the lauch.json. The definition for the configuration was fixed in 2.2.0, so now the warning should disapear also.