4

I'm using perlcritic with perltidy and while the other rules are applied, I'm, getting the following error message:

Code is not tidy at line 1, near 'package MyPackage;'.

I've run perltidy to tidy the code but I am still getting the problem. In the .perlcriticrc file I've added the following rule with the path to .perltidyrc file.

[CodeLayout::RequireTidyCode]
perltidyrc = /path/to/.perltidyrc

I'm running perlcritic like this:

perlcritic --profile .perlcriticrc file.pm

I'm sure that perlcritic is using the rc file as I'm able to turn rules on and off by ammending the file content. I'm also sure that perltidy is using the correct rc file in the same way.

How can I prevent this error without disabling the RequireTidyCode rule?

I've added an example to recreate the problem by cutting the actual files down to the minimum where I'm still getting the error. This is how I'm running it:

cd /tmp
perltidy -b -nst TestCritic.pm
perlcritic --profile /tmp/.perlcriticrc /tmp/TestCritic.pm

The first line of the critic output is:

Code is not tidy at line 1, near 'package TestCritic;'.

These are the files:

TestCritic.pm

package TestCritic;

sub startup {
    my $self = shift;

    my $config;
    my $service_name;

    if ( defined $ENV{MYVAR} ) {
        if ( $ENV{MYVAR} eq 'TESTVAL' ) {
            $config       = { Config => { file => '/tmp/tmp.txt' } };
            $service_name = 'TestCritic.' . $$;
        }
    }
}

1;

.perltidyrc

-pbp
-lp
-bar
-l=120
-nolc
-vmll
-cti=1
-pt=0
-vt=0
-vtc=0

.perlcriticrc

# Show all errors
severity = 1
verbose = 11
theme = core + pbp + bugs + maintenance + cosmetic + complexity + security + tests
exclude = Subroutines::ProhibitCallsToUndeclaredSubs

Versions are: perlcritic v1.132 perltidy v20181120

Thanks,

Geraint Anderson
  • 3,234
  • 4
  • 28
  • 49
  • When you ran perltidy, did you tell it to modify your code in place with [`-backup-and-modify-in-place`](https://metacpan.org/pod/distribution/Perl-Tidy/bin/perltidy#-b,-backup-and-modify-in-place)? – ThisSuitIsBlackNot Apr 25 '19 at 16:49
  • Yes, I've used that. I've also written the critic output to a new file and diffed them to make sure it's working and they are the same. – Geraint Anderson Apr 25 '19 at 17:17
  • 3
    In that case, I can't reproduce your problem. Could you add a short, self-contained example that other people can run? – ThisSuitIsBlackNot Apr 25 '19 at 17:26
  • I've included an example perl file, perlcriticrc and perltidy rc which can be used to recreate the problem. – Geraint Anderson Apr 29 '19 at 09:52
  • On the off-chance, are you using some particular editors or a code management system? See [this bug](https://github.com/Komodo/KomodoEdit/issues/2829) for Komodo. (I can't reproduce either) – zdim May 18 '19 at 03:41
  • I'm using VS Code but I get the problem when running on the command line too. – Geraint Anderson May 22 '19 at 10:14

1 Answers1

1

Put ...

use strict;
use warnings;

... at the top of the file, before the package declaration, then you should be good.

Tested against perlcritic v1.130

TFBW
  • 989
  • 7
  • 12
  • I'm getting the same problem: `perltidy had errors!! at line 1, near 'use strict;'.` – Geraint Anderson May 15 '19 at 08:38
  • I'm unable to reproduce your problem with the configuration given in your question, including the addition of `[CodeLayout::RequireTidyCode]`, using perlcritic v1.130 and perltidy v20170521. The critic complains about a few things, but "perltidy had errors" is not one of them. – TFBW May 15 '19 at 10:14