VS Code File Locker
A Visual Studio Code extension that implements file locking to prevent concurrent editing of files by multiple users.
Features
- Lock files to prevent other users from editing them
- Visual indicators showing locked/unlocked status in the status bar
- Read-only mode for locked files
- Lock expiration mechanism
- Context menu commands for locking/unlocking files
- Instance ID tracking to handle multiple VS Code windows per user
How It Works
The extension tracks when files are opened and maintains a lock registry. When a user locks a file:
- A lock entry is created with the user's identifier and VS Code instance ID
- The status bar updates to show the locked status
- Other users trying to edit the file will be notified it's locked
- The original user can unlock the file when done editing
Currently, the extension supports two lock storage modes:
- In-memory locks: Only effective within the same VS Code window
- File system-based locks: Stored as files in a
.locks
directory, effective across all instances
Instance ID Tracking
Each VS Code window gets a unique instance ID when the extension is activated. This allows:
- The same user to have multiple VS Code windows open without conflicting locks
- Clear identification of which window has locked a file
- Better handling of lock management when multiple instances are involved
For example, if you lock a file in one window, then try to edit it in another window, you'll see a message indicating that you (not just another user) have locked the file in a different VS Code instance.
Installation
From VS Code Marketplace
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Search for "VS Code File Locker"
- Click Install
Manual Installation
- Download the .vsix file from the releases page
- In VS Code, open Extensions (Ctrl+Shift+X)
- Click on the "..." menu at the top right of the Extensions panel
- Select "Install from VSIX..."
- Choose the downloaded .vsix file
Usage
- Open a file you want to edit
- Lock the file using:
- The "Lock Current File" command from the command palette (Ctrl+Shift+P)
- The context menu option (right-click in editor)
- The status bar indicator
- Edit your file as normal
- When finished, unlock the file using similar methods
Lock Management
- Locks automatically expire after a configurable time period
- Files can only be unlocked by the same user and VS Code instance that created the lock
- Administrators can use the "Force Unlock" command for emergency situations
- The "List All Locked Files" command shows all locks, including which instance created them
Extension Settings
fileLocker.lockExpirationTime
: Time in seconds before a lock expires automatically (default: 3600)
fileLocker.showNotifications
: Enable/disable notifications when files are locked or unlocked
fileLocker.lockStorageType
: Type of lock storage to use (memory
or fileSystem
)
fileLocker.autoLockOnOpen
: Automatically lock files when opened (default: true)
fileLocker.autoLockOnEdit
: Automatically lock files when edited (default: false)
fileLocker.autoUnlockOnClose
: Automatically unlock files when closed (default: false)
fileLocker.showInstanceIds
: Show instance IDs in lock information (default: true)
fileLocker.fileFilter
: Array of glob patterns to determine which files should be lockable (default: ["**/*"])
File Filtering
You can control which files the File Locker extension applies to by configuring the fileLocker.fileFilter
setting. This setting accepts an array of glob patterns:
- Include patterns are specified as regular glob patterns (e.g.,
"**/*.js"
)
- Exclude patterns are specified with a leading
!
character (e.g., "!**/node_modules/**"
)
Examples
// Lock only JavaScript and TypeScript files
"fileLocker.fileFilter": [
"**/*.js",
"**/*.ts"
]
// Lock all files except those in node_modules
"fileLocker.fileFilter": [
"**/*",
"!**/node_modules/**"
]
// Lock only files in the src directory
"fileLocker.fileFilter": [
"**/src/**"
]
The file filtering happens at the point of locking, so files that don't match the filter:
- Won't be auto-locked on opening or editing
- Can't be locked manually
- Won't show any lock-related UI
This feature is useful for excluding generated files, dependencies, or other files that don't need locking.
Commands
- File Locker: Lock Current File: Lock the currently active file
- File Locker: Unlock Current File: Unlock the currently active file
- File Locker: List All Locked Files: View all files that are currently locked
- File Locker: Force Unlock File: Administrative command to force unlock a file locked by another user or instance
Shared Lock Storage
To make locks work across different machines and users, you can use the file system storage option:
- Set
fileLocker.lockStorageType
to fileSystem
in your workspace settings
- Ensure all team members have access to the shared repository
- Add
.locks/
to your .gitignore
file (automatically done by the extension)
Network Share Considerations
For teams using a network share:
- Ensure all users have read/write access to the
.locks
directory
- Lock files are stored as JSON and include the username, hostname, and VS Code instance ID
- The lock expiration mechanism ensures that "orphaned" locks don't persist
Force Unlocking
In some cases, locks might need to be removed by administrators:
- A user may have closed VS Code without unlocking files
- A system crash may have prevented proper cleanup
- Multiple VS Code instances from the same user might be confusing
For these situations, use the "Force Unlock File" command. This should be used with caution and only when necessary.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This extension is licensed under the MIT License.