Skip to content

Commit 9d3b96f

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 9d3b96f

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

src/languageservice/services/schemaRequestHandler.ts

Lines changed: 22 additions & 3 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
@@ -20,7 +26,8 @@ export const schemaRequestHandler = (
2026
workspaceFolders: WorkspaceFolder[],
2127
workspaceRoot: URI,
2228
useVSCodeContentRequest: boolean,
23-
fs: FileSystem
29+
fs: FileSystem,
30+
isWeb: boolean
2431
): Promise<string> => {
2532
if (!uri) {
2633
return Promise.reject('No schema specified');
@@ -29,7 +36,19 @@ export const schemaRequestHandler = (
2936
// If the requested schema URI is a relative file path
3037
// Convert it into a proper absolute path URI
3138
if (isRelativePath(uri)) {
32-
uri = relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
39+
// HACK: the fs/readUri extension is only available with vscode-yaml,
40+
// and this fix is specific to vscode-yaml on web, so don't use it in other cases
41+
if (workspaceFolders.length === 1 && isWeb) {
42+
const wsUri = URI.parse(workspaceFolders[0].uri);
43+
const wsDirname = dirname(wsUri.path);
44+
const modifiedUri = wsUri.with({ path: join(wsDirname, uri) });
45+
return connection.sendRequest(FSReadUri.type, modifiedUri.toString()).catch((e) => {
46+
connection.window.showErrorMessage(`failed to get content of '${modifiedUri}': ${e}`);
47+
throw e;
48+
});
49+
} else {
50+
uri = relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
51+
}
3352
}
3453

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

src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promi
6060
yamlSettings.workspaceFolders,
6161
yamlSettings.workspaceRoot,
6262
yamlSettings.useVSCodeContentRequest,
63-
fileSystem
63+
fileSystem,
64+
false
6465
);
6566
};
6667

src/webworker/yamlServerMain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ self.onmessage = (e) => {
4141
yamlSettings.workspaceFolders,
4242
yamlSettings.workspaceRoot,
4343
yamlSettings.useVSCodeContentRequest,
44-
fileSystem
44+
fileSystem,
45+
true
4546
);
4647
};
4748

test/bundlel10n.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ describe('Bundle l10n Test', () => {
3232
yamlSettings.workspaceFolders,
3333
yamlSettings.workspaceRoot,
3434
yamlSettings.useVSCodeContentRequest,
35-
testFileSystem
35+
testFileSystem,
36+
false
3637
);
3738
};
3839
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);

test/schemaRequestHandler.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe('Schema Request Handler Tests', () => {
3535
[],
3636
URI.parse(''),
3737
false,
38-
testFileSystem
38+
testFileSystem,
39+
false
3940
);
4041
expect(readFileStub).calledOnceWith('c:\\some\\window\\path\\scheme.json');
4142
const result = await resultPromise;
@@ -44,7 +45,7 @@ describe('Schema Request Handler Tests', () => {
4445

4546
it('UNIX URI should works', async () => {
4647
const connection = {} as Connection;
47-
const resultPromise = schemaRequestHandler(connection, '/some/unix/path/', [], URI.parse(''), false, testFileSystem);
48+
const resultPromise = schemaRequestHandler(connection, '/some/unix/path/', [], URI.parse(''), false, testFileSystem, false);
4849
const result = await resultPromise;
4950
expect(result).to.be.equal('{some: "json"}');
5051
});
@@ -57,7 +58,8 @@ describe('Schema Request Handler Tests', () => {
5758
[],
5859
URI.parse(''),
5960
false,
60-
testFileSystem
61+
testFileSystem,
62+
false
6163
);
6264
expect(readFileStub).calledOnceWith(URI.file('a:/some/window/path/scheme.json').fsPath);
6365
const result = await resultPromise;

test/utils/testHelper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export function setupLanguageService(languageSettings: LanguageSettings): TestLa
7676
yamlSettings.workspaceFolders,
7777
yamlSettings.workspaceRoot,
7878
yamlSettings.useVSCodeContentRequest,
79-
testFileSystem
79+
testFileSystem,
80+
false
8081
);
8182
};
8283
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);

0 commit comments

Comments
 (0)