Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Directory LockNew to Visual Studio Code? Get it now.
Directory Lock

Directory Lock

shamantao

|
1 install
| (0) | Free
Lock directories as read-only to prevent unauthorized modifications by AI agents and other tools
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

🔒 Directory Lock - VS Code Extension

Protect your directories from unauthorized modifications by AI agents and other tools.

Version License

✨ Features

  • 🔒 Lock directories with a simple right-click in the Explorer
  • 🔓 Unlock directories just as easily
  • 💾 Persistent state - Locked directories remain locked across sessions
  • 🛡️ Complete protection - Prevents modifications, deletions, creations, and renames
  • 🎨 Visual indicators - Locked directories display a 🔒 badge and red color
  • 📋 View locked directories - Command to see all currently locked directories
  • ⚡ Instant restoration - Automatically restores deleted/modified files in locked directories

📦 Installation

From VS Code Marketplace

  1. Open VS Code
  2. Go to Extensions (Cmd+Shift+X or Ctrl+Shift+X)
  3. Search for "Directory Lock"
  4. Click Install

From VSIX file

  1. Download the latest .vsix file from Releases
  2. Open Command Palette (Cmd+Shift+P or Ctrl+Shift+P)
  3. Type "Extensions: Install from VSIX..."
  4. Select the downloaded file

🚀 Usage

Lock a directory

  1. In the VS Code Explorer, right-click on a folder
  2. Select "🔒 Lock this directory"
  3. The directory is now protected (shows 🔒 badge)

Unlock a directory

  1. Right-click on the locked folder
  2. Select "🔓 Unlock this directory"
  3. The directory can now be modified

View all locked directories

  1. Open Command Palette (Cmd+Shift+P or Ctrl+Shift+P)
  2. Type "Directory Lock: Show Locked Directories"
  3. A list of all locked directories appears

🛡️ Protection Features

When a directory is locked, the extension prevents:

  • ✋ File modifications - Edits are reverted to original content
  • ✋ File creation - New files are immediately deleted
  • ✋ File deletion - Deleted files are automatically restored
  • ✋ File/folder renaming - Renames are instantly reversed

Error messages appear for each blocked operation.

🎯 Use Cases

This extension is particularly useful for:

  • 🤖 Protecting directories from AI agents (like GitHub Copilot) that might modify files without permission
  • 📦 Locking dependencies or auto-generated files
  • 🔐 Preventing accidental modifications to critical code
  • 👥 Marking "read-only" zones when collaborating
  • 🧪 Preserving test fixtures and reference data

🏗️ Technical Architecture

Core Components

1. State Management

  • Uses context.workspaceState to persist locked directories
  • In-memory Set<string> for fast lookups
  • Automatic state restoration on workspace load

2. File Protection System

Document Modification Protection:

  • onDidOpenTextDocument - Saves original content when files are opened
  • onDidChangeTextDocument - Warns users about modifications in locked directories
  • onWillSaveTextDocument - Restores original content before save using waitUntil()

File System Operation Protection:

  • Creation: onDidCreateFiles → Immediate deletion of created files
  • Deletion: onWillDeleteFiles + onDidDeleteFiles → Backup before deletion, restore after
  • Rename: onWillRenameFiles + onDidRenameFiles → Error throwing + immediate reversal

3. Visual Indicators

  • Custom FileDecorationProvider implementation
  • Displays 🔒 badge on locked directories
  • Red color theme for visual distinction
  • EventEmitter for decoration updates

4. Data Structures

// Locked directories
lockedDirectories: Set<string>;

// Original content for modification detection
originalContent: Map<string, string>;

// File backups for deletion restoration
fileBackups: Map<string, { content: Uint8Array; type: FileType }>;

Event Flow

User locks directory
    ↓
workspaceState.update()
    ↓
decorationChangeEmitter.fire()
    ↓
Visual indicator appears
    ↓
File operations monitored
    ↓
Blocked operations immediately reversed

API Usage

  • vscode.workspace.onWillSaveTextDocument - Intercept saves
  • vscode.workspace.onDidCreateFiles - Detect creations
  • vscode.workspace.onWillDeleteFiles / onDidDeleteFiles - Handle deletions
  • vscode.workspace.onWillRenameFiles / onDidRenameFiles - Handle renames
  • vscode.workspace.fs.readFile/writeFile/delete - File system operations
  • vscode.window.registerFileDecorationProvider - Visual decorations
  • vscode.commands.registerCommand - Command registration

Limitations

  • VS Code API doesn't provide true "blocking" of file operations
  • Operations are reversed after they occur (near-instant, but not preventive)
  • Deleted directories can only be restored if they were empty or if backup was captured
  • Doesn't prevent modifications via external tools outside VS Code

🛠️ Development

Prerequisites

  • Node.js 16+
  • VS Code 1.105.0+

Setup

# Clone the repository
git clone https://github.com/shamantao/vscode-directory-lock
cd vscode-directory-lock

# Install dependencies
npm install

# Compile
npm run compile

Testing

Press F5 in VS Code to launch the Extension Development Host.

Building

# Create VSIX package
npm install -g @vscode/vsce
vsce package

📝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

🐛 Known Issues

  • Operations are reversed after they occur (not blocked preventively) due to VS Code API limitations
  • Nested directory deletions might not fully restore complex structures
  • External file system changes (outside VS Code) are not monitored

📜 Changelog

See CHANGELOG.md for release history.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

👏 Acknowledgments

  • Inspired by the need to protect code from overzealous AI agents
  • Built with the VS Code Extension API
  • Thanks to the VS Code extension development community

🔗 Links

  • VS Code Marketplace
  • GitHub Repository
  • Report Issues
  • Version française 🇫🇷

Made with ❤️ for safer development

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft