[WinError 183] Cannot create a file when that file already exists
Describe the bug
we have an issue to connect to azure cli on our paw since a few days. already tried to reinstall azure cli on windows and also tried to delete the .azure folder but still not working.
Related command
az login
Errors
The command failed with an unexpected error. Here is the traceback:
[WinError 183] Cannot create a file when that file already exists: 'C:\Users\username\.azure'
Traceback (most recent call last):
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 666, in execute
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 733, in _run_jobs_serially
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 703, in _run_job
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 336, in call
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py", line 121, in handler
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/profile/custom.py", line 173, in login
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/_profile.py", line 176, in login
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 167, in login_with_auth_code
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 126, in _msal_app
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 118, in _msal_public_app_kwargs
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 100, in _msal_app_kwargs
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 131, in _load_msal_token_cache
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/persistence.py", line 27, in load_persisted_token_cache
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/persistence.py", line 42, in build_persistence
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 204, in init
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 148, in init
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 40, in _mkdir_p
File "
Issue script & Debug output
cli.knack.cli: Command arguments: ['login', '--debug']
cli.knack.cli: init debug log:
Enable color in terminal.
cli.knack.cli: Event: Cli.PreExecute []
cli.knack.cli: Event: CommandParser.OnGlobalArgumentsCreate [<function CLILogging.on_global_arguments at 0x000002628423B2E0>, <function OutputProducer.on_global_arguments at 0x00000262845DC040>, <function CLIQuery.on_global_arguments at 0x0000026284601440>]
cli.knack.cli: Event: CommandInvoker.OnPreCommandTableCreate []
cli.azure.cli.core: Modules found from index for 'login': ['azure.cli.command_modules.profile']
cli.azure.cli.core: Loading command modules:
cli.azure.cli.core: Name Load Time Groups Commands
cli.azure.cli.core: profile 0.004 2 8
cli.azure.cli.core: Total (1) 0.004 2 8
cli.azure.cli.core: Loaded 2 groups, 8 commands.
cli.azure.cli.core: Found a match in the command table.
cli.azure.cli.core: Raw command : login
cli.azure.cli.core: Command table: login
cli.knack.cli: Event: CommandInvoker.OnPreCommandTableTruncate [<function AzCliLogging.init_command_file_logging at 0x0000026286889C60>]
cli.azure.cli.core.azlogging: metadata file logging enabled - writing logs to 'C:\Users\username.azure\commands\2025-03-19.16-45-25.login.4856.log'.
az_command_data_logger: command args: login --debug
cli.knack.cli: Event: CommandInvoker.OnPreArgumentLoad [<function register_global_subscription_argument.
cli.azure.cli.core.azclierror: The command failed with an unexpected error. Here is the traceback:
az_command_data_logger: The command failed with an unexpected error. Here is the traceback:
cli.azure.cli.core.azclierror: [WinError 183] Cannot create a file when that file already exists: 'C:\Users\username\.azure'
Traceback (most recent call last):
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 666, in execute
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 733, in _run_jobs_serially
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 703, in _run_job
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 336, in call
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py", line 121, in handler
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/profile/custom.py", line 173, in login
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/_profile.py", line 176, in login
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 167, in login_with_auth_code
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 126, in _msal_app
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 118, in _msal_public_app_kwargs
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 100, in _msal_app_kwargs
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/identity.py", line 131, in _load_msal_token_cache
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/persistence.py", line 27, in load_persisted_token_cache
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/auth/persistence.py", line 42, in build_persistence
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 204, in init
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 148, in init
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 40, in _mkdir_p
File "
Expected behavior
login window should appear
Environment Summary
azure cli 2.70
Additional context
No response
Thank you for opening this issue, we will look into it.
The call stack shows this line fails:
File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\msal_extensions/persistence.py", line 40, in _mkdir_p
File "", line 225, in makedirs
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\Users\username\.azure'
However this line does says exist_ok=True:
https://github.com/AzureAD/microsoft-authentication-extensions-for-python/blob/54b682bc9fc940a71ad36fd945f2d2ca428a55c1/msal_extensions/persistence.py#L40
os.makedirs(path, exist_ok=True)
os.makedirs (L225) also takes racing conditions into consideration:
https://github.com/python/cpython/blob/v3.12.9/Lib/os.py#L225
try:
mkdir(name, mode)
except OSError:
# Cannot rely on checking for EEXIST, since the operating system
# could give priority to other errors like EACCES or EROFS
if not exist_ok or not path.isdir(name):
raise
It doesn't seem to be possible for FileExistsError to be raised. Which package have you installed? Could you share the URL please?
tried several ways to install.
Initially it was installed using chocolately. But i removed and installed using MSI from the official github repo:
I tried also previous versions: 2.67.0: https://github.com/Azure/azure-cli/releases/download/azure-cli-2.67.0/azure-cli-2.67.0-x64.msi 2.70.0: https://github.com/Azure/azure-cli/releases/download/azure-cli-2.70.0/azure-cli-2.70.0-x64.msi
Also tried to install / update via winget but the error remains.
If i install 2.70.0 on my local client everything is working fine.
The issue appears only in a remote azure virtual desktop environment with FSLogix profile containers attached
I also am experiencing this same issue, just discovered this yesterday with an identical setup, azure virtual desktop with FSLogix profile container - tried installing 2.70.0 and 2.69.0 using both winget and msi installer (I wouldn't normally have commented, but wanted to identify that this wasn't a singular issue)
The only possibility is path.isdir(name) returns False.
Could you get the status of C:\Users\<username>\.azure with (Get-Item C:\Users\<username>\.azure).PSisContainer to see if that is a directory?
It is expected to be
> (Get-Item C:\Users\<username>\.azure).PSisContainer
True
https://stackoverflow.com/questions/39825440/check-if-a-path-is-a-folder-or-a-file-in-powershell
PS C:\Users\USER> (Get-Item C:\Users\USER.azure).PSisContainer True PS C:\Users\USER>
This command returns True as expected
Could you directly run python.exe and check the result? It is expected to look like
> & "C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe" -c "import os; print(os.makedirs(r'C:\Users\USER\.azure', exist_ok=True))"
None
> & "C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe" -c "import os; print(os.makedirs(r'C:\Users\USER\.azure', exist_ok=False))"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen os>", line 225, in makedirs
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\USER\\.azure'
> & "C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe" -c "import os; print(os.path.isdir(r'C:\Users\USER\.azure'))"
True
PS C:\Users\USER> & "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe" -c "import os; print(os.makedirs(r'C:\Users\USER\.azure', exist_ok=True))"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen os>", line 225, in makedirs
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\USER\\.azure'
Exact same output with exist_ok=False
But the last command returns False
PS C:\Users\USER> & "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe" -c "import os; print(os.path.isdir(r'C:\Users\USER\.azure'))"
False
Had the same problem after updating FSLogix to 3.25.202.4223 on Win 11 24H2 with March CU AVD MS Host. After downgrading to FSLogix 2.9.8884.27471, Azure CLI works normally
It seems C:\Users\USER\.azure is occupied by FSLogix and Python no longer sees it as a directory. I have reached out to FSLogix team (IcM 610534758).
For now, could you try setting AZURE_CONFIG_DIR to another location and see if you can get unblocked?
$env:AZURE_CONFIG_DIR='C:\Users\USER\.azure-cli'
See https://learn.microsoft.com/en-us/cli/azure/azure-cli-configuration#cli-configuration-file
I didn't set the AZURE_CONFIG_DIR, but I was able to work around the issue by creating a custom FSLogix redirection.xml file to exclude the .azure directory from the profile container (see https://learn.microsoft.com/en-us/fslogix/concepts-redirections-xml) I did initially have to delete the existing .azure directory for it to work.
I didn't set the AZURE_CONFIG_DIR, but I was able to work around the issue by creating a custom FSLogix redirection.xml file to exclude the .azure directory from the profile container (see https://learn.microsoft.com/en-us/fslogix/concepts-redirections-xml) I did initially have to delete the existing .azure directory for it to work.
I am having the same issue, I tried the following redirections.xml without success:
<?xml version="1.0" encoding="UTF-8"?>
<FrxProfileFolderRedirection ExcludeCommonFolders="0">
<Excludes>
<Exclude Copy="0">.azure</Exclude>
</Excludes>
<Includes>
</Includes>
</FrxProfileFolderRedirection>
@jhayes-acb any chance you could share your working redirections.xml?
I'm having similar issues to @christophecolly. Redirection is in place as documented with reg key via Intune policy and XML file stored on centrally accessible share.
I've asked our apps support to roll back the version of FSLogix to test functionality on the previous version.
Same issue for me. Azure CLI in AVD with FSLogix 25.02 and 25.04 doesn't work. After downgrade FSLogix to 2.9.8884.27471 Azure CLI starts working. Beside Azure CLI the application pgadmin throws a similar error but works after downgrade of FSLogix, too.
@denis-beuermann I have the same issue with pgadmin + avd + FsLogix
See my comment below how i worked around it using redirects. Don’t know if is the most efficient way, but it works.
https://github.com/pgadmin-org/pgadmin4/issues/8672#issuecomment-2904425250
For me the exclude rule create the folder .azure in local_%USERPROFILE% but it still writes to the original folder in %USERPROFILE%\.azure.
My workaround was using a custom PS profile on each AVD host:
Create Powershell default profile in C:\Program Files\PowerShell\7\profile.ps1 (Get the path using this $PROFILE.AllUsersAllHosts)
Added -> $env:AZURE_CONFIG_DIR = "C:\Users\$(Split-Path $env:USERPROFILE -Leaf)\.azure"
If there is a simpler way, let me know.
@gwynbleide: I tried your suggestion, but for me it only worked setting setting the $env:AZURE_CONFIG_DIR to a different path in the roaming profile folder: $env:AZURE_CONFIG_DIR = "C:\Users\local_$(Split-Path $env:USERPROFILE -Leaf)\.configazcli"
@jiasli: did you receive any update from the FSLogix team (IcM 610534758)?
According to FSLogix team, it has been identified to be an issue with FSLogix but there is currently no ETA for fixing it.
For now, please set AZURE_CONFIG_DIR environment variable as a workaround.
tried several ways to install.
Initially it was installed using chocolately. But i removed and installed using MSI from the official github repo:
I tried also previous versions: 2.67.0: https://github.com/Azure/azure-cli/releases/download/azure-cli-2.67.0/azure-cli-2.67.0-x64.msi 2.70.0: https://github.com/Azure/azure-cli/releases/download/azure-cli-2.70.0/azure-cli-2.70.0-x64.msi
Also tried to install / update via winget but the error remains.
If i install 2.70.0 on my local client everything is working fine.
The issue appears only in a remote azure virtual desktop environment with FSLogix profile containers attached
I too am seeing the same issue. I hope it gets fixed soon.
Interesting this same error occurs with a python script in QGIS when also using FSLogix https://github.com/qgis/QGIS/issues/61687
I opened a ticket with MS who admitted the Fslogix version 3.25.626.21064 has problems which will be fixed in the near future. Although I didn't get a permanent fix or an ETA for the release of the same, I was given the follow workaround:
Change the value of SupportedFeatures to b (Hexadecimal) / 11 (Decimal) for all 3 drivers and reboot the system: HKLM\SYSTEM\CurrentControlSet\Services\frxdrvvt\SupportedFeatures HKLM\SYSTEM\CurrentControlSet\Services\frxdrv\SupportedFeatures HKLM\SYSTEM\CurrentControlSet\Services\frxccd\SupportedFeatures
@tcostantini Thanks for that, confirmed those registry keys and a reboot did fix the issue on FSLogix 25.04 (Version: 3.25.401.15305)
For me version FSLogix 25.09 (Version: 3.25.822.19044) fixed the issue. No need to apply any further workarounds.
Thanks @denis-beuermann, I can confirm that the issues seem fixed with FSLogix Version: 3.25.822.19044 (tested against az cli and Anaconda Navigator).