Skip to content

Commit 72b3f2f

Browse files
authored
Handle urls in import path by making vfs not panic on Url paths (#2685)
1 parent b2ff5bf commit 72b3f2f

13 files changed

Lines changed: 119 additions & 1 deletion

internal/project/untitled_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,26 @@ x++;`
159159
// We expect to find 4 references
160160
assert.Assert(t, len(refs) == 4, "Expected 4 references, got %d", len(refs))
161161
}
162+
163+
func TestImportsInUntitled(t *testing.T) {
164+
t.Parallel()
165+
if !bundled.Embedded {
166+
t.Skip("bundled files are not embedded")
167+
}
168+
169+
files := map[string]any{
170+
// Make sure typings directory exists so it would actually try to fetch typings from this location
171+
projecttestutil.TestTypingsLocation + "/node_modules/@types/somelib/index.d.ts": `export const x: number;`,
172+
}
173+
session, _ := projecttestutil.Setup(files)
174+
content := `import "https://deno.land/std@0.208.0/path/mod.ts"
175+
import "./relative"
176+
`
177+
uri1 := lsproto.DocumentUri("untitled:Untitled-1")
178+
session.DidOpenFile(context.Background(), uri1, 1, content, lsproto.LanguageKindTypeScript)
179+
180+
// 2) Wait for ATA/background tasks to finish, then get a language service for the first file
181+
session.WaitForBackgroundTasks()
182+
_, err := session.GetLanguageService(context.Background(), uri1)
183+
assert.NilError(t, err)
184+
}

internal/vfs/internal/internal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ type Common struct {
1919

2020
func RootLength(p string) int {
2121
l := tspath.GetEncodedRootLength(p)
22-
if l <= 0 {
22+
if l == 0 {
2323
panic(fmt.Sprintf("vfs: path %q is not absolute", p))
24+
} else if l < 0 {
25+
return ^l
2426
}
2527
return l
2628
}

internal/vfs/iovfs/iofs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func From(fsys fs.FS, useCaseSensitiveFileNames bool) FsWithSys {
112112
p := tspath.RemoveTrailingDirectorySeparator(root)
113113
sub, err := fs.Sub(fsys, p)
114114
if err != nil {
115+
if tspath.IsUrl(root) {
116+
return nil
117+
}
115118
panic(fmt.Sprintf("vfs: failed to create sub file system for %q: %v", p, err))
116119
}
117120
return sub
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/importUrl.ts] ////
2+
3+
//// [index.ts]
4+
import "https://deno.land/std@0.208.0/path/mod.ts"
5+
6+
7+
//// [index.js]
8+
"use strict";
9+
Object.defineProperty(exports, "__esModule", { value: true });
10+
require("https://deno.land/std@0.208.0/path/mod.ts");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/importUrl.ts] ////
2+
3+
=== /src/index.ts ===
4+
5+
import "https://deno.land/std@0.208.0/path/mod.ts"
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
======== Resolving module 'https://deno.land/std@0.208.0/path/mod.ts' from '/src/index.ts'. ========
2+
Module resolution kind is not specified, using 'Bundler'.
3+
Resolving in CJS mode with conditions 'require', 'types'.
4+
File '/src/package.json' does not exist.
5+
File '/package.json' does not exist.
6+
Skipping module 'https://deno.land/std@0.208.0/path/mod.ts' that looks like an absolute URI, target file types: TypeScript, JavaScript, Declaration, JSON.
7+
======== Module name 'https://deno.land/std@0.208.0/path/mod.ts' was not resolved. ========
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/importUrl.ts] ////
2+
3+
=== /src/index.ts ===
4+
5+
import "https://deno.land/std@0.208.0/path/mod.ts"
6+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/importUrlNodeModulesExist.ts] ////
2+
3+
//// [package.json]
4+
{ "name": "foo", "version": "1.0.0" }
5+
6+
//// [index.d.ts]
7+
export declare function useFoo(): string;
8+
9+
//// [index.ts]
10+
import "https://deno.land/std@0.208.0/path/mod.ts"
11+
12+
13+
14+
//// [index.js]
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
require("https://deno.land/std@0.208.0/path/mod.ts");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/importUrlNodeModulesExist.ts] ////
2+
3+
=== /node_modules/foo/index.d.ts ===
4+
export declare function useFoo(): string;
5+
>useFoo : Symbol(useFoo, Decl(index.d.ts, 0, 0))
6+
7+
=== /src/index.ts ===
8+
9+
import "https://deno.land/std@0.208.0/path/mod.ts"
10+
11+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
======== Resolving module 'https://deno.land/std@0.208.0/path/mod.ts' from '/src/index.ts'. ========
2+
Module resolution kind is not specified, using 'Bundler'.
3+
Resolving in CJS mode with conditions 'require', 'types'.
4+
File '/src/package.json' does not exist.
5+
File '/package.json' does not exist.
6+
Skipping module 'https://deno.land/std@0.208.0/path/mod.ts' that looks like an absolute URI, target file types: TypeScript, JavaScript, Declaration, JSON.
7+
======== Module name 'https://deno.land/std@0.208.0/path/mod.ts' was not resolved. ========

0 commit comments

Comments
 (0)