Psalm: Running on specific files ignores config

Created on 4 Sep 2019  Â·  13Comments  Â·  Source: vimeo/psalm

Hello,
I'm running psalm on a pre-commit hook and would like to check the specific files which are going to be committed. Works good so far, but the config done in the psalm.xml seems to be ignored. Especially the config in <projectFiles><ignoreFiles>

    <projectFiles>
        <directory name="src"/>
        <ignoreFiles>
            <directory name="tests"/>
        </ignoreFiles>
    </projectFiles>

If I now change a file in /tests, the pre-commit hook will throw an error. If I run psalm for the whole project, not just a specific file, everything went fine.

Most helpful comment

I can't control how Husky passes the file list to the command.

The hack I used in a similar situation (build scripts defined in composer.json) was to include the shell comment character at the end of the script line:

{
    "scripts": {
          "static-analysis" : "psalm #"
    }
}

This way file arguments are added, but are ignored because they effectively become a part of a comment.

All 13 comments

Are you running it as psalm tests/SomeTest.php? When running like that Psalm assumes you know what you're doing and checks the file even though it matches the ignoreFiles directive.

yes, like this.
I see :+1: thanks for the clarification

I'm running into this same issue, but it's because we're using some other tools (Husky with pre-commit hooks in yarn), and it passes all the changed files to the commands.

We have a command in a package.json script that looks like this: psalm --show-info=true --config=tests/psalm.xml. When Husky's pre-commit hook runs, it passes all the changed files to the command, like this:

psalm --show-info=true --config=tests/psalm.xml '.editorconfig' '.gitignore' '.phan/config.php' 'composer.json' 'composer.lock' 'package.json' 'phpcs.xml' 'phpunit.xml.dist' 'scripts/linter.sh' 'scripts/static-analysis.sh' 'src/Service.php' 'tests/phpstan.neon' 'tests/psalm-baseline.xml' 'tests/psalm.xml' 'tests/unit/ServiceTest.php' 'tests/unit/TestCase.php'

I don't have a way to disable or turn this off, and I don't want Psalm to run for files that aren't in src/.

Any recommendations?

Run with --diff --diff-methods which effectively does the same thing, but is a little safer – it should also reanaiyse files that are dependent on the ones you've changed

Those two flags will become the default once I stop seeing the occasional bug in Vimeo's usage

Unfortunately, those two flags don't appear to make any difference.

I'll clarify...

They make a difference if I run the command like this:

psalm --diff --diff-methods --show-info=true --config=tests/psalm.xml '.editorconfig' '.gitignore' '.phan/config.php' 'composer.json' 'composer.lock' 'package.json' 'phpcs.xml' 'phpunit.xml.dist' 'scripts/linter.sh' 'scripts/static-analysis.sh' 'src/Service.php' 'tests/phpstan.neon' 'tests/psalm-baseline.xml' 'tests/psalm.xml' 'tests/unit/ServiceTest.php' 'tests/unit/TestCase.php'

But when that same command is run as a result of yarn kicking it off (e.g., yarn run staticanalysis), it fails, as if it is ignoring the options and config file entirely.

Sorry... I'll clarify some more.

When I run yarn run staticanalysis, it's fine. It's when that gets run as part of Husky's pre-commit hook that it fails.

🤣

What I'm saying is you shouldn’t need to add the file list – those dependencies should be calculated automatically with --diff --diff-methods

I can't control how Husky passes the file list to the command.

At any rate, I'm an idiot.

I needed to add the following to my psalm.xml config:

        <ignoreFiles>
            <directory name="tests"/>
        </ignoreFiles>

And then, I needed to make sure that change was staged. That did the trick!

Thanks for your help!

I can't control how Husky passes the file list to the command.

The hack I used in a similar situation (build scripts defined in composer.json) was to include the shell comment character at the end of the script line:

{
    "scripts": {
          "static-analysis" : "psalm #"
    }
}

This way file arguments are added, but are ignored because they effectively become a part of a comment.

Thanks for the tip!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muglug picture muglug  Â·  3Comments

ADmad picture ADmad  Â·  3Comments

roukmoute picture roukmoute  Â·  3Comments

vudaltsov picture vudaltsov  Â·  3Comments

orklah picture orklah  Â·  3Comments