AGENT SKILL
跨平台路径处理
cross-platform-paths
本 VS Code 扩展中跨平台路径处理的关键模式。Windows 与 POSIX 路径差异导致的缺陷是问题的头号来源。在审查或编写路径相关代码时使用此技能。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 147
- Forks
- 83
01 前言 Front Matter
- name
- cross-platform-paths
- description
- Critical patterns for cross-platform path handling in this VS Code extension. Windows vs POSIX path bugs are the #1 source of issues. Use this skill when reviewing or writing path-related code.
- argument-hint
- Review path handling in [file or component]
- user-invocable
- false
02 资源文件 7
- 参考文档AGENTS.md1.7 KB
- 参考文档references/AGENTS.md1.3 KB
- 参考文档references/channel-commands.md13.2 KB
- 参考文档references/contribute.md4.9 KB
- 参考文档references/implementation.md11.1 KB
- 参考文档references/review.md5.0 KB
- 参考文档references/webui.md9.7 KB
03 SKILL.md 正文
Cross-Platform Path Handling
CRITICAL: This extension runs on Windows, macOS, and Linux. Path bugs are the #1 source of issues.
Core Rules
Rule 1: Never Concatenate Paths with /
// ❌ WRONG: POSIX-style path concatenation
const envPath = homeDir + '/.venv/bin/python';
// ✅ RIGHT: Use path.join()
const envPath = path.join(homeDir, '.venv', 'bin', 'python');
Rule 2: Use path.resolve() for Comparisons, Not path.normalize()
// ❌ WRONG: path.normalize keeps relative paths relative on Windows
const normalized = path.normalize(fsPath);
// path.normalize('\test') → '\test' (still relative!)
// ✅ RIGHT: path.resolve adds drive letter on Windows
const normalized = path.resolve(fsPath);
// path.resolve('\test') → 'C:\test' (absolute!)
// When comparing paths, use resolve() on BOTH sides:
const pathA = path.resolve(fsPath);
const pathB = path.resolve(e.environmentPath.fsPath);
return pathA === pathB;
Rule 3: Use Uri.file().fsPath for VS Code Paths
// ❌ WRONG: Raw string comparison
if (filePath === otherPath) {
}
// ✅ RIGHT: Compare fsPath to fsPath
import { Uri } from 'vscode';
const fsPathA = Uri.file(pathA).fsPath;
const fsPathB = Uri.file(pathB).fsPath;
if (fsPathA === fsPathB) {
}
Platform-Specific Gotchas
Windows
| Issue | Details |
|---|---|
| Drive letters | Paths start with C:\, D:\, etc. |
| Backslashes | Separator is \, not / |
| Case insensitivity | C:\Test equals c:\test |
| Long paths | Paths >260 chars may fail |
| Mapped drives | Z:\ may not be accessible |
| pyenv-win | Uses pyenv.bat, not pyenv or pyenv.exe |
| Poetry cache | %LOCALAPPDATA%\pypoetry\Cache\virtualenvs |
| UNC paths | \\server\share\ format |
macOS
| Issue | Details |
|---|---|
| Case sensitivity | Depends on filesystem (usually insensitive) |
| Homebrew symlinks | Complex symlink chains in /opt/homebrew/ |
| Poetry cache | ~/Library/Caches/pypoetry/virtualenvs |
| XCode Python | Different from Command Line Tools Python |
Linux
| Issue | Details |
|---|---|
| Case sensitivity | Paths ARE case-sensitive |
| /bin symlinks | /bin may be symlink to /usr/bin |
| XDG directories | ~/.local/share/virtualenvs for pipenv |
| Poetry cache | ~/.cache/pypoetry/virtualenvs |
| Hidden files | Dot-prefixed files are hidden |
Common Patterns
Getting Platform-Specific Paths
import * as os from 'os';
import * as path from 'path';
// Home directory
const home = os.homedir(); // Works cross-platform
// Construct paths correctly
const venvPath = path.join(home, '.venv', 'bin', 'python');
// Windows: C:\Users\name\.venv\bin\python
// macOS: /Users/name/.venv/bin/python
// Linux: /home/name/.venv/bin/python
Environment-Specific Executable Names
const isWindows = process.platform === 'win32';
// Python executable
const pythonExe = isWindows ? 'python.exe' : 'python';
// Activate script
const activateScript = isWindows
? path.join(venvPath, 'Scripts', 'activate.bat')
: path.join(venvPath, 'bin', 'activate');
// pyenv command
const pyenvCmd = isWindows ? 'pyenv.bat' : 'pyenv';
Normalizing Paths for Comparison
import { normalizePath } from './common/utils/pathUtils';
// Use normalizePath() for map keys and comparisons
const key = normalizePath(filePath);
cache.set(key, value);
// But preserve original for user display
traceLog(`Discovered: ${filePath}`); // Keep original
Handling Uri | string Union Types
// ❌ WRONG: Assuming Uri
function process(locator: Uri | string) {
const fsPath = locator.fsPath; // Crashes if string!
}
// ✅ RIGHT: Handle both types
function process(locator: Uri | string) {
const fsPath = locator instanceof Uri ? locator.fsPath : locator;
// Now normalize for comparisons
const normalized = path.resolve(fsPath);
}
File Existence Checks
import * as fs from 'fs';
import * as path from 'path';
// Check file exists (cross-platform)
const configPath = path.join(projectRoot, 'pyproject.toml');
if (fs.existsSync(configPath)) {
// File exists
}
// Use async version when possible
import { promises as fsPromises } from 'fs';
try {
await fsPromises.access(configPath);
// File exists
} catch {
// File does not exist
}
Shell Path Escaping
// ❌ WRONG: Unescaped paths in shell commands
terminal.sendText(`python ${filePath}`);
// D:\path\file.py becomes "D:pathfile.py" in some shells!
// ✅ RIGHT: Quote paths
terminal.sendText(`python "${filePath}"`);
// For Git Bash on Windows, escape backslashes
const shellPath = isGitBash ? filePath.replace(/\\/g, '/') : filePath;
Testing Cross-Platform Code
When testing path-related code:
- Test on Windows (cmd, PowerShell, Git Bash)
- Test on macOS (zsh, bash)
- Test on Linux (bash, fish)
Pay special attention to:
- Paths with spaces:
C:\Program Files\Python - Paths with Unicode:
~/проекты/ - Very long paths (>260 chars on Windows)
- Paths with special characters:
$,&,(,)
04 同仓库更多 Skills
VS Code 设置优先级规则
settings-precedence
VS Code 设置优先级规则及常见陷阱。适用于任何读取或写入设置的代码。涵盖 getConfiguration 作用域、inspect() 与 get() 的区别,以及多工作区处理。
★ 147
VS Code 扩展冒烟测试
run-smoke-tests
运行冒烟测试,在真实 VS Code 环境中验证扩展功能。在更改后检查基本功能是否正常工作时使用。
★ 147
代码提交前(pre-commit)检查
run-pre-commit-checks
在提交代码前运行强制性的 pre-commit 检查,包括 lint(代码规范检查)、类型检查和单元测试。每次提交前都必须运行。
★ 147
扩展组件集成测试运行
run-integration-tests
运行集成测试以验证扩展的各个组件能否正确协同工作。在修改组件交互或事件处理后使用此技能。
★ 147
E2E 端到端测试运行
run-e2e-tests
运行 E2E 测试以验证完整的用户工作流(如环境的发现、创建和选择)。在发布前或进行重大变更后使用此技能。
★ 147
Python 环境管理器探测
python-manager-discovery
针对特定环境管理器的探测模式及已知问题。在编写或审查 conda、poetry、pipenv、pyenv 或 venv 的环境探测代码时使用。
★ 147