Skip to content

packageId is not set when calling resolveModuleName on a directory with an index.d.ts file in a dependencyΒ #63547

@TheLazySquid

Description

@TheLazySquid

πŸ”Ž Search Terms

packageId, resolveModuleName, index.d.ts

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about this issue

⏯ Playground Link

https://github.com/user-attachments/files/28815038/repro.zip

πŸ’» Code

import ts from "typescript";
import { resolve } from "path";

const importer = resolve("node_modules/@dimforge/rapier2d-compat/pipeline/world.d.ts");

const resolved1 = ts.resolveModuleName("../control", importer, {}, ts.sys);
const resolved2 = ts.resolveModuleName("../control/index", importer, {}, ts.sys);

console.log(resolved1.resolvedModule.packageId); // undefined
console.log(resolved2.resolvedModule.packageId); // defined properly
console.log(resolved1.resolvedModule.resolvedFileName === resolved2.resolvedModule.resolvedFileName); // true

πŸ™ Actual behavior

If a dependency contains a directory with a file called index.d.ts, say dir/index.d.ts, resolving the file with typescript.resolveModuleName only sets resolvedModule.packageId when resolving dir/index, and not when resolving dir. In both cases resolvedModule.resolvedFileName is the same.

I tested this with moduleResolution: "bundler" and ran into the same issue.

πŸ™‚ Expected behavior

packageId should be set regardless of if the index file was resolved indirectly

Additional information about the issue

I noticed this issue with @dimforge/rapier2d-compat, but I imagine it happens elsewhere. This is an issue for me because rollup-plugin-dts doesn't know that it should inline the types because it doesn't know the name of the module they are from.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions