Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an off-by-one error in getBaseDirectoriesFromRootDirs #55233

Merged
merged 7 commits into from
Sep 11, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,14 +647,14 @@ function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOpti
*/
function getBaseDirectoriesFromRootDirs(rootDirs: string[], basePath: string, scriptDirectory: string, ignoreCase: boolean): readonly string[] {
// Make all paths absolute/normalized if they are not already
rootDirs = rootDirs.map(rootDirectory => normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory)));
rootDirs = rootDirs.map(rootDirectory => ensureTrailingDirectorySeparator(normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory))));

// Determine the path to the directory containing the script relative to the root directory it is contained within
const relativeDirectory = firstDefined(rootDirs, rootDirectory => containsPath(rootDirectory, scriptDirectory, basePath, ignoreCase) ? scriptDirectory.substr(rootDirectory.length) : undefined)!; // TODO: GH#18217

// Now find a path for each potential directory that is to be merged with the one containing the script
return deduplicate<string>(
[...rootDirs.map(rootDirectory => combinePaths(rootDirectory, relativeDirectory)), scriptDirectory],
[...rootDirs.map(rootDirectory => combinePaths(rootDirectory, relativeDirectory)), scriptDirectory].map(baseDir => removeTrailingDirectorySeparator(baseDir)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to also add removeTrailingDirectorySeparator here because otherwise some tests got broken due to duplicated entries with mismatched trailing slashes. It's probably a good idea to return consistent results anyway.

equateStringsCaseSensitive,
compareStringsCaseSensitive,
);
Expand Down
37 changes: 37 additions & 0 deletions tests/cases/fourslash/completionForStringLiteralRelativeImport5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference path='fourslash.ts' />

// Should give correct completions for directories when invoked from within nested folders

// @rootDirs: /repo/src1,/repo/src2/,/repo/generated1,/repo/generated2/

// @Filename: /dir/secret_file.ts
//// /*secret_file*/

// @Filename: /repo/src1/dir/test1.ts
//// import * as foo1 from ".//*import_as1*/
//// import foo2 = require(".//*import_equals1*/
//// var foo3 = require(".//*require1*/

// @Filename: /repo/src2/dir/test2.ts
//// import * as foo1 from ".//*import_as2*/
//// import foo2 = require(".//*import_equals2*/
//// var foo3 = require(".//*require2*/

// @Filename: /repo/generated1/dir/f1.ts
//// /*f1*/

// @Filename: /repo/generated2/dir/f2.ts
//// /*f2*/

verify.completions(
{
marker: ["import_as1", "import_equals1", "require1"],
unsorted: ["f1", "f2", "test2"],
isNewIdentifierLocation: true,
},
{
marker: ["import_as2", "import_equals2", "require2"],
unsorted: ["f1", "f2", "test1"],
isNewIdentifierLocation: true,
}
);
34 changes: 34 additions & 0 deletions tests/cases/fourslash/completionForStringLiteralRelativeImport6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference path='fourslash.ts' />

// Should give correct completions for rootDirs regardless of the slash at the end

// @rootDirs: /repo/src1,/repo/src2/,/repo/generated1,/repo/generated2/

// @Filename: /repo/src1/test1.ts
//// import * as foo1 from "./dir//*import_as1*/
//// import foo2 = require("./dir//*import_equals1*/
//// var foo3 = require("./dir//*require1*/

// @Filename: /repo/src2/test2.ts
//// import * as foo1 from "./dir//*import_as2*/
//// import foo2 = require("./dir//*import_equals2*/
//// var foo3 = require("./dir//*require2*/

// @Filename: /repo/generated1/dir/f1.ts
//// /*f1*/

// @Filename: /repo/generated2/dir/f2.ts
//// /*f2*/

verify.completions(
{
marker: ["import_as1", "import_equals1", "require1"],
exact: ["f1", "f2"],
isNewIdentifierLocation: true,
},
{
marker: ["import_as2", "import_equals2", "require2"],
exact: ["f1", "f2"],
isNewIdentifierLocation: true,
}
);