Skip to content
Open
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
22 changes: 22 additions & 0 deletions packages/typescript/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,28 @@ describe("API", { concurrency }, () => {
assert.equal(callbackCalls, 1);
});

test("module resolvers normalize URI document identifiers", async () => {
const resolvedFileName = "/workspace/sub/node_modules/pkg/index.d.ts";
await using api = spawnAPI({
"/workspace/sub/node_modules/pkg/package.json": JSON.stringify({ name: "pkg", version: "1.0.0", types: "index.d.ts" }),
[resolvedFileName]: "export {};",
});
const resolver = await api.createModuleResolver({
typeRoots: [],
moduleResolution: ModuleResolutionKind.NodeNext,
});
for (const prefix of ["file:///", "file:/", "file:", "/"]) {
const directory = { uri: `${prefix}workspace/sub/src` };
const file = { uri: `${prefix}workspace/sub/src/index.ts` };
assert.equal((await resolver.resolveModuleName("pkg", directory)).resolvedModule?.resolvedFileName, resolvedFileName, directory.uri);
assert.equal(
(await resolver.resolveTypeReferenceDirective("pkg", file)).resolvedTypeReferenceDirective?.resolvedFileName,
resolvedFileName,
file.uri,
);
}
});

test("type reference resolver uses configured roots independently of module overrides", async () => {
await using api = new API({
cwd: "/workspace/project",
Expand Down
22 changes: 22 additions & 0 deletions packages/typescript/test/sync/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,28 @@ describe("API", { concurrency }, () => {
assert.equal(callbackCalls, 1);
});

test("module resolvers normalize URI document identifiers", () => {
const resolvedFileName = "/workspace/sub/node_modules/pkg/index.d.ts";
using api = spawnAPI({
"/workspace/sub/node_modules/pkg/package.json": JSON.stringify({ name: "pkg", version: "1.0.0", types: "index.d.ts" }),
[resolvedFileName]: "export {};",
});
const resolver = api.createModuleResolver({
typeRoots: [],
moduleResolution: ModuleResolutionKind.NodeNext,
});
for (const prefix of ["file:///", "file:/", "file:", "/"]) {
const directory = { uri: `${prefix}workspace/sub/src` };
const file = { uri: `${prefix}workspace/sub/src/index.ts` };
assert.equal((resolver.resolveModuleName("pkg", directory)).resolvedModule?.resolvedFileName, resolvedFileName, directory.uri);
assert.equal(
(resolver.resolveTypeReferenceDirective("pkg", file)).resolvedTypeReferenceDirective?.resolvedFileName,
resolvedFileName,
file.uri,
);
}
});

test("type reference resolver uses configured roots independently of module overrides", () => {
using api = new API({
cwd: "/workspace/project",
Expand Down
9 changes: 6 additions & 3 deletions tsc/internal/api/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ type InitializeResponse struct {

// DocumentIdentifier identifies a document by either a file name (plain string) or a URI object.
// On the wire it is string | { uri: string }.
// URI inputs use the language server's URI normalization and validation.
//
// @example
//
Expand Down Expand Up @@ -358,14 +359,16 @@ func (d *DocumentIdentifier) UnmarshalJSONFrom(dec *json.Decoder) error {
if foundURI {
return fmt.Errorf("DocumentIdentifier: duplicate field %q", key.String())
}
val, err := dec.ReadToken()
val, err := dec.ReadValue()
if err != nil {
return err
}
if val.Kind() != '"' || val.String() == "" {
if val.Kind() != '"' || string(val) == `""` {
return errors.New("DocumentIdentifier: uri must be a non-empty string")
}
d.URI = lsproto.DocumentUri(val.String())
if err := json.Unmarshal(val, &d.URI); err != nil {
return err
}
foundURI = true
} else if err := dec.SkipValue(); err != nil {
return err
Expand Down
87 changes: 87 additions & 0 deletions tsc/internal/api/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestDocumentIdentifierUnmarshalJSON(t *testing.T) {
input string
fileName string
uri string
resolved string
err string
}{
{
Expand All @@ -46,6 +47,83 @@ func TestDocumentIdentifierUnmarshalJSON(t *testing.T) {
input: `{"uri":"file:///foo.ts"}`,
uri: "file:///foo.ts",
},
{
name: "file uri with one slash",
input: `{"uri":"file:/workspace/sub/src/index.ts"}`,
uri: "file:///workspace/sub/src/index.ts",
resolved: "/workspace/sub/src/index.ts",
},
{
name: "file uri with opaque path",
input: `{"uri":"file:workspace/sub/src/index.ts"}`,
uri: "file:///workspace/sub/src/index.ts",
resolved: "/workspace/sub/src/index.ts",
},
{
name: "uri without scheme",
input: `{"uri":"/workspace/sub/src/index.ts"}`,
uri: "file:///workspace/sub/src/index.ts",
resolved: "/workspace/sub/src/index.ts",
},
{
name: "escaped file uri",
input: `{"uri":"file:/workspace/file%20name.ts"}`,
uri: "file:///workspace/file%20name.ts",
resolved: "/workspace/file name.ts",
},
{
name: "escaped opaque file uri",
input: `{"uri":"file:workspace/file%20name.ts"}`,
uri: "file:///workspace/file%20name.ts",
resolved: "/workspace/file name.ts",
},
{
name: "invalid opaque file uri escape",
input: `{"uri":"file:workspace/file%xx.ts"}`,
err: "invalid URI",
},
{
name: "file uri with authority",
input: `{"uri":"file://server/share/index.ts"}`,
uri: "file://server/share/index.ts",
resolved: "//server/share/index.ts",
},
{
name: "empty file uri path",
input: `{"uri":"file://"}`,
uri: "file:///",
resolved: "/",
},
{
name: "untitled uri",
input: `{"uri":"untitled:Untitled-1"}`,
uri: "untitled:Untitled-1",
},
{
name: "remote uri",
input: `{"uri":"vscode-remote://ssh-remote+host/workspace/file%20name.ts?query#fragment"}`,
uri: "vscode-remote://ssh-remote+host/workspace/file%20name.ts?query#fragment",
},
{
name: "file uri as plain file name",
input: `"file:/workspace/sub/src/index.ts"`,
fileName: "file:/workspace/sub/src/index.ts",
},
{
name: "invalid uri escape",
input: `{"uri":"file:///test%xx.ts"}`,
err: "invalid URI",
},
{
name: "invalid non-file uri",
input: `{"uri":"vscode-remote://host/test%xx.ts"}`,
err: "invalid URI",
},
{
name: "file uri path without authority",
input: `{"uri":"file:////server/share"}`,
err: "invalid URI",
},
{
name: "uri object with unknown fields",
input: `{"uri":"file:///foo.ts","extra":true}`,
Expand Down Expand Up @@ -76,6 +154,11 @@ func TestDocumentIdentifierUnmarshalJSON(t *testing.T) {
input: `{"uri":42}`,
err: "uri must be a non-empty string",
},
{
name: "null uri",
input: `{"uri":null}`,
err: "uri must be a non-empty string",
},
{
name: "duplicate uri",
input: `{"uri":"file:///foo.ts","uri":"file:///bar.ts"}`,
Expand All @@ -100,6 +183,10 @@ func TestDocumentIdentifierUnmarshalJSON(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, d.FileName, tt.fileName)
assert.Equal(t, string(d.URI), tt.uri)
if tt.resolved != "" {
assert.Equal(t, d.ToFileName("/cwd").AsString(), tt.resolved)
assert.Equal(t, string(d.ToURI("/cwd")), tt.uri)
}
})
}
}
Expand Down
6 changes: 5 additions & 1 deletion tsc/internal/lsp/lsproto/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ func unmarshalURI(dec *json.Decoder) (string, error) {
parsed.Scheme = "file"
}
if parsed.Scheme == "file" {
parsed.OmitHost = false
if parsed.Opaque != "" {
parsed.Path = parsed.Opaque
parsed.Path, err = url.PathUnescape(parsed.Opaque)
if err != nil {
return "", fmt.Errorf("invalid URI %q", value)
}
parsed.Opaque = ""
}
if parsed.Path == "" {
Expand Down
5 changes: 5 additions & 0 deletions tsc/internal/lsp/lsproto/lsp_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ func TestUnmarshalURI(t *testing.T) {
wantErr bool
}{
{name: "file", uri: "file:///test.ts", expected: "file:///test.ts"},
{name: "single slash file", uri: "file:/test.ts", expected: "file:///test.ts"},
{name: "escaped single slash file", uri: "file:/file%20name.ts", expected: "file:///file%20name.ts"},
{name: "untitled", uri: "untitled:Untitled-1", expected: "untitled:Untitled-1"},
{name: "empty file path", uri: "file://", expected: "file:///"},
{name: "relative file path", uri: "file:test.ts", expected: "file:///test.ts"},
{name: "escaped opaque file path", uri: "file:workspace/file%20name.ts", expected: "file:///workspace/file%20name.ts"},
{name: "escaped percent in opaque file path", uri: "file:workspace/file%2520name.ts", expected: "file:///workspace/file%2520name.ts"},
{name: "invalid opaque file escape", uri: "file:workspace/file%xx.ts", wantErr: true},
{name: "missing scheme", uri: "/test.ts", expected: "file:///test.ts"},
{name: "invalid escape", uri: "file:///test%xx.ts", wantErr: true},
{name: "path without authority", uri: "file:////server/share", wantErr: true},
Expand Down
Loading