php-scoper icon indicating copy to clipboard operation
php-scoper copied to clipboard

Relative path inconsistency in configuration

Open MIvanIsten opened this issue 1 year ago • 5 comments

Bug report

Question Answer
PHP-Scoper version 0.18.14@d4e7ffa
PHP version 8.2.8
Platform with version Windows 11
Github Repo -

While 'output-dir' is relative to working dir, until then paths in 'exclude-files' are realtive to config file location. This behavior is not mentioned in documentation and is very confusing.

MIvanIsten avatar Jul 03 '24 12:07 MIvanIsten

This is actually a bug.

The configuration file out-dir should be relative to the config file location, any settings from the config file should remain identical regardless of the working directory, hence having them relative to the config file is the sensible behaviour.

Note that the same cannot be said about the executed commands, so if you were to pass a relative path to the command for the output dir, this one should be relative to the working dir.

theofidry avatar Jul 03 '24 12:07 theofidry

The fix should be simple: in the Configuration factory if the path is provided it should be made absolute

theofidry avatar Jul 03 '24 12:07 theofidry

For me, having relative paths relative to working dir (sources root) seems more logical.

  • The files are actually there.
  • Also no need to rewrite paths in case config file moved to another place.

MIvanIsten avatar Jul 14 '24 16:07 MIvanIsten

If you have:

<?php
// /path/to/project/php-scoper.inc.php

return [
    'out-dir' => 'my-dir',
];

The expectation is /path/to/project/my-dir.

If you execute

$ php-scoper add-prefix --out-dir=my-dir

Then the expectation (as for any CLI command that allows a relative directory – make, git, ls, cp, npm, composer etc.) is that the directory used is $(pwd)/my-dir.

Either way it should be documented though.

theofidry avatar Jul 14 '24 17:07 theofidry

I have /path/to/project ├─ build ├─ src │ ├── include_me.php │ └── exclude_me.php ├─ scoper │ ├── vendor │ └── scoper.inc.php

/path/to/project/scoper$ vendor/bin/php-scoper add-prefix --working-dir=../src --config=../scoper/scoper.inc.php
// /path/to/project/scoper/scoper.inc.php
$excludedFiles = [
    '../src/exclude_me.php',
];
return [
    'output-dir' => '../build', 
    'exclude-files' => [
        // 'src/an-excluded-file.php',
        ...$excludedFiles,
    ],
];
  • --config arg is relative to --working-dir arg (/path/to/project/src ../scoper/scoper.inc.php)
  • 'output-dir' is relative to --working-dir (/path/to/project/src ../build)
  • 'exclude-files' paths are relative to scoper.inc.php location (/path/to/project/scoper ../src/exclude_me.php)

Would be more logical to define 'exclude-files' paths relative to --working-dir since the files in question are actualy in working dir. Not to mention what a nightmare it is to maintain config file if its moved to another loaction. All 'exclude-files' paths must be recalculated relative to new config file location. Which is very weird, since all files remained in place inside working dir.

MIvanIsten avatar Jul 24 '24 14:07 MIvanIsten