You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gcloud SDK for PowerShell配置文件重复行错误排查求助

问题:Gcloud配置文件重复project行导致加载失败

环境与问题现象

  • Windows Server环境,多用户/服务账号运行PowerShell脚本
  • 不定期出现Gcloud加载失败,日志显示config_fp的[core]段中project行重复
  • 手动删除重复行后恢复,但后续会复发
  • 已创建独立配置config_cag,问题依旧

错误栈片段

ERROR: gcloud failed to load. This usually indicates corruption in your gcloud installation or problems with your Python interpreter.
Please verify that the following is the path to a working Python 3.8-3.13 executable:
C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\python.exe
If it is not, please set the CLOUDSDK_PYTHON environment variable to point to a working Python executable.
If you are still experiencing problems, please reinstall the Google Cloud CLI.
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\gcloud.py", line 110, in gcloud_exception_handler
yield
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\gcloud.py", line 187, in main
gcloud_main = _import_gcloud_main()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\gcloud.py", line 90, in _import_gcloud_main
import googlecloudsdk.gcloud_main
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\gcloud_main.py", line 34, in
from googlecloudsdk.calliope import base
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\calliope\base.py", line 30, in
from googlecloudsdk.calliope import arg_parsers
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\calliope\arg_parsers.py", line 64, in
from googlecloudsdk.core import log
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\log.py", line 849, in
_log_manager = _LogManager()
^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\log.py", line 584, in init
self.Reset(sys.stdout, sys.stderr)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\log.py", line 597, in Reset
std_console_formatter = _ConsoleFormatter(stderr)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\log.py", line 371, in init
console_log_format = properties.VALUES.core.console_log_format.Get()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\properties.py", line 4162, in Get
property_value = self.GetPropertyValue(required, validate)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\properties.py", line 4182, in GetPropertyValue
named_configs.ActivePropertiesFile.Load(),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\configurations\named_configs.py", line 447, in Load
ActivePropertiesFile._PROPERTIES = properties_file.PropertiesFile(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\configurations\properties_file.py", line 52, in init
self.__Load(properties_path)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\configurations\properties_file.py", line 64, in __Load
parsed_config.read(properties_path)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\Lib\configparser.py", line 684, in read
self._read(fp, filename)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\Lib\configparser.py", line 1075, in _read
raise DuplicateOptionError(sectname, optname,
configparser.DuplicateOptionError: While reading from 'C:\Users\appladmin\AppData\Roaming\gcloud\configurations\config_fp' [line 5]: option 'project' in section 'core' already exists

PowerShell代码片段

#... code not related to gcloud ...
            #Activate CAG  
            gcloud config configurations activate $cag 
            gcloud config set account $gcsaccount
            #gcloud auth activate-service-account $gcsaccount --key-file $credentialPath --project=$gcsproject
            #... code not related to gcloud ...
            gcloud storage ls --recursive $DestinationDir
            #De-Activate CAG  
            gcloud config configurations activate fp  
            $this.logging.LogMessage( "GCS Rsync completed::"+$SourceDir+"::"+$DestinationDir , "Info")             
            $this.logging.LogMessage( "End of class - GcsCopy - method - Rsync" , "Info")
        } catch {
            echo $_.Exception.GetType().FullName,$_.Exception.Message
            $this.logging.LogMessage( "Exception - Rsync - method - Rsync" , "Error")
            $this.logging.LogMessage(  $_.Exception.GetType().FullName+"::"+$_.Exception.Message , "Error")
        }

配置文件内容

config_fp

[core]
account = posteam-data-upload@ne-fprt-data-cloud-production.iam.gserviceaccount.com
project = ne-fprt-data-cloud-production
project = ne-fprt-data-cloud-production

config_cag

[core]
account = posteam-data-upload@ne-fprt-data-cloud-production.iam.gserviceaccount.com
project = ne-fprt-data-cloud-production

重复行产生的原因

  1. 多进程并发写入冲突:多个脚本/服务账号同时操作config_fp时,gcloud config set或配置切换操作可能并发写入,导致重复追加project行。Windows系统的文件锁机制如果没被Gcloud正确利用,就会出现这类竞争问题。
  2. 配置切换的隐式写入:脚本切换回fp配置时,Gcloud可能自动同步或写入配置项,若此时有其他进程也在操作该配置,就会触发重复写入。
  3. 未显式锁定项目配置:代码中注释了带--project参数的服务账号激活命令,激活账号后Gcloud可能尝试自动填充项目字段,加上多进程竞争,导致重复行出现。

彻底解决的方案

1. 为每个账号分配独立配置文件

不要让多个服务账号共用config_fp,为每个账号创建专属配置:

# 为目标账号创建独立配置
gcloud config configurations create account-specific-config
gcloud config configurations activate account-specific-config
gcloud config set account $gcsaccount
gcloud config set project ne-fprt-data-cloud-production

脚本中固定激活当前账号的专属配置,彻底避免交叉写入冲突。

2. 脚本中添加文件锁定逻辑

在操作Gcloud配置的代码块前后,添加Windows文件锁,防止并发写入:

$configPath = "C:\Users\appladmin\AppData\Roaming\gcloud\configurations\config_fp"
$lockFile = "$configPath.lock"
# 创建独占锁
$lock = New-Object System.IO.FileStream($lockFile, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)

try {
    #Activate CAG  
    gcloud config configurations activate $cag 
    gcloud config set account $gcsaccount
    gcloud storage ls --recursive $DestinationDir
    #De-Activate CAG  
    gcloud config configurations activate fp  
} finally {
    # 释放锁并删除锁文件
    $lock.Close()
    Remove-Item $lockFile -Force -ErrorAction SilentlyContinue
}

3. 执行Gcloud操作前自动修复配置文件

在脚本开头添加去重逻辑,确保配置文件没有重复项:

$configPath = "C:\Users\appladmin\AppData\Roaming\gcloud\configurations\config_fp"
$configContent = Get-Content $configPath -Raw
$newContent = @()
$currentSection = $null
$seenOptions = @{}

# 按段解析配置并去重
foreach ($line in ($configContent -split '\r?\n')) {
    if ($line -match '^\[(.*?)\]$') {
        $currentSection = $matches[1].Trim()
        $newContent += $line
        $seenOptions.Clear()
    } elseif ($line -match '^\s*([^=#]+?)\s*=' -and $currentSection) {
        $key = $matches[1].Trim()
        if (-not $seenOptions.ContainsKey($key)) {
            $newContent += $line
            $seenOptions[$key] = $true
        }
    } else {
        $newContent += $line
    }
}

# 写入去重后的配置
Set-Content $configPath -Value ($newContent -join "`r`n") -Force

4. 禁用Gcloud自动配置写入

通过环境变量禁止Gcloud自动修改配置文件,所有变更必须显式执行:

$env:CLOUDSDK_CONFIG_DISABLE_AUTO_WRITE = "true"

内容的提问来源于stack exchange,提问作者Liquid Nitrogen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 20:35:58