yarn icon indicating copy to clipboard operation
yarn copied to clipboard

yarn install --flat - Automatic Choice

Open shellscape opened this issue 7 years ago • 30 comments

Do you want to request a feature or report a bug? feature

What is the current behavior? yarn install --flat asks the user which version of a module they would like, if there is a conflict.

Please mention your node.js, yarn and operating system version. node v6.8.1 npm v3.10.8 yarn v0.16.1

yarn install --flat is wonderful in that it asks the users which version of a dep they want to install. However, it would be immensely useful for automation environments, to allow for an option to direct yarn to automatically assume the newest version.

shellscape avatar Nov 03 '16 16:11 shellscape

If this is a feature that should be implemented, I can work on it.

polizz avatar Dec 19 '16 19:12 polizz

Composer does this really well. It actually always resolves it's own version, and gives you options: https://getcomposer.org/doc/04-schema.md#prefer-stable and https://getcomposer.org/doc/04-schema.md#minimum-stability

You can also set this on the CLI https://getcomposer.org/doc/03-cli.md#update

If Composer can't resolve a version, it doesn't have you choose, it just throws an exception and explains why there is no suitable version and forces you to resolve this in your project, or in a dependency. It also resolves the dependency versions every time you run update rather than storing this somewhere. I think this is a much better behavior than asking the user to resolve the dependencies.

davidbarratt avatar Jan 24 '17 21:01 davidbarratt

For reference: I put together a gulp plugin that examines an install tree prior to installing to look for any discrepancies (aka version conflicts) between modules in the entire tree. https://github.com/shellscape/gulp-version-conflicts is what I came up with and while it's quite quick, it's still another step before we get to running npm or yarn.

shellscape avatar Jan 25 '17 00:01 shellscape

@shellscape I was looking for this same option, but I only see it as being useful for the initial transition to yarn flat mode.

As I see it, automated environments should be using yarn.lock rather than trying to resolve dependencies nondeterministically. (Though perhaps I'm just not understanding your use case.)

FWIW, I did find the unexpected interactivity rather surprising. The ordering of the options also seems somewhat arbitrary. (I didn't look into where the order comes from though.)

It would be nice if at least option 1 was always the newest version (or best guess through some smarter heuristic?), then I could run yes 1 | yarn --flat as a reasonable starting point. (Though a dedicated option may be easier for more people.)

aij avatar Feb 14 '17 16:02 aij

I expected this to work like npm dedupe

davidbarratt avatar Feb 14 '17 23:02 davidbarratt

@davidbarratt No, flat mode will install a single version of each package (like bower AFAICT). npm dedupe only deduplicates non conflicting dependencies. (Which seems to only be for somewhat cleaning up after it's non-reproducibility?)

For example, with yarn --flat I get a single copy of babel-runtime. If instead I run npm install I get 59 copies. After running npm dedupe that is still 59 copies.

aij avatar Feb 15 '17 14:02 aij

@aij Ah yes. I agree. Let me rephrase, in my opinion it should resolve everything to a single version, and if it can't, it should throw an exception and explain the conflicts rather than making you decide.

davidbarratt avatar Feb 15 '17 14:02 davidbarratt

Hi, is there any changes for this?

beejei avatar Feb 24 '17 02:02 beejei

Not having this is quite vexing, I actually have to write a script that tails the output of yarn, feeding it to a state machine that can answer those questions automatically to get this feature : C

jaen avatar Mar 02 '17 09:03 jaen

Can there at least be a config option to auto-select the first/highest version in the specified range? Not having this is a major issue for my workflow.

aliatsis avatar Apr 28 '17 11:04 aliatsis

In case it's helpful to anyone, here's the temporary workaround I'm using until this is resolved:

read -r -d '' modules <<- EOM
	@angular/animations
	@angular/cli
	@angular/common
	...
	xkcd-passphrase
	zombie
	zone.js
EOM


cd ..
yarn add compare-versions
cd -

script -fc "
	while [ ! -f yarn.done ] ; do
		answer=\"\$(node -e 'console.log(
			(
				(
					fs.readFileSync(\"yarn.out\").
						toString().
						split(\"Unable to find a suitable version\")[1]
					|| \"\"
				).
					match(/resolved to \".*?\"/g)
				|| []
			).
				map((s, i) => ({index: i + 1, version: s.split(\"\\\"\")[1]})).
				reduce(
					(a, b) => require(\"compare-versions\")(
						a.version,
						b.version
					) === 1 ?
						a :
						b
					,
					{index: \"\", version: \"0\"}
				).index
		)')\"

		if [ \"\${answer}\" ] ; then
			echo > yarn.out
			echo \"\${answer}\"
		fi
	done | bash -c '
		yarn add \
			--flat \
			--ignore-engines \
			--ignore-platform \
			--ignore-scripts \
			--non-interactive \
			$(echo "${modules}" | tr '\n' ' ') \
		|| \
			touch yarn.failure

		touch yarn.done
	'
" yarn.out

if [ -f yarn.failure ] ; then
	exit 1
fi

rm -rf ../node_modules ../package.json ../yarn.lock yarn.failure yarn.out

As I see it, automated environments should be using yarn.lock rather than trying to resolve dependencies nondeterministically. (Though perhaps I'm just not understanding your use case.)

I can't speak to anyone else's use case, but in my case we have a script that generates and commits a new package.json and yarn.lock with the latest versions of a list of modules, after which point yarn.lock is used as you suggest.

buu700 avatar Jun 06 '17 02:06 buu700

Bash scares the hell out of me so I also made a script to automate this, but in javascript: https://gist.github.com/ds300/158250f230d1825af8a3edd6e7af9cc0

ds300 avatar Jun 28 '17 11:06 ds300

In Bower, it uses a config property named resolutions in the bower.json file to remember what version to use when there is a conflict:

  "resolutions": {
    "jquery": "1.12.4"
  }

That would tell it to always chose [email protected] when multiple versions could be selected. Yarn could follow that pattern.

rally25rs avatar Aug 09 '17 12:08 rally25rs

This is preventing adoption of yarn here. Yarn appears to require a tty with --flat even before it has discovered that there are actual conflicts (just in case it eventually encounters a conflict), and even when --non-interactive is also specified. A google search yielded a bunch of results that were ultimately a waste of time in diagnosing this limitation.

mrmachine avatar Nov 13 '17 00:11 mrmachine

This feature is necessary to run --flat in CI type environments

tomkel avatar Feb 12 '18 23:02 tomkel

For anyone who would like to try to implement this feature, it might be helpful to write up an RFC and post it over on https://github.com/yarnpkg/rfcs where there can be some discussion as to what the correct behavior should be. 👍

rally25rs avatar Feb 16 '18 23:02 rally25rs

For Enterprise Software this a must have feature. We currently also think about to switch back to NPM because this breaks our CI plans.

Is there already a RFC or a Plan how to do this ?

bys92 avatar Mar 21 '18 12:03 bys92

At least sort the damn choices by version, so I can just press first option 100 times and at least see if it's going to work or not. Currently wasting time trying to flatten polymer dependencies.

TheoXD avatar Apr 14 '18 04:04 TheoXD

Shouldn't this have the high-priority label ? This is in the top 10 issues if you sort by 👍

Mouvedia avatar Apr 14 '18 09:04 Mouvedia

please make yarn install --flat --json work!
And then there will be tons of package can resolve the automatic problem.

GongT avatar Dec 11 '18 09:12 GongT

Mind-bottling that this is still an open issue after two years.

shellscape avatar Dec 11 '18 13:12 shellscape

someone should bounty this on gitcoin.co

pmespresso avatar Feb 25 '19 08:02 pmespresso

+1 for this 🙏🏻

Did anybody find a solution to auto-install latest versions when running --flat?

damianobarbati avatar Mar 08 '19 14:03 damianobarbati

Me too, so annoying @@

davidtranjs avatar Mar 29 '19 04:03 davidtranjs

If this is a feature that should be implemented, I can work on it.

@polizz You still working on this?

Danzabar avatar Apr 03 '19 17:04 Danzabar

@polizz 🙏🏻

damianobarbati avatar Aug 24 '19 18:08 damianobarbati

I created a brand new sh*t on npm: @gongt/flat-yarn-install

GongT avatar Oct 11 '19 11:10 GongT

This also annoys me greatly. So I created https://github.com/DerekZiemba/yarn-V2-workspaces-simple-monorepo/blob/classic/build/yarn-install-flat.js

Should be able to drop that file in anywhere. It auto-installs dependencies globally such as yarn, semver, and node-pty`. So not only can it do --flat, but it'll initialize a brand new repo that doesn't have any node_modules installed yet.

There's a readme here https://github.com/DerekZiemba/yarn-V2-workspaces-simple-monorepo#automated

DerekZiemba avatar Aug 28 '20 23:08 DerekZiemba

any updates on this one?

Bec-k avatar Apr 07 '21 16:04 Bec-k

My takeaway from reading this issue. Defining behaviour is sensitive/complex; Needs to go through RFC pipe :+1:

But this proposal would actually help tremendously without a negative impact:

It would be nice if at least option 1 was always the newest version (or best guess through some smarter heuristic?), then I could run yes 1 | yarn --flat as a reasonable starting point. (Though a dedicated option may be easier for more people.)

Can I have this please? :)

telamon avatar Sep 22 '22 20:09 telamon