Skip to content

Bug: DependencyConstraint.d.ts fails to compile due to phantom dependency on "semver" #8259

Closed
@octogonz

Description

@octogonz

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

rule-tester

Repro Code

Try to compile an ESLint plugin project with this code and `skipLibCheck: false` in **tsconfig.json**:

import { RuleTester } from '@typescript-eslint/rule-tester';

Expected Result

The code should compile without errors.

Actual Result

tsc reports a build failure like this:

../../common/temp/node_modules/.pnpm/@typescript-eslint+rule-tester@6.19.0_@eslint+eslintrc@3.0.0_eslint@8.7.0_typescript@5.3.3/node_modules
/@typescript-eslint/rule-tester/dist/types
/DependencyConstraint.d.ts:1:35 - error TS7016: Could not find a declaration file for module 'semver'. 'C:/Git/rushstack/common/temp/node_modules/.pnpm/semver@7.5.4/node_modules/semver/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/semver` if it exists or add a new declaration (.d.ts) file containing `declare module 'semver';`

1 import type { RangeOptions } from 'semver';
                                    ~~~~~~~~


Found 1 error in ../../common/temp/node_modules/.pnpm/@typescript-eslint+rule-tester@6.19.0_@eslint+eslintrc@3.0.0_eslint@8.7.0_typescript@5.3.3/node_modules/@typescript-eslint/rule-tester/dist/types/DependencyConstraint.d.ts:1

The problem is caused by this code:

import type { RangeOptions } from 'semver';
export interface SemverVersionConstraint {
readonly range: string;
readonly options?: RangeOptions | boolean;

It imports types from "semver", but that dependency is not declared in package.json.

Using an old-fashioned installation model such as NPM or Yarn Classic, then the semver package will likely get "hoisted" allowing the compiler to load typings from a semver folder installed to satisfy some other unrelated package. However with a modern installation model such as Yarn Plug'n'Play or PNPM, the import will fail; this is more correct behavior, because dependency versions should be explicitly specified rather than fished out of a node_modules folder with no idea what version you will get.

Versions

package version
@typescript-eslint/eslint-plugin 6.19.0
TypeScript 5.3.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions