hogashi.*

日記から何から

VSCodeのUriでファイルシステム上のパスを見るときはpathよりfsPathがよさそう

 VSCode拡張機能を作ったりするとき、 vscode.Uriファイルシステム上のパスを見るには、 path よりも fsPath のほうが (OS に合わせたパスになるので) よさそう。
 Mac では値が変わらないので気づかなかったけど、 Windows ではこういう感じで値が違う。

path: '/c:/Users/azuma/Documents/ghq/test/vscode-copy-github-permalink'
fsPath: 'c:\\Users\\azuma\\Documents\\ghq\\test\\vscode-copy-github-permalink'

 vscode.Uri の型についているコメントを見ると説明されている *1https://github.com/microsoft/vscode/blob/58e7c7b8865e3c3ea77055461ffb5d656a7a46af/src/vscode-dts/vscode.d.ts#L1420-L1440
 UNIX 系?のファイルシステムでは、パスの区切りがスラッシュだけど、 Windows ではバックスラッシュで、 fsPath はそこを含めてプラットフォームに合わせたパスになっている、と書かれていそう。あと UNC paths というのは Windows ネットワークにおけるパスとしてそういう表記方法があるらしい。

* The string representing the corresponding file system path of this Uri.
*
* Will handle UNC paths and normalize windows drive letters to lower-case. Also
* uses the platform specific path separator.
*
* * Will *not* validate the path for invalid characters and semantics.
* * Will *not* look at the scheme of this Uri.
* * The resulting string shall *not* be used for display purposes but
* for disk operations, like `readFile` et al.
*
* The *difference* to the {@linkcode Uri.path path}-property is the use of the platform specific
* path separator and the handling of UNC paths. The sample below outlines the difference:
* ```ts
* const u = URI.parse('file://server/c$/folder/file.txt')
* u.authority === 'server'
* u.path === '/shares/c$/file.txt'
* u.fsPath === '\\server\c$\folder\file.txt'
* ```
https://github.com/microsoft/vscode/blob/58e7c7b8865e3c3ea77055461ffb5d656a7a46af/src/vscode-dts/vscode.d.ts#L1420-L1440
日記

 VSCode拡張機能で、開いているファイルに対応する GitHub 上での URL をコピーするというの *2 を作っている。不具合を issue *3 で教えてもらい、 Mac で直したら Windows で動かなくなっていて、 Windowsデバッグしたら pathfsPath の違いがあることに気づいた。
 直したあと issue 上で動かなくなったよって教えてもらったときには Mac でしか試してなくて、再現しなくてなんでだろ……と思ったまま 2ヶ月経っていたのだけど、他の人にもそういえばその後どう?ってコメントしてもらって、 Windows で再現手順をもらったところで、そういえば Windows で動かないってことがあるのかも、と気づけた。ファイルシステムのパスを扱うときは OS の違いも意識したい……。