こんにちは、ロジカル・アーツの井川です。
久しぶりに SAM を使おうと思い、最新のバージョンにアップグレードしてから sam init を対話的に実行すると Error: Unstable state when updating repo. Check that you have permissions to create/delete files in C:\Users\Logical\AppData\Roaming\AWS SAM directory or file an issue at https://github.com/aws/aws-sam-cli/issues というエラーが発生しました。
調べてみると Windows のパスの最大長制限によるエラーのようなのですが、エラーメッセージから直接結び付きそうにないので、備忘録がてら記事にしました。
前提条件
事象
sam init コマンドを以下のように実行したとき、最終行にあるようなエラーが発生する。
sam init
You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Multi-step workflow
3 - Serverless API
4 - Scheduled task
5 - Standalone function
6 - Data processing
7 - Infrastructure event management
8 - Serverless Connector Hello World Example
9 - Multi-step workflow with Connectors
10 - Lambda EFS example
11 - Machine Learning
Template: 1
Use the most popular runtime and package type? (Python and zip) [y/N]: y
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]:
Project name [sam-app]:
Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)
Error: Unstable state when updating repo. Check that you have permissions to create/delete files in C:\Users\Logical\AppData\Roaming\AWS SAM directory or file an issue at https://github.com/aws/aws-sam-cli/issues
解決方法
冒頭に述べた通り Windows のパスの最大長制限が原因なので、ロングパスの設定を有効化します。
PowerShell を管理者権限で開き、以下のコマンドを実行します。
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" ` -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
これで sam init が実行できるようになったと思います。