Skip to content

Properly compare path prefixes when generating module paths #40885

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

Merged
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/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ namespace ts.moduleSpecifiers {
: discoverProbableSymlinks(host.getSourceFiles(), getCanonicalFileName, cwd);

const symlinkedDirectories = links.getSymlinkedDirectories();
const compareStrings = (!host.useCaseSensitiveFileNames || host.useCaseSensitiveFileNames()) ? compareStringsCaseSensitive : compareStringsCaseInsensitive;
const useCaseSensitiveFileNames = !host.useCaseSensitiveFileNames || host.useCaseSensitiveFileNames();
const result = symlinkedDirectories && forEachEntry(symlinkedDirectories, (resolved, path) => {
if (resolved === false) return undefined;
if (startsWithDirectory(importingFileName, resolved.realPath, getCanonicalFileName)) {
return undefined; // Don't want to a package to globally import from itself
}

return forEach(targets, target => {
if (compareStrings(target.slice(0, resolved.real.length), resolved.real) !== Comparison.EqualTo) {
if (!containsPath(resolved.real, target, !useCaseSensitiveFileNames)) {
return;
}

Expand Down
49 changes: 49 additions & 0 deletions tests/baselines/reference/declarationEmitSymlinkPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [tests/cases/compiler/declarationEmitSymlinkPaths.ts] ////

//// [package.json]
{
"name": "search",
"version": "0.0.1",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false
}
//// [index.d.ts]
export declare function test<T> (a: () => T): () => T;
//// [NotificationAPIUtils.ts]
export type NotificationRequest = {};
export type NotificationResponse = {};
export function getNotification(): NotificationResponse {
return {};
}
//// [NotificationStore.ts]
import { test } from "search/lib/index";
import { getNotification } from "../API/NotificationAPIUtils";

export const NotificationScenario = test(
getNotification
);

//// [NotificationAPIUtils.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNotification = void 0;
function getNotification() {
return {};
}
exports.getNotification = getNotification;
//// [NotificationStore.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationScenario = void 0;
var index_1 = require("search/lib/index");
var NotificationAPIUtils_1 = require("../API/NotificationAPIUtils");
exports.NotificationScenario = index_1.test(NotificationAPIUtils_1.getNotification);


//// [NotificationAPIUtils.d.ts]
export declare type NotificationRequest = {};
export declare type NotificationResponse = {};
export declare function getNotification(): NotificationResponse;
//// [NotificationStore.d.ts]
export declare const NotificationScenario: () => import("../API/NotificationAPIUtils").NotificationResponse;
36 changes: 36 additions & 0 deletions tests/baselines/reference/declarationEmitSymlinkPaths.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== /packages/search/lib/index.d.ts ===
export declare function test<T> (a: () => T): () => T;
>test : Symbol(test, Decl(index.d.ts, 0, 0))
>T : Symbol(T, Decl(index.d.ts, 0, 29))
>a : Symbol(a, Decl(index.d.ts, 0, 33))
>T : Symbol(T, Decl(index.d.ts, 0, 29))
>T : Symbol(T, Decl(index.d.ts, 0, 29))

=== /packages/search-prefix/src/API/NotificationAPIUtils.ts ===
export type NotificationRequest = {};
>NotificationRequest : Symbol(NotificationRequest, Decl(NotificationAPIUtils.ts, 0, 0))

export type NotificationResponse = {};
>NotificationResponse : Symbol(NotificationResponse, Decl(NotificationAPIUtils.ts, 0, 37))

export function getNotification(): NotificationResponse {
>getNotification : Symbol(getNotification, Decl(NotificationAPIUtils.ts, 1, 38))
>NotificationResponse : Symbol(NotificationResponse, Decl(NotificationAPIUtils.ts, 0, 37))

return {};
}
=== /packages/search-prefix/src/Store/NotificationStore.ts ===
import { test } from "search/lib/index";
>test : Symbol(test, Decl(NotificationStore.ts, 0, 8))

import { getNotification } from "../API/NotificationAPIUtils";
>getNotification : Symbol(getNotification, Decl(NotificationStore.ts, 1, 8))

export const NotificationScenario = test(
>NotificationScenario : Symbol(NotificationScenario, Decl(NotificationStore.ts, 3, 12))
>test : Symbol(test, Decl(NotificationStore.ts, 0, 8))

getNotification
>getNotification : Symbol(getNotification, Decl(NotificationStore.ts, 1, 8))

);
34 changes: 34 additions & 0 deletions tests/baselines/reference/declarationEmitSymlinkPaths.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== /packages/search/lib/index.d.ts ===
export declare function test<T> (a: () => T): () => T;
>test : <T>(a: () => T) => () => T
>a : () => T

=== /packages/search-prefix/src/API/NotificationAPIUtils.ts ===
export type NotificationRequest = {};
>NotificationRequest : NotificationRequest

export type NotificationResponse = {};
>NotificationResponse : NotificationResponse

export function getNotification(): NotificationResponse {
>getNotification : () => NotificationResponse

return {};
>{} : {}
}
=== /packages/search-prefix/src/Store/NotificationStore.ts ===
import { test } from "search/lib/index";
>test : <T>(a: () => T) => () => T

import { getNotification } from "../API/NotificationAPIUtils";
>getNotification : () => import("/packages/search-prefix/src/API/NotificationAPIUtils").NotificationResponse

export const NotificationScenario = test(
>NotificationScenario : () => import("/packages/search-prefix/src/API/NotificationAPIUtils").NotificationResponse
>test( getNotification) : () => import("/packages/search-prefix/src/API/NotificationAPIUtils").NotificationResponse
>test : <T>(a: () => T) => () => T

getNotification
>getNotification : () => import("/packages/search-prefix/src/API/NotificationAPIUtils").NotificationResponse

);
29 changes: 29 additions & 0 deletions tests/cases/compiler/declarationEmitSymlinkPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @declaration: true
// @outDir: /packages/search-prefix/lib
// @rootDir: /packages/search-prefix/src
// @target: es5
// @filename: /packages/search/package.json
{
"name": "search",
"version": "0.0.1",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false
}
// @filename: /packages/search/lib/index.d.ts
export declare function test<T> (a: () => T): () => T;
// @filename: /packages/search-prefix/src/API/NotificationAPIUtils.ts
export type NotificationRequest = {};
export type NotificationResponse = {};
export function getNotification(): NotificationResponse {
return {};
}
// @filename: /packages/search-prefix/src/Store/NotificationStore.ts
import { test } from "search/lib/index";
import { getNotification } from "../API/NotificationAPIUtils";

export const NotificationScenario = test(
getNotification
);
// @link: /packages/search -> /node_modules/search
// @link: /packages/search-prefix -> /node_modules/search-prefix