Skip to content

Conversation

@codedmonkey
Copy link
Contributor

@codedmonkey codedmonkey commented Nov 30, 2021

Fixes #840

Changes how PackageJsonSynchronizer handles dependency resolving. If a dependency is already defined under the dependencies section of package.json, Flex won't add it again under devDependencies. If multiple UX bundles require the same dependency, no action is performed.

Because the way PackageJsonSynchronizer was written, I had to change the order of the steps in the synchronization process. While it used to update the package.json for each UX dependency individually, it now resolves a list of dependencies and updates package.json at the end.

I also changed how it handles incompatible peer dependencies, no action is performed by the synchronizer if multiple ux package require incompatible peer dependencies.

@codedmonkey codedmonkey changed the title Refactor PackageJsonSynchronizer to prevent unresolvable and unintentional duplicate dependencies [WIP] Refactor PackageJsonSynchronizer to prevent unresolvable and unintentional duplicate dependencies Nov 30, 2021
$manipulator->addSubNode('devDependencies', '@'.$phpPackage['name'], 'file:'.substr($packageJson->getPath(), 1 + \strlen($this->rootDir), -13));
$dependencies['@'.$phpPackage['name']] = 'file:'.substr($packageJson->getPath(), 1 + \strlen($this->rootDir), -13);

$versionParser = new VersionParser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying on the Composer version parser to parse npm constraints looks wrong to me. The syntax is not the same between both ecosystems (some parts of the syntax are common, but not everything)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and worse than that. Some syntax is the same with different meaning (so the parser would accept it but produce a different result)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's a problem. In the current version it simply generates an invalid package.json the developer has to fix themselves. This hasn't been a common use case for Flex yet so it rarely occurs. What is the intended behavior?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the problem a bit more that this is solving? Is this if you install 2 packages that both have chart.js has a peer dependency... but they one requires v2 and another requires v3?

If so, yea... it's super rare. Well, probably not a reality at all right now. I would vote to simply "do nothing". What I mean is, if you already had chart.js in your package.json, leave it with whatever version was there. This is a problem the user will need to resolve and they will see peer dependency warnings from yarn/npm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't trying to solve it but while writing the PR I noticed it simply generated invalid JSON with 2 conflicting peer dependencies.

Apperantly I forgot the use case for peer dependencies in NPM so awesome suggestion. Thanks :)

@codedmonkey
Copy link
Contributor Author

Apologies for the delay, I had a very persistent cold the last few weeks.

@codedmonkey codedmonkey changed the title [WIP] Refactor PackageJsonSynchronizer to prevent unresolvable and unintentional duplicate dependencies Refactor PackageJsonSynchronizer to prevent unresolvable and unintentional duplicate dependencies Dec 22, 2021
@codedmonkey codedmonkey changed the title Refactor PackageJsonSynchronizer to prevent unresolvable and unintentional duplicate dependencies Refactor PackageJsonSynchronizer to prevent unintentional duplicate dependencies Dec 26, 2021
@nicolas-grekas
Copy link
Member

@nicolas-grekas
Copy link
Member

/cc @tgalopin could you please have a look?

@codedmonkey codedmonkey force-pushed the json-elevated branch 3 times, most recently from 023a72e to db23b8d Compare January 21, 2022 14:49
@nicolas-grekas
Copy link
Member

Thank you @codedmonkey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate package.json dependencies

4 participants