Skip to content

Commit 0cfd664

Browse files
committed
Fix resolving schemas on web
- Use URI instead of filepath on web, since the web environment sometimes uses URIs with schemes other than `file://` for the workspace files Fixes redhat-developer/vscode-yaml#1194 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 6ab8de8 commit 0cfd664

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/languageservice/services/schemaRequestHandler.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { URI } from 'vscode-uri';
2-
import { Connection, WorkspaceFolder } from 'vscode-languageserver';
2+
import { Connection, RequestType, WorkspaceFolder } from 'vscode-languageserver';
33
import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light';
44
import * as URL from 'url';
55
import { CustomSchemaContentRequest, VSCodeContentRequest } from '../../requestTypes';
66
import { isRelativePath, relativeToAbsolutePath } from '../utils/paths';
77
import { WorkspaceContextService } from '../yamlLanguageService';
8+
import { dirname, join } from 'path';
89

910
export interface FileSystem {
1011
readFile(fsPath: string, encoding?: string): Promise<string>;
1112
}
1213

14+
// eslint-disable-next-line @typescript-eslint/no-namespace
15+
namespace FSReadUri {
16+
export const type: RequestType<string, string, unknown> = new RequestType('fs/readUri');
17+
}
18+
1319
/**
1420
* Handles schema content requests given the schema URI
1521
* @param uri can be a local file, vscode request, http(s) request or a custom request
@@ -29,7 +35,18 @@ export const schemaRequestHandler = (
2935
// If the requested schema URI is a relative file path
3036
// Convert it into a proper absolute path URI
3137
if (isRelativePath(uri)) {
32-
uri = relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
38+
if (workspaceFolders.length === 1) {
39+
const wsUri = URI.parse(workspaceFolders[0].uri);
40+
const wsDirname = dirname(wsUri.path);
41+
connection.window.showInformationMessage(`wsDirname: ${wsDirname}`);
42+
const modifiedUri = wsUri.with({ path: join(wsDirname, uri) });
43+
return connection.sendRequest(FSReadUri.type, modifiedUri.toString()).catch((e) => {
44+
connection.window.showErrorMessage(`failed to get content of '${modifiedUri}': ${e}`);
45+
throw e;
46+
});
47+
} else {
48+
uri = relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
49+
}
3350
}
3451

3552
let scheme = URI.parse(uri).scheme.toLowerCase();

0 commit comments

Comments
 (0)