Skip to content
Permalink
Browse files

Work in progress - resolve directories from config file location (#1904)

* Add resolveFromConfigFile config option

Treats all paths as relative to the location of the config file, not
the current working directory of the process.

This commit just changes psalm, further commits will be needed to
apply the change to psalter and the LSP server.

* Copy asset xml files into project root for testing, delete during teardown

Needed since paths are now resolved relative to the position of the
file.

Not sure why I only saw a test failre for 1.xml - would have expected it
for all eight files.

* Fix following rebase

* Move psalm --init handly code above working directory setting code

If there's no psalm.xml yet we can't use the location of psalm.xml to
set our working directory

* Move Psalm version output code above working directory resolution

Working directory doesn't need to be known to output version constant

* Rely on new config file based working directory in end to end test

* Dont use rely on config dir for --alter - not currently working

* Fix code style error

* Add failing test for supporting config without `resolveFromConfigFile="true"`

* Don't treat config directory as a path to check

* Document resolveFromConfigFile setting
  • Loading branch information...
bdsl authored and muglug committed Jul 6, 2019
1 parent 40a5194 commit 94f934627c6901e2e85e2cf7dc1384e2af9a0909
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -52,6 +52,7 @@
<xs:attribute name="findUnusedVariablesAndParams" type="xs:string" />
<xs:attribute name="parseSql" type="xs:string" />
<xs:attribute name="maxStringLength" type="xs:string" />
<xs:attribute name="resolveFromConfigFile" type="xs:string" />
</xs:complexType>

<xs:complexType name="ProjectFilesType">
@@ -24,6 +24,18 @@ Psalm uses an XML config file (by default, `psalm.xml`). A barebones example loo
```
Enabling this will make Psalm very strict, such that it needs to be able to evaluate the type of every single statement, and emitting a bevy of `Mixed*` issues if the types cannot be determined. Defaults to `false`.

#### resolveFromConfigFile

```xml
<psalm
resolveFromConfigFile="[bool]"
/>
```
If this is enabled, relative directories mentioned in the config file will be resolved relative to the location
of the config file. If it is disabled, or absent they will be resolved relative to the working directory of the Psalm process.

New versions of Psalm enable this option when generating config files. Older versions did not include it.

#### useDocblockTypes

```xml
@@ -10,6 +10,7 @@
checkForThrowsDocblock="false"
throwExceptionOnError="0"
findUnusedCode="true"
resolveFromConfigFile="true"
xsi:schemaLocation="https://getpsalm.org/schema/config config.xsd"
>
<projectFiles>
@@ -333,6 +333,14 @@ class Config
*/
public $find_unused_variables = false;
/**
* Whether to resolve file and directory paths from the location of the config file,
* instead of the current working directory.
*
* @var bool
*/
public $resolve_from_config_file = false;
/**
* @var string[]
*/
@@ -466,15 +474,15 @@ protected function __construct()
* Searches up a folder hierarchy for the most immediate config.
*
* @param string $path
* @param string $base_dir
* @param string $current_dir
* @param string $output_format
*
* @throws ConfigException if a config path is not found
*
* @return Config
* @psalm-suppress MixedArgument
*@throws ConfigException if a config path is not found
*
*/
public static function getConfigForPath($path, $base_dir, $output_format)
public static function getConfigForPath($path, $current_dir, $output_format)
{
$config_path = self::locateConfigFile($path);
@@ -487,7 +495,7 @@ public static function getConfigForPath($path, $base_dir, $output_format)
throw new ConfigException('Config not found for path ' . $path);
}
return self::loadFromXMLFile($config_path, $base_dir);
return self::loadFromXMLFile($config_path, $current_dir);
}
/**
@@ -526,20 +534,22 @@ public static function locateConfigFile(string $path)
* Creates a new config object from the file
*
* @param string $file_path
* @param string $base_dir
* @param string $current_dir
*
* @return self
*/
public static function loadFromXMLFile($file_path, $base_dir)
public static function loadFromXMLFile($file_path, $current_dir)
{
$file_contents = file_get_contents($file_path);
$base_dir = dirname($file_path) . DIRECTORY_SEPARATOR;
if ($file_contents === false) {
throw new \InvalidArgumentException('Cannot open ' . $file_path);
}
try {
$config = self::loadFromXML($base_dir, $file_contents);
$config = self::loadFromXML($base_dir, $file_contents, $current_dir);
$config->hash = sha1($file_contents . \PSALM_VERSION);
} catch (ConfigException $e) {
throw new ConfigException(
@@ -555,6 +565,7 @@ public static function loadFromXMLFile($file_path, $base_dir)
*
* @param string $base_dir
* @param string $file_contents
* @param string|null $current_dir Current working directory, if different to $base_dir
*
* @return self
* @psalm-suppress MixedArgument
@@ -564,11 +575,13 @@ public static function loadFromXMLFile($file_path, $base_dir)
* @psalm-suppress MixedOperand
* @psalm-suppress MixedPropertyAssignment
*/
public static function loadFromXML($base_dir, $file_contents)
public static function loadFromXML($base_dir, $file_contents, $current_dir = null)
{
$config = new static();
if ($current_dir === null) {
$current_dir = $base_dir;
}
$config->base_dir = $base_dir;
$config = new static();
$schema_path = dirname(dirname(__DIR__)) . '/config.xsd';
@@ -635,6 +648,20 @@ public static function loadFromXML($base_dir, $file_contents)
$config->hide_external_errors = $attribute_text === 'true' || $attribute_text === '1';
}
if (isset($config_xml['resolveFromConfigFile'])) {
$attribute_text = (string) $config_xml['resolveFromConfigFile'];
$config->resolve_from_config_file = $attribute_text === 'true' || $attribute_text === '1';
}
if ($config->resolve_from_config_file) {
$config->base_dir = $base_dir;
} else {
$config->base_dir = $current_dir;
$base_dir = $current_dir;
}
if (isset($config_xml['autoloader'])) {
$autoloader_path = $config->base_dir . DIRECTORY_SEPARATOR . $config_xml['autoloader'];
@@ -201,6 +201,11 @@ function getPathsToCheck($f_paths)
/** @var string */
$input_path = $input_paths[$i];
if ($i > 0 && in_array($input_paths[$i-1], ['-c', '--config'])) {
// This is the path to the config file, not a path to check.
continue;
}
if (realpath($input_path) === realpath(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'psalm')
|| realpath($input_path) === realpath(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'psalter')
|| realpath($input_path) === realpath(Phar::running(false))

0 comments on commit 94f9346

Please sign in to comment.
You can’t perform that action at this time.