jaz303 / phake

A rake/make clone for PHP 5.3
365 stars 27 forks source link

Do not chdir() to phakefile directory #31

Open clue opened 10 years ago

clue commented 10 years ago

This is done in order to preserve relative path passed as arguments, as they're relative to the current working directory.

Consider the directory structure:

project/
|- demo/
|   \- example.txt
\- Phakefile.php

Consider I have a task named test which accepts a single argument file:

Open for discussion:

jaz303 commented 10 years ago

Build systems in general must cd to the directory containing the build file so that any file references within the file itself are resolved predictably and consistently; rake and make certainly both perform a chdir.

If implemented, this feature should either be a) behind a command-line flag or b) set within the Phakefile itself (e.g. a leading comment like // phake-nochdir.

clue commented 10 years ago

rake and make certainly both perform a chdir

Alright, admittedly I have little experience with relative paths in either make or rake, and a quick search didn't turn up many results either. So you can confirm this is actually the case?

My assumption was that all paths are relative to the current working directory (as with normal (php) scripts), whereas you're suggesting that they should be relative to the file they're defined in, right? This shouldn't show any obvious effects if you're running phake from the folder where your Phakefile is located, but it differs if you're running if from a sub-folder (say "demo/" in the above example).

Personally, I'm okay with it either way, it's just that latter case was unexpected. So if we keep it that way, we should probably address a few things instead:

jaz303 commented 10 years ago

I've tested the behaviour across make and rake:

clue commented 10 years ago

Thanks for taking the time to confirm this!

When the -f option is used, the working directory is again unchanged.

So in other words, using relative paths is unpredictable as it depends on several factors not in control of the Phakefile author? If that is actually the case, I'm even more tempted to say that using chdir() is a bad idea and should be avoided.

In PHP, one usually works around this issue by using paths like require __DIR__ . '/myfile.php' instead. This makes it explicit on when to use relative and when to use absolute paths. Some quick searches seem to indicated this is applicable to rake/ruby as well?

If so, it's probably easier (and safer) to refrain from trying to work around relative paths as per the original patch.

Also, this would open up a few possibilities like