See issue #599698: Load a drushrc.php from cwd for an example use case where drush could not correctly find the drupal root when the user had set up some symlinks for convenience. The patch attached to this incident fixes the problem being experienced there in a more direct way, by following symlinks as they are encountered.

Comments

malclocke’s picture

StatusFileSize
new639 bytes

I had already uploaded a patch that fixes this with less impact at http://drupal.org/node/599698#comment-2131418 before that issue got hijacked and this one created, although the title of this issue is more descriptive of my problem. Re-attaching that patch here, have been using it for several weeks now without issue. It fixes the problem without the recursive walk in the patch above.

greg.1.anderson’s picture

I apologize; I fear I did not notice your patch above among all of the comments about loading drushrc.php from cwd (the title of the incident). Your solution is much more elegant than my patch; however, there are a number of people who call drush.php directly, and handling the symlinks in drush_locate_root solves this issue for them too. The recursive walk towards the directory root already exists in the current code; my patch might lengthen this walk by following symlinks.

@moshe: I leave it up to you which version you want to commit. There's no harm in taking both, but only one is necessary.

moshe weitzman’s picture

Status: Needs review » Fixed

Committed greg's patch since we do need to handle folks who call drush.php directly. I do that routinely:

alias drush='php -d error_reporting=E_ALL ~/contributions/modules/drush/drush.php'

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

owen barton’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new1.74 KB

This patch prevents Drush from finding a Drupal root on my sandbox. Here is the layout:

Drupal root is at /home/USER/workspace/drupal-6/

Site code is at /home/USER/workspace/sitename.6/

Site is symlinked in so /home/USER/workspace/drupal-6/sites/sitename.6 is a symlink to /home/USER/workspace/sitename.6/

I used to be able to cd to /home/USER/workspace/drupal-6/sites/sitename.6/ and Drush would walk up the current directory chain (not jumping to symlinks source destinations) and would find the drupal-6 directory. It would then correctly determine "sitename.6" as the active site, with no drushrc needed at all - I wrote this code specifically so it worked like this ;)

In other words this is the exact converse of the use case presented in #599698: Load a drushrc.php from cwd, which had the files stored inside the Drupal sites folder, and then a symlink out to the workspace. I think both are perfectly valid layouts, although I think mine is probably the more common, as it fits more naturally with Eclipse's workflow (and probably other project based editors) - the only way to replicate the other layout would be to create your project, close Eclipse, move the project, create a symlink back to where the project was and then restart Eclipse.

The only practical approach that comes to mind, (without adding restrictions or an obscure option) is simply to check using one approach (e.g. without link resolving) first and if nothing is found then check the other (e.g. with link resolving). The attached patch attempts this - I have tested with both layouts and it appears to work well, but a verification would be good.

The attached patch also fixes a separate issue I found with the original path in that it would break when relative symlinks are used, because readlink only returns the content of the link (e.g. "drupal-6/sites/sitename.6", or "../../sitename.6"), which are insufficient for Drupal roots. When I switched to using realpath it worked correctly however, because this always give a path from the filesystem root.

moshe weitzman’s picture

Seems reasonable to me. Lets give in the next couple of days soon so we can modify or commit this.

greg.1.anderson’s picture

StatusFileSize
new1.79 KB

I fixed two minor bugs, neither of which are necessary unless you accept that my somewhat odd use-case is valid. See below for the use case; first, the fixes:.

  1. I prefer to follow symlinks on the first pass, and skip past them on the second pass. This still works for Owen's use case, as a drupal root won't be found on the first pass, when the symlink is followed.
  2. The second 'break' should be 'break 2', so that it will break out of both the 'while' and the 'foreach' loops. Otherwise, drush_locate_root will look for a drupal root twice, even if it finds a root on the first iteration.

For my somewhat odd use case, consider a local drupal installation with two instances of drupal, 'mydrupal.com' and 'dev.mydrupal.com'. Because these are d6 sites, they both need to be stored at 'sites/mydrupal.com'. Therefore, we store them in separate drupal roots:

/srv/www/drupal-live/sites/mydrupal.com
/srv/www/drupal-dev/sites/mydrupal.com

The web server is set up so that 'mydrupal.com' serves files from /srv/www/drupal-live, and 'dev.mydrupal.com' serves files from /srv/www/drupal-dev. So far so good; now comes the odd part. For convenience, I also set up a symlink:

/srv/www/drupal-live/sites/dev.mydrupal.com -> /srv/www/drupal-dev/sites/mydrupal.com

This symlink is never used by drupal, it is only used by drush. It is sort of an odd duck; it would never be necessary in d7, and really wouldn't be necessary with a full compliment of site aliases set up, but I still have a number of these sitting around from the time before I had site aliases. The clincher is, I never identify my sites via cwd; I name them by dirname or alias name. Therefore, Owen's patch is good enough for my uses. I present these minor bug fixes because I feel they are technically more correct. I have tested both versions with Owen's layout and malclocke's layout, and both work fine in those cases. You may commit either version -- but if you take Owen's (with follow symlinks second), I think you should still at least take the 'break 2' bugfix.

greg.1.anderson’s picture

Priority: Normal » Minor
StatusFileSize
new1.18 KB

Okay, let's do this. I went ahead and committed #5. I didn't like keeping drush-HEAD broken against this common site layout configuration, and Owen's fix restores this important functionality.

Regarding #7, here is a new patch showing just the delta between #5 and #7. I've set the priority to minor; you can discuss and decide whether to take this modification or not at your leisure.

owen barton’s picture

Thanks Greg!

The "break 2" certainly looks correct, and was just an oversight on my part.

The order of symlink/no-symlink search is not critical either way I think. It wasn't clear if the order affected your use case or not - if so, then it seems it would be fine to switch the order.

I doesn't really affect things, but the reasoning behind my suggestion - other than that I think it correlates to the more common layout - was that I think it maps to the users intention better, since they already chose to place themselves in the selected directory, rather than the "resolved" path, which they could have reached by doing "cd -P" rather than a straight "cd" if that was what they wanted.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

seems like this is rtbc with or without owen's suggestion. i leave it to greg.

greg.1.anderson’s picture

Assigned: Unassigned » greg.1.anderson
greg.1.anderson’s picture

Status: Reviewed & tested by the community » Fixed

Since the order of symlink / no-symlink doesn't affect Owen's use case, but does affect mine, I committed #8.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

joachim’s picture

Component: Code » PM (dl, en, up ...)
Status: Closed (fixed) » Active

This doesn't work with a symlink in my root folder like this:

lrwxr-xr-x 1 joachim staff 40 26 Sep 08:27 m-alpha -> sites/localhost.module-lab.alpha/modules

~/Sites/module-lab/m-alpha joachim$ drush dl kitten
Project kitten (6.x-1.0) downloaded to                               [success]
/Users/joachim/Sites/module-lab/sites/all/modules/kitten.
greg.1.anderson’s picture

Status: Active » Closed (fixed)

Drush dl is behaving correctly in the above example. dl puts downloads at the cwd only if there is no bootstrapped site. In your example above, drush can find your site root at 'module-lab', so it puts your module at sites/all/modules where they belong. You have to use --destination if you want to force dl to put downloads in a site module folder.

joachim’s picture

In that case, I don't understand that this issue fixed.

If I do

$ cd sites/localhost.module-lab.alpha/modules
$ drush dl kitten

then it correctly downloads to the subsite.

I thought the point of this issue was to correctly handle a symlink that points to a sites folder.

greg.1.anderson’s picture

Status: Closed (fixed) » Active

Okay, I'll look at it again later.

greg.1.anderson’s picture

geek-merlin’s picture

@greg: sorry, but evern after some thinking i don't understand your special use case in #7:

For my somewhat odd use case, consider a local drupal installation with two instances of drupal, 'mydrupal.com' and 'dev.mydrupal.com'. Because these are d6 sites, they both need to be stored at 'sites/mydrupal.com'.

sorry id really don't understand - according to the docs you just create a 'dev.mydrupal.com' site - what am i missing?

(edit: this may go off-topic, feel free to pm me or ignore)

greg.1.anderson’s picture

If you make a folder called "sites/dev.mydrupal.com", then certain edit actions will write "sites/dev.mydrupal.com" into the Drupal database. This means that your path will go awry (file not found) if you migrate dev.mydrupal.com back to mydrupal.com, where the path is expected to be "sites/mydrupal.com". One solution to this problem is to symlink sites/dev.mydrupal.com to sites/mydrupal.com on your live server, and let some of your paths in the database be "sites/mydrupal.com", and others be "sites/dev.mydrupal.com". On d6, I always make sure that the folder that contains settings.php has the same name for all of my dev / staging / test / live sites. Nowadays I just use "sites/default" and avoid multisites; it's simpler.

anarcat’s picture

Status: Active » Fixed

I think the issue was reopened here for the wrong reason. The original bug report was about finding the drupal root - Drush now does that fine - the problem mentionned here is more like #1158510: drush dl sends everything to sites/all even though i'm in sites/example.com that I just opened (and have a patch for sitting in my branches).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.