🔒 Directory Lock - VS Code Extension
Protect your directories from unauthorized modifications by AI agents and other tools.

✨ 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
- Open VS Code
- Go to Extensions (
Cmd+Shift+X
or Ctrl+Shift+X
)
- Search for "Directory Lock"
- Click Install
From VSIX file
- Download the latest
.vsix
file from Releases
- Open Command Palette (
Cmd+Shift+P
or Ctrl+Shift+P
)
- Type "Extensions: Install from VSIX..."
- Select the downloaded file
🚀 Usage
Lock a directory
- In the VS Code Explorer, right-click on a folder
- Select "🔒 Lock this directory"
- The directory is now protected (shows 🔒 badge)
Unlock a directory
- Right-click on the locked folder
- Select "🔓 Unlock this directory"
- The directory can now be modified
View all locked directories
- Open Command Palette (
Cmd+Shift+P
or Ctrl+Shift+P
)
- Type "Directory Lock: Show Locked Directories"
- 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
)
- Commit your changes (
git commit -m 'Add some AmazingFeature'
)
- Push to the branch (
git push origin feature/AmazingFeature
)
- 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
Made with ❤️ for safer development