Skip to content

Commit

Permalink
resolves #819 fix drive letter normalization (#825)
Browse files Browse the repository at this point in the history
Previously, we were applying toLowerCase on the whole path causing the WebView to return 401 on resources (such as images)
  • Loading branch information
ggrossetie authored Nov 19, 2023
1 parent d0df68a commit 4d86a4c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
27 changes: 27 additions & 0 deletions src/test/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os from 'os'
import { Uri } from 'vscode'
import chai from 'chai'
import { normalizeUri } from '../util/workspace'

const expect = chai.expect

suite('Normalize URI', () => {
test('Should lowercase drive letter on Windows', async () => {
if (os.platform() === 'win32') {
const result = normalizeUri(Uri.parse('file:///C:/path/WITH/camelCase/A/b/C/index.adoc'))
expect(result.path).to.equal('/c:/path/WITH/camelCase/A/b/C/index.adoc')
}
})
test('Should do nothing since the drive letter is already lowercase', async () => {
if (os.platform() === 'win32') {
const result = normalizeUri(Uri.parse('file:///c:/path/WITH/camelCase/A/b/C/index.adoc'))
expect(result.path).to.equal('/c:/path/WITH/camelCase/A/b/C/index.adoc')
}
})
test('Should do nothing on Linux', async () => {
if (os.platform() !== 'win32') {
const result = normalizeUri(Uri.parse('/C/path/WITH/camelCase/A/b/C/index.adoc'))
expect(result.path).to.equal('/C/path/WITH/camelCase/A/b/C/index.adoc')
}
})
})
28 changes: 7 additions & 21 deletions src/test/workspaceHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os from 'os'
import vscode, { FileSystemError, FileType } from 'vscode'
import { getDefaultWorkspaceFolderUri } from '../util/workspace'
import { getDefaultWorkspaceFolderUri, normalizeUri } from '../util/workspace'

export async function removeFiles (files: vscode.Uri[]) {
for (const file of files) {
Expand All @@ -24,14 +23,9 @@ async function exists (file: vscode.Uri): Promise<boolean> {
}

export async function createFile (content: string, ...pathSegments: string[]): Promise<vscode.Uri> {
let file = vscode.Uri.joinPath(getDefaultWorkspaceFolderUri(), ...pathSegments)
const file = vscode.Uri.joinPath(getDefaultWorkspaceFolderUri(), ...pathSegments)
await vscode.workspace.fs.writeFile(file, Buffer.from(content))
if (os.platform() === 'win32') {
// normalize Windows drive letter
// https://github.com/microsoft/vscode/issues/194692
file = file.with({ path: file.path.replace(/^\/([A-Z]):.*/, (v) => v.toLowerCase()) })
}
return file
return normalizeUri(file)
}

export async function createDirectories (...pathSegments: string[]): Promise<void> {
Expand All @@ -57,24 +51,16 @@ export async function createDirectories (...pathSegments: string[]): Promise<voi
}

export async function createDirectory (...pathSegments: string[]): Promise<vscode.Uri> {
let dir = vscode.Uri.joinPath(getDefaultWorkspaceFolderUri(), ...pathSegments)
const dir = vscode.Uri.joinPath(getDefaultWorkspaceFolderUri(), ...pathSegments)
await vscode.workspace.fs.createDirectory(dir)
if (os.platform() === 'win32') {
// normalize Windows drive letter
// https://github.com/microsoft/vscode/issues/194692
dir = dir.with({ path: dir.path.replace(/^\/([A-Z]):.*/, (driverLetter) => driverLetter.toLowerCase()) })
}
return dir
return normalizeUri(dir)
}

export async function createLink (existingPathSegments: string[], newPathSegments: string[]): Promise<vscode.Uri> {
const fs = require('fs').promises
const workspaceUri = getDefaultWorkspaceFolderUri()
const existingPath = vscode.Uri.joinPath(workspaceUri, ...existingPathSegments)
let newPath = vscode.Uri.joinPath(workspaceUri, ...newPathSegments)
const newPath = vscode.Uri.joinPath(workspaceUri, ...newPathSegments)
await fs.symlink(existingPath.fsPath, newPath.fsPath)
if (os.platform() === 'win32') {
newPath = newPath.with({ path: newPath.path.replace(/^\/([A-Z]):.*/, (driverLetter) => driverLetter.toLowerCase()) })
}
return newPath
return normalizeUri(newPath)
}
17 changes: 12 additions & 5 deletions src/util/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import vscode, { Uri, WorkspaceFolder } from 'vscode'
import os from 'os'

const driveLetterRx = /(?<=^\/)([A-Z])(?=:\/)/

export function getWorkspaceFolder (uri: Uri): WorkspaceFolder | undefined {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri)
if (workspaceFolder && os.platform() === 'win32') {
return {
uri: workspaceFolder.uri.with({ path: workspaceFolder.uri.path.replace(/^\/([A-Z]):.*/, (driverLetter) => driverLetter.toLowerCase()) }),
uri: normalizeUri(workspaceFolder.uri),
name: workspaceFolder.name,
index: workspaceFolder.index,
}
Expand All @@ -17,7 +19,7 @@ export function getWorkspaceFolders (): WorkspaceFolder[] | undefined {
return vscode.workspace.workspaceFolders?.map((workspaceFolder) => {
if (os.platform() === 'win32') {
return {
uri: workspaceFolder.uri.with({ path: workspaceFolder.uri.path.replace(/^\/([A-Z]):.*/, (driverLetter) => driverLetter.toLowerCase()) }),
uri: normalizeUri(workspaceFolder.uri),
name: workspaceFolder.name,
index: workspaceFolder.index,
}
Expand All @@ -36,9 +38,14 @@ export function findDefaultWorkspaceFolderUri (): Uri | undefined {

export function getDefaultWorkspaceFolderUri (): Uri | undefined {
const workspaceFolders = getWorkspaceFolders()
let workspaceUri = workspaceFolders[0].uri
return normalizeUri(workspaceFolders[0].uri)
}

export function normalizeUri (uri: Uri): Uri {
// normalize Windows drive letter
// https://github.com/microsoft/vscode/issues/194692
if (os.platform() === 'win32') {
workspaceUri = workspaceUri.with({ path: workspaceUri.path.replace(/^\/([A-Z]):.*/, (driverLetter) => driverLetter.toLowerCase()) })
return uri.with({ path: uri.path.replace(driveLetterRx, (driverLetter) => driverLetter.toLowerCase()) })
}
return workspaceUri
return uri
}

0 comments on commit 4d86a4c

Please sign in to comment.