Skip to content

Commit 4d04fa9

Browse files
author
Andy
authored
Fix bug in normalizeAndPreserveTrailingSlash: For "./", return "", not "/" (#21704) (#21726)
* Fix bug in normalizeAndPreserveTrailingSlash: For "./", return "", not "/" (#21704) * Also check for '.\'
1 parent e66e471 commit 4d04fa9

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/harness/fourslash.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ namespace FourSlash {
865865
ts.zipWith(actual, expected, (completion, expectedCompletion, index) => {
866866
const { name, insertText, replacementSpan } = typeof expectedCompletion === "string" ? { name: expectedCompletion, insertText: undefined, replacementSpan: undefined } : expectedCompletion;
867867
if (completion.name !== name) {
868-
this.raiseError(`Expected completion at index ${index} to be ${expectedCompletion}, got ${completion.name}`);
868+
this.raiseError(`Expected completion at index ${index} to be ${name}, got ${completion.name}`);
869869
}
870870
if (completion.insertText !== insertText) {
871871
this.raiseError(`Expected completion insert text at index ${index} to be ${insertText}, got ${completion.insertText}`);

src/services/pathCompletions.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ namespace ts.Completions.PathCompletions {
455455
}
456456

457457
function normalizeAndPreserveTrailingSlash(path: string) {
458-
return hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalizePath(path)) : normalizePath(path);
458+
if (path === "./" || path === ".\\") {
459+
// normalizePath turns "./" into "". "" + "/" would then be a rooted path instead of a relative one, so avoid this particular case.
460+
// There is no problem for adding "/" to a non-empty string -- it's only a problem at the beginning.
461+
return "";
462+
}
463+
const norm = normalizePath(path);
464+
return hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(norm) : norm;
459465
}
460466

461467
/**
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)