richterger / Perl-LanguageServer

Language Server for Perl
Other
224 stars 53 forks source link

global variables use in BEGIN causes false positives #15

Closed slymz closed 4 years ago

slymz commented 4 years ago

Take

# file foo.pl
BEGIN {
    our $GLOBALVAR = 1;
}
use Bar;
Bar::BarFun();

where

package Bar;
BEGIN {
    die 'assert' unless defined $::GLOBALVAR;
}

sub BarFun {
    print "hello from Bar::BarFun\n";
}
1;

This is legitimate Perl. The pattern here is just a mock up, but in reality it is used to declare a common tools location. This is a convenient and a very very conservative use of global variable in our large codebase.

With Perl::LanguageServer we get in the Problems pane:

Bar.pm(2)
   assert at - line 3.
   BEGIN failed--compilation aborted at - line 4.

and any modules that use Bar.pm get other usage errors, making Problems pane unusable (See issue #14)

Could there be a solution for this?

One simple idea is to allow declaration of global variables manually, just the same way perl.perlInc setting is configured.

thanks

richterger commented 4 years ago

The sysntax checker just runs perl -c Bar.pm . So you could set perlCmd in your vscode perl configuration, to a small script that set your global var and then runs the syntax check