Skip to content

Commit c1b4c5b

Browse files
author
Andy
authored
Fix bug in normalizeAndPreserveTrailingSlash: For "./", return "", not "/" (#21704)
1 parent 9f10888 commit c1b4c5b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ namespace FourSlash {
850850
ts.zipWith(actual, expected, (completion, expectedCompletion, index) => {
851851
const { name, insertText, replacementSpan } = typeof expectedCompletion === "string" ? { name: expectedCompletion, insertText: undefined, replacementSpan: undefined } : expectedCompletion;
852852
if (completion.name !== name) {
853-
this.raiseError(`Expected completion at index ${index} to be ${expectedCompletion}, got ${completion.name}`);
853+
this.raiseError(`Expected completion at index ${index} to be ${name}, got ${completion.name}`);
854854
}
855855
if (completion.insertText !== insertText) {
856856
this.raiseError(`Expected completion insert text at index ${index} to be ${insertText}, got ${completion.insertText}`);

src/services/pathCompletions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,13 @@ namespace ts.Completions.PathCompletions {
462462
}
463463

464464
function normalizeAndPreserveTrailingSlash(path: string) {
465-
return hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalizePath(path)) : normalizePath(path);
465+
if (path === "./") {
466+
// normalizePath turns "./" into "". "" + "/" would then be a rooted path instead of a relative one, so avoid this particular case.
467+
// There is no problem for adding "/" to a non-empty string -- it's only a problem at the beginning.
468+
return "";
469+
}
470+
const norm = normalizePath(path);
471+
return hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(norm) : norm;
466472
}
467473

468474
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /foo.ts
4+
////not read
5+
6+
// @Filename: /x/b.ts
7+
////export const x = 0;
8+
9+
// @Filename: /x/a.ts
10+
////import { } from "foo/[|/**/|]";
11+
12+
// @Filename: /x/tsconfig.json
13+
////{
14+
//// "compilerOptions": {
15+
//// "baseUrl": ".",
16+
//// "paths": {
17+
//// "foo/*": ["./*"]
18+
//// }
19+
//// }
20+
////}
21+
22+
const [replacementSpan] = test.ranges();
23+
verify.completionsAt("", ["a", "b"].map(name => ({ name, replacementSpan })));

0 commit comments

Comments
 (0)