Skip to content

moduleNameResolver: preserve originalPath of symlinked module on cache hit #26310

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

Closed
Closed
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
30 changes: 14 additions & 16 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ts {
}

function withPackageId(packageId: PackageId | undefined, r: PathAndExtension | undefined): Resolved | undefined {
return r && { path: r.path, extension: r.ext, packageId };
return r && { path: r.path, extension: r.ext, packageId, originalPath: undefined };
}

function noPackageId(r: PathAndExtension | undefined): Resolved | undefined {
Expand All @@ -29,6 +29,7 @@ namespace ts {
path: string;
extension: Extension;
packageId: PackageId | undefined;
originalPath: string | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Make this optional so you dont need to set it in withPackageId

}

/** Result of trying to resolve a module at a file. Needs to have 'packageId' added later. */
Expand Down Expand Up @@ -62,9 +63,9 @@ namespace ts {
return { fileName: resolved.path, packageId: resolved.packageId };
}

function createResolvedModuleWithFailedLookupLocations(resolved: Resolved | undefined, originalPath: string | undefined, isExternalLibraryImport: boolean, failedLookupLocations: string[]): ResolvedModuleWithFailedLookupLocations {
function createResolvedModuleWithFailedLookupLocations(resolved: Resolved | undefined, isExternalLibraryImport: boolean, failedLookupLocations: string[]): ResolvedModuleWithFailedLookupLocations {
return {
resolvedModule: resolved && { resolvedFileName: resolved.path, originalPath, extension: resolved.extension, isExternalLibraryImport, packageId: resolved.packageId },
resolvedModule: resolved && { resolvedFileName: resolved.path, originalPath: resolved.originalPath, extension: resolved.extension, isExternalLibraryImport, packageId: resolved.packageId },
failedLookupLocations
};
}
Expand Down Expand Up @@ -753,12 +754,12 @@ namespace ts {
tryResolve(Extensions.JavaScript) ||
(compilerOptions.resolveJsonModule ? tryResolve(Extensions.Json) : undefined));
if (result && result.value) {
const { resolved, originalPath, isExternalLibraryImport } = result.value;
return createResolvedModuleWithFailedLookupLocations(resolved, originalPath, isExternalLibraryImport, failedLookupLocations);
const { resolved, isExternalLibraryImport } = result.value;
return createResolvedModuleWithFailedLookupLocations(resolved, isExternalLibraryImport, failedLookupLocations);
}
return { resolvedModule: undefined, failedLookupLocations };

function tryResolve(extensions: Extensions): SearchResult<{ resolved: Resolved, originalPath?: string, isExternalLibraryImport: boolean }> {
function tryResolve(extensions: Extensions): SearchResult<{ resolved: Resolved, isExternalLibraryImport: boolean }> {
const loader: ResolutionKindSpecificLoader = (extensions, candidate, failedLookupLocations, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, /*considerPackageJson*/ true);
const resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, failedLookupLocations, state);
if (resolved) {
Expand All @@ -773,17 +774,14 @@ namespace ts {
if (!resolved) return undefined;

let resolvedValue = resolved.value;
let originalPath: string | undefined;
if (!compilerOptions.preserveSymlinks && resolvedValue) {
originalPath = resolvedValue.path;
if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm we need better way to identify that its from cache and no need to calculate originalPath since when its not symbollink and its coming from cache we shouldnt need to do realPath logic here. Considering this involves more changes than previously expected, do you mind if i take it over?

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 don't mind you taking over.
I think this could easily be fixed by always setting originalPath to the original path regardless of the result of realpath. If originalPath is present it's from the cache, otherwise resolve the path using realpath

const path = realPath(resolvedValue.path, host, traceEnabled);
if (path === originalPath) {
originalPath = undefined;
if (path !== resolvedValue.path) {
resolvedValue = { ...resolvedValue, path, originalPath: resolvedValue.path };
}
resolvedValue = { ...resolvedValue, path };
}
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
return { value: resolvedValue && { resolved: resolvedValue, originalPath, isExternalLibraryImport: true } };
return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport: true } };
}
else {
const { path: candidate, parts } = normalizePathAndParts(combinePaths(containingDirectory, moduleName));
Expand Down Expand Up @@ -1233,7 +1231,7 @@ namespace ts {
trace(host, Diagnostics.Resolution_for_module_0_was_found_in_cache_from_location_1, moduleName, containingDirectory);
}
failedLookupLocations.push(...result.failedLookupLocations);
return { value: result.resolvedModule && { path: result.resolvedModule.resolvedFileName, extension: result.resolvedModule.extension, packageId: result.resolvedModule.packageId } };
return { value: result.resolvedModule && { path: result.resolvedModule.resolvedFileName, extension: result.resolvedModule.extension, packageId: result.resolvedModule.packageId, originalPath: result.resolvedModule.originalPath } };
}
}

Expand All @@ -1245,7 +1243,7 @@ namespace ts {

const resolved = tryResolve(Extensions.TypeScript) || tryResolve(Extensions.JavaScript);
// No originalPath because classic resolution doesn't resolve realPath
return createResolvedModuleWithFailedLookupLocations(resolved && resolved.value, /*originalPath*/ undefined, /*isExternalLibraryImport*/ false, failedLookupLocations);
return createResolvedModuleWithFailedLookupLocations(resolved && resolved.value, /*isExternalLibraryImport*/ false, failedLookupLocations);

function tryResolve(extensions: Extensions): SearchResult<Resolved> {
const resolvedUsingSettings = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loadModuleFromFileNoPackageId, failedLookupLocations, state);
Expand Down Expand Up @@ -1292,7 +1290,7 @@ namespace ts {
const state: ModuleResolutionState = { compilerOptions, host, traceEnabled };
const failedLookupLocations: string[] = [];
const resolved = loadModuleFromNodeModulesOneLevel(Extensions.DtsOnly, moduleName, globalCache, failedLookupLocations, state);
return createResolvedModuleWithFailedLookupLocations(resolved, /*originalPath*/ undefined, /*isExternalLibraryImport*/ true, failedLookupLocations);
return createResolvedModuleWithFailedLookupLocations(resolved, /*isExternalLibraryImport*/ true, failedLookupLocations);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/testRunner/unittests/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,31 @@ namespace ts {
checkResolvedModule(resolution.resolvedModule, createResolvedModule(resolvedFileName, /*isExternalLibraryImport*/ true));
});
}

it("preserves originalPath on cache hit", () => {
const originalHost = createModuleResolutionHost(
/*hasDirectoryExists*/ true,
{ name: "/linked/index.d.ts", symlinks: ["/app/node_modules/linked/index.d.ts"] },
{ name: "/app/node_modules/linked/package.json", content: '{"version": "0.0.0", "main": "./index"}' },
);
let realpathCalls = 0;
const host: ModuleResolutionHost = {
...originalHost,
realpath(path) {
assert.strictEqual(++realpathCalls, 1, "realpath should be cached");
return originalHost.realpath!(path);
}
};
const cache = createModuleResolutionCache("/", (f) => f);
const compilerOptions: CompilerOptions = { moduleResolution: ModuleResolutionKind.NodeJs };
let resolution = resolveModuleName("linked", "/app/src/app.ts", compilerOptions, host, cache);
checkResolvedModule(resolution.resolvedModule, createResolvedModule("/linked/index.d.ts", /*isExternalLibraryImport*/ true));
assert.strictEqual(resolution.resolvedModule && resolution.resolvedModule.originalPath, "/app/node_modules/linked/index.d.ts");

resolution = resolveModuleName("linked", "/app/lib/main.ts", compilerOptions, host, cache);
checkResolvedModule(resolution.resolvedModule, createResolvedModule("/linked/index.d.ts", /*isExternalLibraryImport*/ true));
assert.strictEqual(resolution.resolvedModule && resolution.resolvedModule.originalPath, "/app/node_modules/linked/index.d.ts");
});
});

describe("Module resolution - relative imports", () => {
Expand Down