Pub: "pub get" consistently fails on Windows

Created on 6 Feb 2018  Â·  57Comments  Â·  Source: dart-lang/pub

I am working on a Flutter app on Windows and everything used to work fine. However, today I started to see the following error consistently whenever I run pub get (even after a fresh restart):

Failed to rename directory because access was denied.
This may be caused by a virus scanner or having a file
in the directory open in another application.

Yes, I am running a virus scanner on my machine (Windows Defender). I am running on a corporate machine and I don't have the proper rights to turn the scanner off, though.

What do I do now? How do I get past this error to get my Flutter/Dart environment running again on my Windows machine?

Most helpful comment

Getting Same error while saving and upgrading pubspec.yaml file.
Error:
Failed to rename directory because access was denied.
This may be caused by a virus scanner or having a file
in the directory open in another application.
Running "flutter pub get" in ...
pub get failed (1)

Solution:
Just Disable Antivirus which installed in your system for 15mins and save & get packages. This helped me to resolve this issue.

All 57 comments

There's unfortunately not a lot we can do about this in pub. We surface this error because the Windows system call to move the file fails, and only after retrying the call several times in case it's a transient issue.

Note that the issue may well not be a virus scanner, but something else entirely that's taking out a lock on your temp files or your .pub-cache directory. Maybe you have some sort of zombie pub process that you need to kill?

This also happens after a fresh restart of the system, so any zombie pub processes are unlikely.

Does pub have to move the files? Could it copy the files to a new location and then attempt to delete the old version? If that fails, it could just print a warning and then continues on with its work. That way, I would end up with a working environment - with possibly some junk left on my hard disk, which I could delete manually (or if at all possible the junk could be left behind in the system's temp folder and be cleaned up by the OS at an appropriate time).

Or is copying of the locked files also disallowed?

This is for moving a new package in place in the cache: the case where it's downloaded a new package and wants to move the old one out of the way to replace it, and can't rename the old one, right?

It seems to me that an rsync-like process might work where it copies files that have changed, creates directories that have been created, and removes directories that have been removed, and where it was blocked by the system from deleting a file or empty directory, that could be added to a queue, and retried a few times after all the successful operations have finished. This would make it so that any virus scanner or indexer would have a chance to be done with the file so it can be deleted.

Right now, it has to wait until the scanner is done with the entire directory in order to succeed. Of course, failure (during the rsync) could leave you in a broken state, but there are probably ways to fix that (make a copy of the destination before syncing, for instance).

This basically makes pub (and flutter) unusable for any machine with a virus scanner, which, as this is Windows, is a pretty common case. Any chance there are some other solutions we can try? For instance, you can at least tell the user what process/executable is holding the lock, using something like this?

Surely we're not the first to run into locking problems on Windows. There must be a better solution than "turn off your virus scanner, reboot, and cross your fingers". Many corporate users don't have the option to turn off their virus scanners (although rebooting and crossing fingers is usually still allowed).

Does pub have to move the files? Could it copy the files to a new location and then attempt to delete the old version?

It's important that we do the move atomically. Otherwise, if we encounter transient OS issues like this, the cache can end up in an inconsistent state that will at best force the user to run pub cache repair and at worst break pub in unexpected ways.

This basically makes pub (and flutter) unusable for any machine with a virus scanner, which, as this is Windows, is a pretty common case.

It seems to be extremely rare that users run into this in practice. This is the first report I've seen of it since we started retrying on Windows when the move failed, which is why I think something unusual is going on in @goderbauer's system. I wouldn't expect a normal virus scanner to hold a lock long enough for this to fail so consistently.

For instance, you can at least tell the user what process/executable is holding the lock, using something like this?

If dart:io provided access to this information, we could definitely surface it.

It's important that we do the move atomically. Otherwise, if we encounter transient OS issues like this, the cache can end up in an inconsistent state that will at best force the user to run pub cache repair and at worst break pub in unexpected ways.

Can you explain this? How is a copy in this case less atomic than a move? Currently, pub also leaves trash around in the temp directory if it can't do the move. Wouldn't a copy followed by a (possibly failing) delete operation just run the same risk of inconstant state?

which is why I think something unusual is going on in @goderbauer's system.

What can this "something unusual" be? Happy to provide more information about my machine.

Can you explain this? How is a copy in this case less atomic than a move?

Copying is implemented by the OS by individually copying each component file of a directory, while moving is atomic at the filesystem level. If a move fails, there's guaranteed to be nothing in the target location, which isn't the case for a copy.

Currently, pub also leaves trash around in the temp directory if it can't do the move. Wouldn't a copy followed by a (possibly failing) delete operation just run the same risk of inconstant state?

Leaving junk in the temp directory isn't a big deal, since future pub processes won't read from there. What we really care about is leaving junk in the pub cache. For example, if we create a directory there that looks like a package but is missing some of its files, users' programs will fail and even pub cache repair might not notice that it's corrupted.

What can this "something unusual" be? Happy to provide more information about my machine.

I'm not enough of a Windows expert to venture a guess. I think your best bet is running one of the diagnostic tools @gspencergoog linked to to see exactly what process is holding the lock.

FWIW, we also have a report that antivirus on Windows causes problems here: https://github.com/flutter/flutter/issues/12341
We consider that bug a high priority for our current milestone.

Hi @nex3 and interested Dartisan friends.

Can you schedule a VC with the Flutter folks and see if we can resolve this together?

I've re-opened the issue. /cc @kevmoo @mit-mit

If there are any concrete proposals for addressing this while preserving atomicity, I'm happy to work on them. But as far as I can tell, this is essentially an operating system issue, and I don't see a way to work around it in pub beyond what we're already doing.

Have you investigated what other packages managers do on Windows to get around this?

while preserving atomicity

What if we look for a pattern that doesn't require atomicity? For instance we could drop a file in each package that says "this directory is complete" or keep a manifest somewhere. As long as we don't write that file until the non-atomic copy is complete, and don't reference incomplete packages in .packages files, we should be OK.

It might be challenging, though, to upgrade from a pub cache without this marker to one that has it...

For anyone able to repro this with a windows machine:

You should be able to use process explorer and the "Handle" menu to track down the process that is holding on to the file handle.

@nex3 - do we currently print out the path of the directory that caused the issue? If not, can we prioritize adding that?

@natebosch The directory with the lock is printed out. I was not able to catch which process holds on to the lock with process explorer. Seems like process explorer takes a few seconds to find all handles and by that time the lock was released already.

Having said that, I am almost certain that the lock is held by Windows Defender, the build-in virus scanner. If I move my pub cache to a directory excluded by the scanner I can't reproduce the failure anymore.

If the handle is released within a few seconds, maybe the solution is to increase the time between retries.

I thought at first the issue might have been with 'git' and github as i received a notice once I initialized my project directory, but didn't write down the notice.

In any rate, this error was encountered in AndroidStudio. So I closed down Android studio, and manually ran the "flutter package get" command in an elevated command prompt once I was in the project directory and then everything was resolved.
It does suck because some of us use this in an enterprise and cannot work around virus scanners for our dev environment.. Talk to our IT departments :) I know all too well as I did helpdesk for 2 years.

Just wanted to add some info for tracking this down.

My fail (can't rename dir) is always happening when processing sass 1.18.0. If I put in a dependency override to 1.17.3 it all works. Maybe that zip file has a bit pattern that matches a known virus.

Downloading sass 1.18.0 on another machine and copying it into pub.dartlang.org is my current workaround.

Even for the flutter web when I run the command "flutter pub global activate webdev'' got the following error-
Failed to rename directory because access was denied.
This may be caused by a virus scanner or having a file
in the directory open in another application.
pub failed (1)

Failed to rename directory because access was denied.
This may be caused by a virus scanner or having a file
in the directory open in another application.

The solution I found for this problem was to delete the cache folder that is in the flutter/bin/cache directory, and reinstall dart-sdk.

what worked for me is to delete .dart_tool

Where is the directory in question? Where is the pub cache located on windows?

Where is the directory in question? Where is the pub cache located on windows?

%APPDATA%\Pub\Cache\, see:

https://github.com/dart-lang/pub/blob/df0f72daaa724e29ed6075e0fb5549a6d6dc5daf/lib/src/system_cache.dart#L38-L39

image
Run your compiler this way

I also frequently encountered the following error while running

flutter pub get (or upgrade)
Failed to rename directory because access was denied.

This problem kept occurring also after a completely fresh install and after deleting the 'Pub' folder under AppData.

But what I noticed is that the error did NOT occur anymore after I disconnected from my 'Pulse secure' session !

Failed to rename directory because access was denied.

This could also be some I/O issue on the machine.

Capture
https://flutter.dev/docs/get-started/install/windows
Installation bundle of the latest stable release of the Flutter SDK
flutter_windows_v1.9.1+hotfix.4-stable.zip helped me resolve this issue.

Used latest flutter (hotfix.5), still face the same issue.

flutter doctor -v
[√] Flutter (Channel stable, v1.9.1+hotfix.5, on Microsoft Windows [Version 10.0.17134.1069], locale en-IN)
    • Flutter version 1.9.1+hotfix.5 at C:\skm\engine\flutter
    • Framework revision 1aedbb1835 (26 hours ago), 2019-10-17 08:37:27 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\shrkamat\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 40.2.2
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] IntelliJ IDEA Community Edition (version 2018.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4
    • Flutter plugin version 33.3.2
    • Dart plugin version 183.5901

[!] VS Code (version 1.33.0)
    • VS Code at C:\Users\shrkamat\AppData\Local\Programs\Microsoft VS Code
    X Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

! Doctor found issues in 1 category.

I had the same issue. Following workaround did the trick for me:
Remove existing flutter folder from C:/ directory. Download relevant version of flutter from their official site and extract it back to C:/ directory. Run flutter pub get again in your flutter app directory

Getting Same error while saving and upgrading pubspec.yaml file.
Error:
Failed to rename directory because access was denied.
This may be caused by a virus scanner or having a file
in the directory open in another application.
Running "flutter pub get" in ...
pub get failed (1)

Solution:
Just Disable Antivirus which installed in your system for 15mins and save & get packages. This helped me to resolve this issue.

I had the same error.
Restarting Windows sometimes unlocks the file, but not always.
I was able to run the pub get immediately after a reboot, using Command Prompt.

if you have avast just uninstall it .

Here is what I found, this file C:\flutter\bin\cache\dart-sdk\lib\_internal\pub\asset\7zip\7za.exe was actually duplicated in the folder by AppX deployment service in windows 10, I deleted the folder C:\flutter\bin\cache\dart-sdk\lib\_internal\pub\asset\7zip\ and replaced it with a fresh copy rather than deleting the whole of C:\flutter\bin\cache\ and it worked.

I had the same issue and just repeated the installation, everything was working then.
Virus Scanners I never did switch off and I doubt that it has to do with it in my case.

I just resolved this issue on my system, here's how. I closed down the AppX deployment service with Task Manager, I deleted the cache folder at bincache. Using winrar I extracted only the cache folder from the flutter zip file downloaded during setup and replaced the deleted cache folder. I called "flutter clean" just to be safe, and then called "flutter pub get" and it worked fine.

I solved it by re-cloning flutter.git and changing the environment variable to the new path and relocating the SDK path.
I found only this way easier to get away from the infinite loop.

I solved it by deleting the build folder and .dart_tool folders. And then ran pub get again. This worked for me.

pub cache repair did the trick for me

Hey guys, I ran into the same error, and here's what I did to fix it.

run chmod 777 /c/path/flutter/bin/cache/dart-sdk/bin in git bash
and try running pub get in git bash as an administrator.
It worked for me.

Similarly we can try flutter doctor as well after running the above command

One more solution: in case you can't disable antivirus, you can run pub cache repair, it will print packages that can't be moved, then go to flutter\.pub-cache\_temp and rename every dir*** to name from "repair" output (use pubspec file inside each directory to get right one). After this, move these directories to flutter\.pub-cache\hosted\pub.dartlang.org and run flutter pub get again. Works for me.

We have windows machine in my company with the same issue. We also have mcAfee installed on these machines.
flutter pub get fails every time on my machine and the computer of my colleague.
BUT if we run flutter pub get --verbose ... it works on both our machines. Why ? I have no clue.

We have windows machine in my company with the same issue. We also have mcAfee installed on these machines.
flutter pub get fails every time on my machine and the computer of my colleague.
BUT if we run flutter pub get --verbose ... it works on both our machines. Why ? I have no clue.

Same problem with me. flutter pub get --verbose works fine and after that even flutter bub get works fine.

while preserving atomicity

What if we look for a pattern that doesn't require atomicity? For instance we could drop a file in each package that says "this directory is complete" or keep a manifest somewhere. As long as we don't write that file until the non-atomic copy is complete, and don't reference incomplete packages in .packages files, we should be OK.

It might be challenging, though, to upgrade from a pub cache without this marker to one that has it...

pub cache repair

whenever I am trying to run pub cache repair it is showing like this-"pub : The term 'pub' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1

  • pub cache repair
  • ~~~

    • CategoryInfo : ObjectNotFound: (pub:String) [], CommandNotFoundException

    • FullyQualifiedErrorId : CommandNotFoundException"

I am working on a Flutter app on Windows and everything used to work fine. However, today I started to see the following error consistently whenever I run pub get (even after a fresh restart):

Failed to rename directory because access was denied.
This may be caused by a virus scanner or having a file
in the directory open in another application.

Yes, I am running a virus scanner on my machine (Windows Defender). I am running on a corporate machine and I don't have the proper rights to turn the scanner off, though.

What do I do now? How do I get past this error to get my Flutter/Dart environment running again on my Windows machine?

Hello dear,

There is a solution, as it works for me. just reboot/restart you system. and open your android studio once again and then you will see the message at right side of your bottom for pub get command just click on there and everything will be works fine.

please note: don't open any directory as I have just opened my android studio instantly after rebooting, and just got that message at right side of bottom for pub get.

Hope this will helps you.

Other solutions didn't work for me, but deleting pubspec.lock solved it.

Thank you problem is solved

On Thu, May 6, 2021, 1:10 PM Alenros @.*> wrote:

Other solutions didn't work for me, but deleting pubspec.lock solved it.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/pub/issues/1798#issuecomment-833304724, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AOPWOTUQZAXUZ5AXFHNEMUDTMJBYHANCNFSM4EPJGY7Q
.

replace your flutter folder with a new flutter folder at the same spot .I reinstalled flutter and my issue was solved.

Thank you issue is fixed

On Thu, May 6, 2021, 1:10 PM Alenros @.*> wrote:

Other solutions didn't work for me, but deleting pubspec.lock solved it.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/pub/issues/1798#issuecomment-833304724, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AOPWOTUQZAXUZ5AXFHNEMUDTMJBYHANCNFSM4EPJGY7Q
.

I'm still facing it. I tried reinstalling the flutter, recreating the project, etc. I am still facing it. Unfortunately, I can not uninstall/disable antivirus as my org policy doesn't allow me. At this point, no clue what to try next.

I'm still facing it. I tried reinstalling the flutter, recreating the project, etc. I am still facing it. Unfortunately, I can not uninstall/disable antivirus as my org policy doesn't allow me. At this point, no clue what to try next.

I found this as a solution

When there was a dead-end, I found this as a solution. Nothing else worked for me so far. This one just worked like a charm. Such a relief!

https://stackoverflow.com/a/60717742/1127192

I've provided the allow permission for both bin and .pub-cache folder. This screenshot may be useful,

image

Hi All,

I had similar issue while using shared_preferences 2.0.5 in dependency. Followed below steps and it got fixed.

  • Downloaded the package from https://pub.dev/packages/shared_preferences/versions
  • Extracted the folder and pasted in C:<>flutter_windows_2.2.0-stableflutterpackages
  • Updated .packages file in my flutter project with shared_preferences:file:///C:<>/flutter_windows_2.2.0-stable/flutter/packages/shared_preferences-2.0.5/lib/
  • run the command again flutter pub get

It worked for me

I am facing the same issue. I installed flutter (and dart) yesterday using the github method (pull the repository) according to https://flutter.dev/docs/get-started/install/windows:

If you don’t want to install a fixed version of the installation bundle, you can skip steps 1 and 2. Instead, get the source code from the Flutter repo on GitHub, and change branches or tags as needed. For example:

C:\src>git clone https://github.com/flutter/flutter.git -b stable

You are now ready to run Flutter commands in the Flutter Console.

Today I am following the steps on dart: The Dart command-line tool. I just tried to create a test app using this command :

dart create -t console-full my_app

And this generated the application folder, but ended with the following error message:

Pub failed to delete entry because it was in use by another process.
This may be caused by a virus scanner or having a file
in the directory open in another application.

If I access the created "my_app" folder and run

dart pub get

I get the same error. I followed some of the proposed solutions, setting permissions for the folder holding the flutter and dart sdk/installation, removing the temporary folder where dart stores cache:

C:\Users\me\AppData\Local\Pub

Even disabling my antivirus software, adding exceptions to not scan the flutter/dart installation folder, but still no success.

I ran the following command with the verbose option enabled:

dart pub get -v

And this is the last part where it shows the error (i traced every place where the path with the issue was reported):

IO  : Get package from https://pub.dartlang.org/packages/test/versions/1.17.4.tar.gz.
MSG : Downloading test 1.17.4...
IO  : Created temp directory C:\Users\me\AppData\Local\Temp\pub_352e8b5a
IO  : HTTP GET https://pub.dartlang.org/packages/test/versions/1.17.4.tar.gz
...
IO  : Creating C:\Users\me\AppData\Local\Temp\pub_352e8b5a\test-1.17.4.tar.gz from stream.
FINE: Created C:\Users\me\AppData\Local\Temp\pub_352e8b5a\test-1.17.4.tar.gz from stream.
IO  : Created temp directory C:\Users\me\AppData\Local\Pub\Cache\_temp\dird2d90569
IO  : Reading binary file C:\Users\me\AppData\Local\Temp\pub_352e8b5a\test-1.17.4.tar.gz.
...
IO  : Creating C:\Users\me\AppData\Local\Pub\Cache\_temp\dird2d90569\doc\json_reporter.schema.json from stream.
FINE: Created C:\Users\me\AppData\Local\Pub\Cache\_temp\dird2d90569\doc\json_reporter.schema.json from stream.
FINE: Creating C:\Users\me\AppData\Local\Pub\Cache\_temp\dird2d90569\doc\package_config.md pointing to configuration.md
IO  : Deleting directory C:\Users\me\AppData\Local\Temp\pub_352e8b5a.
IO  : Pub failed to delete entry because it was in use by another process. Retrying in 50ms.
IO  : Deleting directory C:\Users\me\AppData\Local\Temp\pub_352e8b5a.
IO  : Pub failed to delete entry because it was in use by another process. Retrying in 50ms.
IO  : Deleting directory C:\Users\me\AppData\Local\Temp\pub_352e8b5a.
ERR : Pub failed to delete entry because it was in use by another process.
    | This may be caused by a virus scanner or having a file
    | in the directory open in another application.
FINE: Exception type: ApplicationException
FINE: package:pub/src/utils.dart 511:5                                                  fail
    | package:pub/src/io.dart 402:5                                                     _attempt
    | package:pub/src/io.dart 412:3                                                     deleteEntry
    | package:pub/src/io.dart 801:5                                                     withTempDir
    | ===== asynchronous gap ===========================
    | dart:async                                                                        Future.catchError
    | package:pub/src/utils.dart 112:52                                                 captureErrors.wrappedCallback
    | package:stack_trace                                                               Chain.capture
    | package:pub/src/utils.dart 125:11                                                 captureErrors
    | package:pub/src/command.dart 168:13                                               PubCommand.run
    | package:args/command_runner.dart 196:27                                           CommandRunner.runCommand
    | package:dartdev/dartdev.dart 200:30                                               DartdevRunner.runCommand
    | package:args/command_runner.dart 112:25                                           CommandRunner.run.<fn>
    | dart:async                                                                        new Future.sync
    | package:args/command_runner.dart 112:14                                           CommandRunner.run
    | package:dartdev/dartdev.dart 54:29                                                runDartdev
    | C:\b\s\w\ir\cache\builder\src\third_party\dart\pkg\dartdev\bin\dartdev.dart 11:9  main
---- End log transcript ----

So basically it created the temporary folder C:\Users\me\AppData\Local\Temp\pub_352e8b5a and now it is unable to delete it. Please notice this folder is outside of any dart/flutter related folder.

For those having this issue on Windows, an alternative would be using Docker. Run this command from the root directory of your Dart Project:

docker run -it --rm --name dartvm -v "%CD%":/app -w /app dart:stable /bin/bash

From there, just run the needed dart commands as needed:

dart pub get
dart analyze
dart test
dart run bin/my_app.dart

guys - quick fix - worked for me - delete cache file in flutter/bin/cache, then use powershell as admin, switch to flutter dev channel and flutter upgrade - flutter will upgrade and also download the latest dart version that's compatible.

guys - quick fix - worked for me - delete cache file in flutter/bin/cache, then use powershell as admin, switch to flutter dev channel and flutter upgrade - flutter will upgrade and also download the latest dart version that's compatible.

Thanks a lot @AkhilSharma90 it worked for me!

I had the same issue and running Vscode as administrator solved my problem.

Since I had the setup, I deleted the Flutter SDK, disabled my antivirus, and reinstalled it but this time, in a different directory like G:/flutter/bin.
It might have to do with some permission issues. But worked for me anyways.

Was this page helpful?
0 / 5 - 0 ratings