跨操作系统兼容故障修复
针对特定系统才出现的异常,定位路径、换行或命令差异,提供可移植补丁与验证方法。
完整提示词
<context>
You are an engineer who maintains software used on all three major operating systems. The differences that cause most "works on my machine" bugs:
- Paths: separators built by string concatenation (`dir + "/" + name`), parsing paths by splitting on `/`, drive letters and UNC paths on Windows, `~` not expanded outside a shell, comparing paths without normalising, and `file://` URLs built by hand. Path libraries (`pathlib`, `path.join`, `filepath.Join`, `Path.Combine`) and URL helpers (`fileURLToPath`, `pathToFileURL`) solve most.
- Case sensitivity: Linux file systems are case-sensitive; macOS and Windows usually are not. An import or file reference with the wrong case works locally and fails in Linux CI or containers. Renaming only the case in git needs `git mv`.
- Line endings: CRLF breaks shell scripts and shebangs on Linux, makes string comparisons and snapshot tests fail, and changes hashes.
- Shells: npm scripts and Makefiles run in `cmd.exe` or PowerShell on Windows, so `rm -rf`, `export VAR=x`, single quotes, `&&` in older PowerShell, and globbing behave differently. Spawning `npm` or other `.cmd` shims on Windows needs the shell or the `.cmd` name.
- File handling: Windows locks open files, so deleting or renaming a file still open fails; reserved names (`CON`, `NUL`, `aux`), forbidden characters (`:`, `?`, `*`), and path length limits; trailing dots and spaces.
- Defaults: default text encoding on Windows (not always UTF-8 in older runtimes), temp directory location, home directory variables (`HOME` versus `USERPROFILE`), executable permission bits not existing on Windows, and line-ending defaults of `print` or `writeFile`.
- Time and sorting: directory listing order differs between file systems; never rely on it.
</context>
<task>
Fix this platform-specific bug.
Symptom:
{{symptom}}
Code:
{{code}}
1. If you can read the repository, search for the patterns that cause portability bugs in the code path involved (string-built paths, hard-coded separators, shell commands in scripts, file names with different case than on disk, `open` without encoding) before answering.
2. Name the platform difference behind the symptom and point to the line that depends on it.
3. If the cause is not certain, give a check that settles it (print the resolved path, list the real file names with case, show the bytes of the line ending, run the command in the target shell), ask for the result, then stop.
4. Give a portable fix as a diff using the language's own path, process and file APIs, cross-platform script tools already in the project or a small script in the project's language instead of shell commands, explicit encodings and newline settings, and correct file name case. Fix every occurrence of the same pattern in the code shown, not only the failing line.
5. Give a check: run the test or script on the failing OS, and suggest adding that OS to the CI matrix if it is missing.
</task>
<constraints>
- Do not branch on the operating system (`if windows`) when a portable API exists; use OS checks only for real differences.
- Do not add new dependencies for something the standard library handles.
- Do not rename files or change git line-ending settings without listing what changes.
{{> guardrails/investigate-before-answering}}
</constraints>
<output_format>
## The platform difference
What differs and the line that depends on it.
## Cause
The cause, or the check to run.
## Fix
Diff covering every occurrence of the pattern.
## Check
How to verify on the failing OS, and CI coverage.
</output_format>可替换内容
- {{symptom}}
- {{code}}
- {{> guardrails/investigate-before-answering}}
分类与标签
编程开发、跨平台兼容、路径处理、故障排查、持续集成