Skip to content

Commit e39d485

Browse files
committed
Correctly handle quoting and expansion when sourcing
1 parent c7d4869 commit e39d485

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

server/src/util/__tests__/sourcing.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('getSourcedUris', () => {
1111
expect(result).toEqual(new Set([]))
1212
})
1313

14-
it('returns an empty set if no files were sourced', () => {
14+
it('returns a set of sourced files', () => {
1515
const result = getSourcedUris({
1616
fileContent: `
1717
@@ -26,6 +26,13 @@ describe('getSourcedUris', () => {
2626
source ~/myscript
2727
2828
# source ...
29+
30+
source "./my_quoted_file.sh"
31+
32+
source "$LIBPATH" # dynamic imports not supported
33+
34+
# conditional is currently not supported
35+
if [[ -z $__COMPLETION_LIB_LOADED ]]; then source "$LIBPATH" ; fi
2936
`,
3037
fileUri,
3138
})
@@ -36,6 +43,7 @@ describe('getSourcedUris', () => {
3643
`${fileDirectory}/x`,
3744
`${fileDirectory}/relative/to-this.sh`,
3845
`${homedir()}/myscript`,
46+
`${fileDirectory}/my_quoted_file.sh`,
3947
]),
4048
)
4149
})

server/src/util/sourcing.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ export function getSourcedLocation({
7272

7373
const mapPathToUri = (path: string): string => path.replace('file:', 'file://')
7474

75+
const stripQuotes = (path: string): string => {
76+
const first = path[0]
77+
const last = path[path.length - 1]
78+
79+
if (first === last && [`"`, `'`].includes(first)) {
80+
return path.slice(1, -1)
81+
}
82+
83+
return path
84+
}
85+
7586
const getSourcedUri = ({
7687
relativePath,
7788
uri,
@@ -83,10 +94,15 @@ const getSourcedUri = ({
8394
// - we could try to resolve the path
8495
// - "If filename does not contain a slash, file names in PATH are used to find
8596
// the directory containing filename." (see https://ss64.com/osx/source.html)
97+
const unquotedRelativePath = stripQuotes(relativePath)
98+
99+
if (unquotedRelativePath.includes('$')) {
100+
return null
101+
}
86102

87-
const resultPath = relativePath.startsWith('~')
88-
? untildify(relativePath)
89-
: path.join(path.dirname(uri), relativePath)
103+
const resultPath = unquotedRelativePath.startsWith('~')
104+
? untildify(unquotedRelativePath)
105+
: path.join(path.dirname(uri), unquotedRelativePath)
90106

91107
return mapPathToUri(resultPath)
92108
}

0 commit comments

Comments
 (0)