richterger / Perl-LanguageServer

Language Server for Perl
Other
219 stars 53 forks source link

Can't find file when a relative path #163

Closed zorgnax closed 1 year ago

zorgnax commented 1 year ago

I am running into a problem where I try to debug in VSCode a script that uses a module in a local directory and it returns this error when I step into the function defined in a local .pm file:

Could not load source 'lib/Foo.pm': Unknown perlmethod _dapreq_source at /Library/Perl/5.30/Perl/LanguageServer.pm line 224.

The script has this code in it:

#!/usr/bin/perl
use lib "lib";
use Foo;
Foo::foo();

And it is trying to use a file in ./lib/Foo.pm with this code in it:

package Foo;

sub foo {
    print "hello\n";
}

1;
dseynhae commented 1 year ago

Since Perl 5.26, the standard search for libraries no longer looks in the "current directory".

To use local directories, you should use FindBin:

package main 1.0;

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib/foo";

I have used this successfully with the VSCode Perl extension...

zorgnax commented 1 year ago

I'm on 5.30.3 and if I put in a use lib "lib"; it works, like it can include the file and run the perl code in it, but the VS Code debugger doesn't show it.

richterger commented 1 year ago

Look in the Readme at FAQ, e.g. "Module not found when debugging or during syntax check"

zorgnax commented 1 year ago

Thanks for pointing me to that, but I guess I still don't understand why it is that way. Setting the perl.perlInc will work fine for me, but it is an extra step.

richterger commented 1 year ago

That's because the current working directory is not defined, when starting inside the debugger. You can set it with perl.cwd . You are using a releative path, so the current working directory matters and the debugger cannot know, what you assume as current working directory.