X Tutup
The Wayback Machine - https://web.archive.org/web/20220729200449/https://github.com/flutter/flutter/issues/53833
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter-specific pub cache causes issues #53833

Closed
tvolkert opened this issue Apr 2, 2020 · 13 comments · Fixed by #105343
Closed

Flutter-specific pub cache causes issues #53833

tvolkert opened this issue Apr 2, 2020 · 13 comments · Fixed by #105343
Assignees
Labels
a: first hour ask: dart customer: product new feature P3 tool

Comments

@tvolkert
Copy link
Contributor

@tvolkert tvolkert commented Apr 2, 2020

Our package archives pre-caches packages in $FLUTTER_ROOT/.pub-cache, whereas normal Pub behavior is to use $HOME/.pub-cache. This causes a number of issues:

  1. If a developer has multiple Flutter SDKs installed (e.g. master + stable), they end up with two duplicated caches, wasting disk space
  2. If a developer has both a Flutter and Dart SDK installed, again they have duplicated caches
  3. General confusion over where the pub cache is located
  4. It causes package versioning issues when switching between SDKs or SDK versions. Often causes hard-to-debug issues with pub global activate packages with commands available from $PATH

The existence of and support for $FLUTTER_ROOT/.pub-cache supports the "offline" Flutter tool usage (e.g. flutter create --offline) as well as lowering the number of "post-installation" downloads the user has to suffer. But given that we don't advertise the --offline flag very well, many users aren't gaining its benefit. This leaves us with all the pain of the solution and little of the gain.

Two things we could do to improve this:

  1. Make the Flutter tool relocate $FLUTTER_ROOT/.pub-cache to $HOME/.pub-cache when it detects its existence.
  2. Nudge users towards --offline if their command fails due to lack of connectivity.
@tvolkert tvolkert added tool a: first hour customer: product ask: dart labels Apr 2, 2020
@zanderso zanderso added the new feature label Apr 2, 2020
@zanderso zanderso added this to Awaiting triage in Tools - Dart and pub review via automation Apr 2, 2020
@zanderso
Copy link
Member

@zanderso zanderso commented Apr 2, 2020

Related issues:
#15717
#22429
#29364
#33672

@zanderso zanderso moved this from Awaiting triage to Engineer reviewed in Tools - Dart and pub review Apr 2, 2020
@jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Apr 2, 2020

Some more context: I'm not sure that we're getting a lot of mileage out of the bundled pub cache. This will only help developers with an offline mode until they add their first plugin, then they need to pull dependencies for Gradle/Cocoapods too

@zanderso
Copy link
Member

@zanderso zanderso commented Apr 2, 2020

I would like to retain the property that the SDK bundles contain most or all of what needs to be downloaded from Flutter/Dart to get started. As noted elsewhere, there was a measurable uptick in retention concomitant with that change. This implies that the first time the tool runs from an SDK bundle, it will need to do some surgery to migrate to $HOME/.pub-cache. That process will need good test coverage, which could be the more difficult part of the change.

@jonasfj
Copy link
Member

@jonasfj jonasfj commented Apr 14, 2020

I would like to retain the property that the SDK bundles contain most or all of what needs to be downloaded from Flutter/Dart to get started.

The safest option is for you to create a tarball containing a pre-populated $PUB_CACHE and ONLY copy it into place if there is no $PUB_CACHE already. You can find the default path for $PUB_CACHE using:

https://github.com/dart-lang/pub/blob/3606265962da4248d34d352aa3d170aae4496a90/lib/src/system_cache.dart#L34-L53

Beware that there is special tricks on place on Windows.

You can also extract specific package tarballs into $PUB_CACHE/hosted/pub.dartlang.org/<package>-<version>/. But I can't promise that the layout of the $PUB_CACHE won't change in the future -- so if you do this I would recommend writing tests that covers this.

@jonahwilliams jonahwilliams self-assigned this May 13, 2020
@jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented May 13, 2020

One case where this hydration strategy worsens the situation is when a user had previously tried Flutter or Dart and has a pub cache as a result. I don't have an idea of how common this would be, but we could avoid making the situation worse if there were a way to populate an existing cache.

@jonasfj though I don't think the cache structure of pub is necessarily a public API, we do ship the flutter tool in lockstep with pub. Would it be reasonable for the tool to make some assumptions here and still preserve safety?

@jonasfj
Copy link
Member

@jonasfj jonasfj commented Jun 8, 2020

but we could avoid making the situation worse if there were a way to populate an existing cache.

I would suggest not populating the cache if there is already one present. In this case it's likely many of the packages are already there :D
If we really do want to do this, it wouldn't be harmful, so long as we just extract tarballs into $PUB_CACHE/hosted/pub.dartlang.org/<package>-<version>/ (assuming said directory doesn't exist.

Would it be reasonable for the tool to make some assumptions here and still preserve safety?

We can probably promise that if the PUB_CACHE directory was created by a previous version of pub it will work with a future version of pub (or at-least not cause issues for a future version of pub).

Hence, if you only create the cache when no $PUB_CACHE folder exists, and follow the pattern that PUB_CACHE currently has, then this should always work. Even if a future version of pub decides to change the cache-layout, such newer version of pub will just think that the PUB_CACHE was created by a previous version of pub.

@tvolkert tvolkert added the P4 label Jul 29, 2020
@jacob314
Copy link
Contributor

@jacob314 jacob314 commented Apr 29, 2022

This is likely contributing to Dart analyzer performance issues for mono-repo projects where pub get was run differently for different packages in the mono_repo.

@jacob314 jacob314 added P3 and removed P4 labels Apr 29, 2022
@mit-mit
Copy link
Member

@mit-mit mit-mit commented May 2, 2022

Can you elaborate, @jacob314 ?

@jacob314
Copy link
Contributor

@jacob314 jacob314 commented May 2, 2022

TLDR: The analyzer relies on absolute file system paths to determine whether two versions of a package are the same. If your mono-repo has two pub cache directories, 2X the memory and CPU is used for resolving packages that are used from both pub cache directories.

If you have a mono-repo with some Dart packages (for example: devtools_shared) and some Flutter packages (for examples: devtools_app) and you happen to run
dart pub get from your Dart package and flutter pub get from your Flutter package then you will have the Flutter specific pub cache directory for one package and the Dart one for your other package. If you next open the parent directory of devtools_app and devtools_shared in an IDE, the Analyzer will be unable to tell that the shared dependencies of the two packages are actually shared because they are in different directories. This doubles the memory usage from the analyzer loading and resolving the shared packages and doubles the time spent resolving the shared packages.

@Jasguerrero
Copy link
Contributor

@Jasguerrero Jasguerrero commented May 12, 2022

After having a discussion with @jacob314. @christopherfujino and I believe the best approach to handle this case is to copy your pub cache located at $FLUTTER_ROOT/.pub-cache over to $HOME/.pub-cache only when $HOME/.pub-cache is not found and then proceed to delete the $FLUTTER_ROOT/.pub-cache directory. The question still remains in what part of the process should be more adequate to add the change. Current idea is when doing a flutter pub get but only the first time a user downloads Flutter.

@mit-mit
Copy link
Member

@mit-mit mit-mit commented May 13, 2022

Yes, I think something like that makes sense; WDYT @jonasfj ?

@jonasfj
Copy link
Member

@jonasfj jonasfj commented May 19, 2022

WDYT @jonasfj ?

Love it!

Not having two PUB_CACHE locations will make the world simpler.

@Jasguerrero
Copy link
Contributor

@Jasguerrero Jasguerrero commented Jun 2, 2022

breadcrumbs: before implementing this, further discussion is needed to find a good strategy for testing the migration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: first hour ask: dart customer: product new feature P3 tool
Projects
Tools - Dart and pub review
  
Engineer reviewed
Development

Successfully merging a pull request may close this issue.

7 participants
X Tutup