@@ -75,12 +75,12 @@ class _Loader {
7575 _checkBuildActions ();
7676
7777 _logger.info ('Initializing inputs' );
78+
79+ var assetGraph = await _tryReadCachedAssetGraph ();
7880 var inputSources = await _findInputSources ();
7981 var cacheDirSources = await _findCacheDirSources ();
8082 var internalSources = await _findInternalSources ();
8183
82- var assetGraph = await _tryReadCachedAssetGraph ();
83-
8484 BuildScriptUpdates buildScriptUpdates;
8585 if (assetGraph != null ) {
8686 var updates = await logTimedAsync (
@@ -94,7 +94,8 @@ class _Loader {
9494 if (! _options.skipBuildScriptCheck &&
9595 buildScriptUpdates.hasBeenUpdated (updates.keys.toSet ())) {
9696 _logger.warning ('Invalidating asset graph due to build script update' );
97- await _deleteGeneratedDir ();
97+ var deletedSourceOutputs = await _cleanupOldOutputs (assetGraph);
98+ inputSources.removeAll (deletedSourceOutputs);
9899 assetGraph = null ;
99100 buildScriptUpdates = null ;
100101 }
@@ -191,10 +192,10 @@ class _Loader {
191192 cachedGraph.buildActionsDigest) {
192193 _logger.warning (
193194 'Throwing away cached asset graph because the build actions have '
194- 'changed. This could happen as a result of adding a new '
195- 'dependency, or if you are using a build script which changes '
196- 'the build structure based on command line flags or other '
197- 'configuration.' );
195+ 'changed. This most commonly would happen as a result of adding a '
196+ 'new dependency or updating your dependencies.' );
197+
198+ await _cleanupOldOutputs (cachedGraph );
198199 return null ;
199200 }
200201 return cachedGraph;
@@ -209,6 +210,26 @@ class _Loader {
209210 });
210211 }
211212
213+ /// Deletes all the old outputs from [graph] that were written to the source
214+ /// tree, and deletes the entire generated directory.
215+ Future <Iterable <AssetId >> _cleanupOldOutputs (AssetGraph graph) async {
216+ var deletedSources = < AssetId > [];
217+ await logTimedAsync (_logger, 'Cleaning up outputs from previous builds.' ,
218+ () async {
219+ // Delete all the non-hidden outputs.
220+ await Future .wait (graph.outputs.map ((id) {
221+ var node = graph.get (id) as GeneratedAssetNode ;
222+ if (node.wasOutput && ! node.isHidden) {
223+ deletedSources.add (id);
224+ return _environment.writer.delete (id);
225+ }
226+ }).where ((v) => v is Future ));
227+
228+ await _deleteGeneratedDir ();
229+ });
230+ return deletedSources;
231+ }
232+
212233 /// Updates [assetGraph] based on a the new view of the world.
213234 ///
214235 /// Once done, this returns a map of [AssetId] to [ChangeType] for all the
0 commit comments