pub icon indicating copy to clipboard operation
pub copied to clipboard

Add a way to have `pub` remove unused dependencies from `pubspec.yaml`

Open gspencergoog opened this issue 2 years ago • 10 comments

There are commands for add and remove that manipulate the pubspec.yaml. Could we maybe add a flag to the remove command to only remove unused packages?

I'm not sure how complicated this is to implement, but surely the analyzer can tell if a symbol is referenced and remove a package from the pubspec.yaml if there are no references to any of its symbols in the package that owns the pubspec.yaml.

The use case for this is that we would like to be able to generate a project for an "open in VSCode" link in Dartpad, but would like to just create a pubspec that only includes those packages that are actually in use, starting from one that is a superset of the packages in use. (see https://github.com/dart-lang/dart-pad/issues/2391).

gspencergoog avatar Oct 31 '22 17:10 gspencergoog

Cool idea.

I guess all we need is to look at the transitive closure of the library import graph... (ie. we don't actually need to do resolution and look at symbols)...

sigurdm avatar Nov 03 '22 08:11 sigurdm

We have to consider dependencies that are used only for assets.

We have to make sure flutter code generation has been done (otherwise federated plugins would break).

sigurdm avatar Nov 03 '22 10:11 sigurdm

Maybe this would be better implemented as a dart fix suggestion...

sigurdm avatar Nov 03 '22 10:11 sigurdm

@gspencergoog I would like to work on it . Please provide me advices and details regarding as I am begineer in open-source

Abhi149209 avatar Nov 19 '22 14:11 Abhi149209

@gspencergoog @gspencergoog @sigurdm @walnutdust @nex3 @sethladd And what is the criteria to remove the unused dependency. Do you want a time frame after which the dependency must be reemoved or I should remove the dependency during building release-apk.

Abhi149209 avatar Nov 19 '22 14:11 Abhi149209

@devoncarew @sigurdm @gspencergoog @nex3 @sethladd @walnutdust hi sir, I am gonna work on this feature , please guide me on this feature, and guide me the flow of add and remove commands , because this will help me

Abhi149209 avatar Nov 20 '22 08:11 Abhi149209

This would be incredibly helpful while maintaining packages. Since I've started working on a package myself and have lately stumbled upon some others that once had a dependency and have never removed them from the dependencies in pubspec.yaml.

Could this code be reused while running pub get so that packages that have those pending dependencies don't constraint dependencies to an older version for example?

FMorschel avatar Apr 22 '24 16:04 FMorschel

Maybe this would be better implemented as a dart fix suggestion...

Could you elaborate more, please? Sorry for the tag, but I have a feeling you are not receiving notifications from this issue @sigurdm.

FMorschel avatar Apr 22 '24 16:04 FMorschel

I do receive notifications, but this issue has not been top of our priorities - sorry.

Not sure this is a good first task for a contributor who doesn't know where to start themselves, as it involves crosscuts between pub and the analyzer.

Maybe this would be better implemented as a dart fix suggestion... Could you elaborate more, please?

Yeah, instead of making a pub command dart pub prune-unused deps we could have the analyzer find and highlight dependencies in pubspec.yaml that are not currently used, and then have a quick-fix action to remove them.

@pq does that sound reasonable? Will the analyzer be able to do a full-package traversal of the (unresolved) import graph?

sigurdm avatar Apr 23 '24 08:04 sigurdm

I think this is reasonable. @keertip who recently added a fix that adds missing (explicit) pub dependencies could answer more decisively though. As she's on vacation, maybe @bwilkerson can chime in?

pq avatar Apr 24 '24 19:04 pq