

vendorファイルの一部に不具合があり、パッチを当てることにしました。  
しかし、実行してみるとエラーが発生してしまい、パッチを当てることができません。  
`Could not apply patch! Skipping. The error was: Cannot apply patch`

次のような手順で行いました。

  1. composer-patchesをインストール  
`composer require cweagans/composer-patches`

  2. パッチファイルの保存先フォルダ `patches_composer` を作成

  3. 編集対象ファイル(Response.php)を .orig としてコピー  
`cp vendor/inertiajs/inertia-laravel/src/Response.php
vendor/inertiajs/inertia-laravel/src/Response.php.orig`

  4. 編集対象ファイル(Response.php)を編集（修正）

  5. 差分ファイルの作成  
`diff -u vendor/inertiajs/inertia-laravel/src/Response.php.orig
vendor/inertiajs/inertia-laravel/src/Response.php > patches_composer/inertia-
laravel-response.patch`  
編集対象ファイル(Response.php) とオリジナル(Response.php.orig)を比較して、  
差分ファイル(patches_composer/inertia-laravel-response.patch)を出力

  6. composer.json ファイルにパッチの情報を追加

[code]

        "extra": {
            "patches": {
                "inertiajs/inertia-laravel": {
                    "twice url": "patches_composer/inertia-laravel-response.patch"
                }
            }
        },
    
[/code]

ちなみに差分ファイルの中身は次のようになっています  
※先頭行の Response.php.orig を Response.php に変更

[code]

    --- vendor/inertiajs/inertia-laravel/src/Response.php   2023-05-26 02:25:36.821932134 +0900
    +++ vendor/inertiajs/inertia-laravel/src/Response.php   2023-05-26 02:25:47.802044249 +0900
    @@ -91,7 +91,7 @@
             $props = ($only && $request->header('X-Inertia-Partial-Component') === $this->component)
                 ? Arr::only($this->props, $only)
                 : array_filter($this->props, static function ($prop) {
    -                return ! ($prop instanceof LazyProp);
    +                return !($prop instanceof LazyProp);
                 });
     
             $props = $this->resolvePropertyInstances($props, $request);
    @@ -99,7 +99,7 @@
             $page = [
                 'component' => $this->component,
                 'props' => $props,
    -            'url' => $request->getBaseUrl().$request->getRequestUri(),
    +            'url' => $request->getRequestUri(),
                 'version' => $this->version,
             ];
     
    
    
[/code]

### エラー

しかしながら、composer install, composer update を実行すると次のようなエラーが発生します。

[code]

      - Applying patches for inertiajs/inertia-laravel
        patches_composer/inertia-laravel-response.patch (twice url)
       Could not apply patch! Skipping. The error was: Cannot apply patch patches_composer/inertia-laravel-response.patch
    
[/code]

エラーの詳細がわからないため、どのように対応すればよいのかがわかりません。  
上記の手順の中で、誤った部分があるでしょうか？

よろしくお願いします。

【追記】  
パッチファイルの先頭が「更新されるファイル」のようなので下記のように変更しました。  
`--- vendor/inertiajs/inertia-laravel/src/Rsponse.php`

`composer install -vvv` で処理の詳細を出力。

[code]

    Dependency resolution completed in 0.000 seconds
    Package operations: 1 install, 0 updates, 0 removals
    Installs: inertiajs/inertia-laravel:v0.6.9
    Reading /home/my_name/.cache/composer/files/inertiajs/inertia-laravel/09ba8e3a83de8dc1b2da392e03f79f9e6a30867a.zip from cache
      - Loading inertiajs/inertia-laravel (v0.6.9) from cache
    > pre-package-install: cweagans\Composer\Patches->gatherPatches
    Gathering patches for root package.
    Gathering patches for dependencies. This might take a minute.
    Found 1 patches for inertiajs/inertia-laravel.
      - Installing inertiajs/inertia-laravel (v0.6.9): Extracting archive
    Executing async command (CWD): '/usr/bin/unzip' -qq '/home/my_name/my_project/vendor/composer/tmp-7ca65e1c011ddf0b2205cb6db25fb1ba' -d '/home/my_name/my_project/vendor/composer/4b699ac1'
    Executing async command (CWD): rm -rf '/home/my_name/my_project/vendor/composer/4b699ac1'
    > post-package-install: cweagans\Composer\Patches->postInstall
      - Applying patches for inertiajs/inertia-laravel
        patches_composer/inertia-laravel-response.patch (twice url)
    patch '-p1' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    Executing command (CWD): patch '-p1' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    can't find file to patch at input line 3
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |--- vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:43:20.246192609 +0900
    |+++ vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:41:02.059394179 +0900
    --------------------------
    File to patch:
    Skip this patch? [y]
    Skipping patch.
    
    1 out of 1 hunk ignored
    
    patch '-p0' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    Executing command (CWD): patch '-p0' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    can't find file to patch at input line 3
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |--- vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:43:20.246192609 +0900
    |+++ vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:41:02.059394179 +0900
    --------------------------
    File to patch:
    Skip this patch? [y]
    Skipping patch.
    
    1 out of 1 hunk ignored
    
    patch '-p2' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    Executing command (CWD): patch '-p2' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    can't find file to patch at input line 3
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |--- vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:43:20.246192609 +0900
    |+++ vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:41:02.059394179 +0900
    --------------------------
    File to patch:
    Skip this patch? [y]
    Skipping patch.
    
    1 out of 1 hunk ignored
    
    patch '-p4' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    Executing command (CWD): patch '-p4' --no-backup-if-mismatch -d '/home/my_name/my_project/vendor/inertiajs/inertia-laravel' < '/home/my_name/my_project/patches_composer/inertia-laravel-response.patch'
    can't find file to patch at input line 3
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |--- vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:43:20.246192609 +0900
    |+++ vendor/inertiajs/inertia-laravel/src/Response.php  2023-05-26 07:41:02.059394179 +0900
    --------------------------
    File to patch:
    Skip this patch? [y]
    Skipping patch.
    
    1 out of 1 hunk ignored
    
       Could not apply patch! Skipping. The error was: Cannot apply patch patches_composer/inertia-laravel-response.patch
    
[/code]

上記の動作を見ると、`inertiajs/inertia-laravel/` がアップデートされて、その後にパッチを当てているように見えます。

`can't find file to patch at input line 3`
については[無視しても良い](https://maxcluster.de/en/knowledge-
base/managedcenter/fasteristableexists)ようです。


CC BY-SA 4.0
2023-05-25T18:20:27.653
94979
2023-05-26T01:18:27.913
2023-05-25T23:15:28.543
7980
7980
question
php composer
phpのパッチ処理 cweagans/composer-patches で エラー スキップしてしまう


解決しました。  
対象ファイルの部分を `composer.json` の指定された `inertiajs/inertia-laravel`
からの相対位置にすると上手くいきました

[code]

    --- src/Response.php   2023-05-26 02:25:36.821932134 +0900
    +++ src/Response.php   2023-05-26 02:25:47.802044249 +0900
    
[/code]


CC BY-SA 4.0
2023-05-26T01:18:27.913
94981
2023-05-26T01:18:27.913
7980
94979
answer