Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/src/shellcheck/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,19 @@ describe('linter', () => {
diagnostics: [],
})
})

it('should handle non-file URI schemes gracefully', async () => {
const shell = ['#!/bin/bash', 'echo "hello"'].join('\n')

const nonFileUri = 'webdav://example.com/path/to/script.sh'
const document = TextDocument.create(nonFileUri, 'bash', 0, shell)

const [result] = await getLintingResult({
document,
sourcePaths: [],
})

expect(result.diagnostics).toEqual([])
expect(result.codeActions).toEqual({})
})
})
22 changes: 20 additions & 2 deletions server/src/shellcheck/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { fileURLToPath, URL } from 'node:url'

import { spawn } from 'child_process'
import * as LSP from 'vscode-languageserver/node'
Expand All @@ -17,6 +17,19 @@ import {
} from './types'

const DEBOUNCE_MS = 500

function safeFileURLToPath(uri: string): string | null {
try {
const url = new URL(uri)
if (url.protocol !== 'file:') {
return null
}
return fileURLToPath(uri)
} catch {
return null
}
}

type LinterOptions = {
executablePath: string
cwd?: string
Expand Down Expand Up @@ -91,10 +104,15 @@ export class Linter {
return { diagnostics: [], codeActions: {} }
}

const documentPath = safeFileURLToPath(document.uri)
const effectiveSourcePaths = documentPath
? [...sourcePaths, dirname(documentPath)]
: sourcePaths

const result = await this.runShellCheck(
documentText,
shellName,
[...sourcePaths, dirname(fileURLToPath(document.uri))],
effectiveSourcePaths,
additionalShellCheckArguments,
)

Expand Down
Loading