Skip to content

hookDevOps and cloud

Protect Lockfiles

A Claude Code hook that blocks hand edits to package-lock.json, yarn.lock, Cargo.lock, poetry.lock and other lockfiles the package manager owns.

npx workspacetemplates add protect-lockfiles --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 891 BFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',x=>d+=x).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){return}const i=j.tool_input||{};const f=String(i.file_path||'').split(String.fromCharCode(92)).join('/');const n=f.split('/').pop();const names=['package-lock.json','yarn.lock','pnpm-lock.yaml','bun.lock','bun.lockb','npm-shrinkwrap.json','Cargo.lock','poetry.lock','Pipfile.lock','uv.lock','composer.lock','Gemfile.lock','go.sum','pubspec.lock','packages.lock.json'];if(names.includes(n)){console.error('Blocked: '+n+' is generated by the package manager. Change the manifest, then let the install command rewrite the lockfile.');process.exitCode=2}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

A lockfile pins exact versions and integrity hashes. A hand edit, even a small one, can break installs or hide a downgrade, so this PreToolUse hook refuses Edit and Write calls that target one.

Fifteen common lockfile names are covered, from npm and yarn to Cargo, Poetry, Composer, Bundler and Go. The agent is told to change the manifest and run the install command, which keeps the lock honest.

Example

you › Fix the conflict in pnpm-lock.yaml by keeping both versions.

agent › The edit is refused. The agent resolves package.json, runs pnpm install and lets pnpm write a clean lockfile.

Questions

Can the agent still update dependencies?
Yes. Shell commands such as npm install or cargo update are not affected, only direct file edits and writes.
How do I add another lockfile name?
Add the file name to the list inside the command, between the square brackets, keeping the single quotes.
  • Block Global Installs

    HookDevOps and cloud

    Free
  • Block npm Publish

    Hooknode

    Free
  • Check Node Version

    Hooknode

    Free
  • Protect CI Workflows

    Hookgithub-actions

    Free