composer: Look “up” for composer.json if one doesn’t appear in the current folder

When I run this command:

cd some/sub/folder
composer require xxx/yyy

And I expected the dependency to be added to the project’s composer dependencies.

But, I actually got a new, empty, composer.json file in the current folder.

It seems to me that we could have composer look to see if there is a composer.json file in the current folder and, if not, “look upwards” to see if one can be found. If it is, and the user confirms, it should add the dependency there.

Essentially, this should be the behaviour for all composer commands except composer init, I think

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 23 (12 by maintainers)

Most upvoted comments

We are not taking anything away here… If you run a require command in some random directory without composer.json, and it can’t find one above, it won’t ask you and will create it in the current directory as is now the case.

If one is found above, then it prompts, and you still have the option to say no if you really want to create in the current dir.

IMO it could be done by looking up folders until at most the HOME dir, because if we reach that there might be some composer.json there that most probably isn’t the “project”. That said, it’d probably be best to do this by prompting the user with the suggestion, to make sure it’s not accidentally doing something in the wrong folder.

Even if we don’t want to look up, we should at least stop execution, cause the result will be unusable.

It is often the case with things like this that people won’t “complain” (I prefer “suggest improvement” - I wasn’t complaining) because they think they have done something “wrong”. Essentially they have, it’s just that the tool could be more “forgiving” of mistakes. We are humans.

As far as other tools are concerned, I can only comment upon the ones I use and that’s not a lot:

  • make: doesn’t look up the directory tree, just says that there is nothing to make. Importantly, though, it doesn’t add or change anything in the filesystem. So, doesn’t cause project “damage”
  • vagrant: looks back up the directory tree for a Vagrantfile and uses that. I do this every single day - it’s beautiful

@jvasseur Yes, and after that it downloads the required project and creates a parallel vendor directory different from the one that’s actually used.

@alcohol Okay. Can we start by admitting that the current behavior is potentially very problematic from a UX perspective? As a maintainer of a large project I get about a complaint a day. If we can agree that the current behavior is problematic, we can discuss BC and possible solutions, but there’s no point in doing that if we’re not on the same page.

This is the expected behaviour though. composer require will create a composer.json from scratch for you if there is not one currently in your working directory. Altering this behaviour would break backwards compatibility.

Yes, I’m suggesting it could change that behaviour, to avoid errors by humans.

The is no sense in a sub directory of a project to create a new composer.json file for any operation other than composer init, surely? It might seem like a convenience but, in practice, it just becomes an inconvenience.

At the very least we could ask for a confirmation if the file does not exist at that location?